11 func TestReadAllOrWarnFail(t *testing.T) {
12 logChan = make(chan string)
15 // The special file /proc/self/mem can be opened for
16 // reading, but reading from byte 0 returns an error.
17 f, err := os.Open("/proc/self/mem")
19 t.Fatalf("Opening /proc/self/mem: %s", err)
21 if x, err := ReadAllOrWarn(f); err == nil {
22 t.Fatalf("Expected error, got %v", x)
25 if _, ok := <-logChan; !ok {
26 t.Fatalf("Expected error message about nonexistent file")
28 if msg, ok := <-logChan; ok {
29 t.Fatalf("Expected channel to close, got %s", msg)
33 func TestReadAllOrWarnSuccess(t *testing.T) {
34 logChan = make(chan string)
37 f, err := os.Open("./crunchstat_test.go")
39 t.Fatalf("Opening ./crunchstat_test.go: %s", err)
41 data, err := ReadAllOrWarn(f)
43 t.Fatalf("got error %s", err)
45 if matched, err := regexp.MatchString("^package main\n", string(data)); err != nil || !matched {
46 t.Fatalf("data failed regexp: %s", err)
49 if msg, ok := <-logChan; ok {
50 t.Fatalf("Expected channel to close, got %s", msg)
54 // Test that if CopyPipeToChan reads a line longer than
55 // bufio.MaxScanTokenSize, it emits an error to the output channel.
56 func TestCopyPipeToChanLongLines(t *testing.T) {
57 logChan := make(chan string)
58 control := make(chan bool)
60 pipeIn, pipeOut := io.Pipe()
61 go CopyPipeToChan(pipeIn, logChan, control)
64 long_line := make([]byte, bufio.MaxScanTokenSize+1)
65 for i := range long_line {
66 long_line[i] = byte('x')
68 pipeOut.Write(long_line)
71 // Expect error message from logChan.
74 if matched, err := regexp.MatchString("^crunchstat: line buffering error:.*token too long", errmsg); err != nil || !matched {
75 t.Fatalf("expected CopyPipeToChan error, got %s", errmsg)