longganhua
2019-07-18 2fcec5d0debb4819c651e8f3b1287f18de9efee9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package agent
 
import (
    "testing"
)
 
type MockLogHandler struct {
    logs []string
}
 
func (m *MockLogHandler) HandleLog(l string) {
    m.logs = append(m.logs, l)
}
 
func TestLogWriter(t *testing.T) {
    h := &MockLogHandler{}
    w := NewLogWriter(4)
 
    // Write some logs
    w.Write([]byte("one")) // Gets dropped!
    w.Write([]byte("two"))
    w.Write([]byte("three"))
    w.Write([]byte("four"))
    w.Write([]byte("five"))
 
    // Register a handler, sends old!
    w.RegisterHandler(h)
 
    w.Write([]byte("six"))
    w.Write([]byte("seven"))
 
    // Deregister
    w.DeregisterHandler(h)
 
    w.Write([]byte("eight"))
    w.Write([]byte("nine"))
 
    out := []string{
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
    }
    for idx := range out {
        if out[idx] != h.logs[idx] {
            t.Fatalf("mismatch %v", h.logs)
        }
    }
}