-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathlivelog_test.go
149 lines (133 loc) · 3.26 KB
/
livelog_test.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package livelog
import (
"bytes"
"io"
"sync"
"testing"
"time"
)
func TestBuffer(t *testing.T) {
var wg sync.WaitGroup
testConc := func(prefix string, r io.Reader, want string, wantErr error) {
wg.Add(1)
go func() {
defer wg.Done()
testRead(t, prefix, r, want, wantErr)
}()
}
sleep := func() { time.Sleep(time.Millisecond) }
const w1, w2, w3, w4 = "first", "second", "third", "fourth"
var buf Buffer
buf.Write([]byte(w1))
r1 := buf.Reader()
testRead(t, "r1, w1", r1, w1, nil)
testConc("r1, w2", r1, w2, nil)
sleep()
buf.Write([]byte(w2))
r2 := buf.Reader()
testRead(t, "r2, w1+w2", r2, w1+w2, nil)
wg.Wait()
testConc("r1, w3", r1, w3, nil)
testConc("r2, w3", r2, w3, nil)
sleep()
buf.Write([]byte(w3))
wg.Wait()
r3 := buf.Reader()
testRead(t, "r3, w1+w2+w3", r3, w1+w2+w3, nil)
testConc("r1, w4", r1, w4, nil)
testConc("r3, eof", r3, "", io.EOF)
sleep()
r3.Close()
buf.Write([]byte(w4))
testRead(t, "r2, w4", r2, w4, nil)
wg.Wait()
testConc("r1 eof", r1, "", io.EOF)
sleep()
buf.Close()
testRead(t, "r2 eof", r2, "", io.EOF)
wg.Wait()
r4 := buf.Reader()
testRead(t, "r4 all", r4, w1+w2+w3+w4, nil)
testRead(t, "r4 eof", r4, "", io.EOF)
}
func testRead(t *testing.T, prefix string, r io.Reader, want string, wantErr error) {
b := make([]byte, 1024)
n, err := r.Read(b)
if err != wantErr {
t.Errorf("%s: got err %v, want %v", prefix, err, wantErr)
return
}
ok := true
if n != len(want) {
t.Errorf("%s: got n = %v, want %v", prefix, n, len(want))
ok = false
}
if s := string(b[:n]); s != want {
t.Errorf("%s: read %q, want %q", prefix, s, want)
ok = false
}
if ok {
t.Logf("%s: ok", prefix)
}
}
func TestTruncation(t *testing.T) {
tests := []struct {
desc string
inputs [][]byte
want []byte
}{
{
desc: "no truncation",
inputs: [][]byte{
bytes.Repeat([]byte{'a'}, maxUserSize),
},
want: bytes.Repeat([]byte{'a'}, maxUserSize),
},
{
desc: "one byte overflow",
inputs: [][]byte{
bytes.Repeat([]byte{'a'}, maxUserSize),
[]byte{'b'},
},
want: append(bytes.Repeat([]byte{'a'}, maxUserSize), []byte(truncationMessage)...),
},
{
desc: "single overflow",
inputs: [][]byte{
bytes.Repeat([]byte{'a'}, 2*MaxBufferSize),
},
want: append(bytes.Repeat([]byte{'a'}, maxUserSize), []byte(truncationMessage)...),
},
{
desc: "multiple overflow",
inputs: [][]byte{
bytes.Repeat([]byte{'a'}, 2*MaxBufferSize),
bytes.Repeat([]byte{'a'}, 2*MaxBufferSize),
},
want: append(bytes.Repeat([]byte{'a'}, maxUserSize), []byte(truncationMessage)...),
},
{
desc: "partial input overflow",
inputs: [][]byte{
bytes.Repeat([]byte{'a'}, maxUserSize-2),
[]byte{'1', '2', '3'},
},
want: append(bytes.Repeat([]byte{'a'}, maxUserSize-2), append([]byte{'1', '2'}, []byte(truncationMessage)...)...),
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
var buf Buffer
for _, input := range test.inputs {
buf.Write(input)
}
got := buf.Bytes()
if !bytes.Equal(got, test.want) {
t.Errorf("buf inputs %v got %v, want %v", test.inputs, got, test.want)
}
})
}
}