15 func TestReadAllOrWarnFail(t *testing.T) {
19 // The special file /proc/self/mem can be opened for
20 // reading, but reading from byte 0 returns an error.
21 f, err := os.Open("/proc/self/mem")
23 t.Fatalf("Opening /proc/self/mem: %s", err)
25 if x, err := ReadAllOrWarn(f); err == nil {
26 t.Fatalf("Expected error, got %v", x)
29 if msg, err := rcv.ReadBytes('\n'); err != nil {
31 } else if matched, err := regexp.MatchString("^crunchstat: .*error.*", string(msg)); err != nil || !matched {
32 t.Fatalf("Expected error message about unreadable file, got \"%s\"", msg)
36 func TestReadAllOrWarnSuccess(t *testing.T) {
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)
50 // Test that CopyPipeToChildLog works even on lines longer than
51 // bufio.MaxScanTokenSize.
52 func TestCopyPipeToChildLogLongLines(t *testing.T) {
56 control := make(chan bool)
57 pipeIn, pipeOut := io.Pipe()
58 go CopyPipeToChildLog(pipeIn, control)
60 sentBytes := make([]byte, bufio.MaxScanTokenSize+MaxLogLine+(1<<22))
62 pipeOut.Write([]byte("before\n"))
64 for i := range sentBytes {
65 // Some bytes that aren't newlines:
66 sentBytes[i] = byte((rand.Int() & 0xff) | 0x80)
68 sentBytes[len(sentBytes)-1] = '\n'
69 pipeOut.Write(sentBytes)
71 pipeOut.Write([]byte("after"))
75 if before, err := rcv.ReadBytes('\n'); err != nil || string(before) != "before\n" {
76 t.Fatalf("\"before\n\" not received (got \"%s\", %s)", before, err)
79 var receivedBytes []byte
82 line, err := rcv.ReadBytes('\n')
86 if len(line) >= 5 && string(line[0:5]) == "[...]" {
87 if receivedBytes == nil {
88 t.Fatal("Beginning of line reported as continuation")
92 if len(line) >= 6 && string(line[len(line)-6:len(line)]) == "[...]\n" {
93 line = line[:len(line)-6]
97 receivedBytes = append(receivedBytes, line...)
99 if bytes.Compare(receivedBytes, sentBytes) != 0 {
100 t.Fatalf("sent %d bytes, got %d different bytes", len(sentBytes), len(receivedBytes))
103 if after, err := rcv.ReadBytes('\n'); err != nil || string(after) != "after\n" {
104 t.Fatal("\"after\n\" not received (got \"%s\", %s)", after, err)
108 case <-time.After(time.Second):
115 func captureLogs() *bufio.Reader {
116 // Send childLog to our bufio reader instead of stderr
117 stderrIn, stderrOut := io.Pipe()
118 childLog = log.New(stderrOut, "", 0)
119 statLog = log.New(stderrOut, "crunchstat: ", 0)
120 return bufio.NewReader(stderrIn)
123 func uncaptureLogs() {
124 childLog = log.New(os.Stderr, "", 0)
125 statLog = log.New(os.Stderr, "crunchstat: ", 0)