14 func TestReadAllOrWarnFail(t *testing.T) {
15 logChan = make(chan string)
18 // The special file /proc/self/mem can be opened for
19 // reading, but reading from byte 0 returns an error.
20 f, err := os.Open("/proc/self/mem")
22 t.Fatalf("Opening /proc/self/mem: %s", err)
24 if x, err := ReadAllOrWarn(f); err == nil {
25 t.Fatalf("Expected error, got %v", x)
28 if _, ok := <-logChan; !ok {
29 t.Fatalf("Expected error message about nonexistent file")
31 if msg, ok := <-logChan; ok {
32 t.Fatalf("Expected channel to close, got %s", msg)
36 func TestReadAllOrWarnSuccess(t *testing.T) {
37 logChan = make(chan string)
40 f, err := os.Open("./crunchstat_test.go")
42 t.Fatalf("Opening ./crunchstat_test.go: %s", err)
44 data, err := ReadAllOrWarn(f)
46 t.Fatalf("got error %s", err)
48 if matched, err := regexp.MatchString("^package main\n", string(data)); err != nil || !matched {
49 t.Fatalf("data failed regexp: %s", err)
52 if msg, ok := <-logChan; ok {
53 t.Fatalf("Expected channel to close, got %s", msg)
57 // Test that CopyPipeToChan works even on lines longer than
58 // bufio.MaxScanTokenSize.
59 func TestCopyPipeToChanLongLines(t *testing.T) {
60 logChan := make(chan string)
61 control := make(chan bool)
63 pipeIn, pipeOut := io.Pipe()
64 go CopyPipeToChan(pipeIn, logChan, control)
66 sentBytes := make([]byte, bufio.MaxScanTokenSize + (1 << 22))
68 for i := range sentBytes {
69 // Some bytes that aren't newlines:
70 sentBytes[i] = byte((rand.Int() & 0xff) | 0x80)
72 pipeOut.Write([]byte("before\n"))
73 pipeOut.Write(sentBytes)
74 pipeOut.Write([]byte("\nafter\n"))
78 if before := <-logChan; before != "before" {
79 t.Fatalf("\"before\" not received (got \"%s\")", before)
81 receivedString := <-logChan
82 receivedBytes := []byte(receivedString)
83 if bytes.Compare(receivedBytes, sentBytes) != 0 {
84 t.Fatalf("sent %d bytes, got %d different bytes", len(sentBytes), len(receivedBytes))
86 if after := <-logChan; after != "after" {
87 t.Fatal("\"after\" not received")
90 case <-time.After(time.Second):