-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_test.go
122 lines (91 loc) · 1.88 KB
/
sync_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
package syncbuffer
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
)
func TestSyncBufferOrder(t *testing.T) {
size := 10
sb := NewSyncBuffer(time.Millisecond, size)
for i := 0; i < size; i++ {
sb.Add([]byte{byte(i)})
}
// Streamer should now start from the beginning of the buffer.
r := NewStreamer(sb)
var count int
for p := range r.Stream() {
if count < size {
assert.Equal(t, []byte{byte(count)}, p)
}
count++
if count == size {
sb.Close()
}
}
}
func TestSyncBufferCursor(t *testing.T) {
const size = 10
sb := NewSyncBuffer(time.Millisecond, size)
defer sb.Close()
for i := 0; i < size+1; i++ {
sb.Add([]byte{byte(i)})
}
// Streamer should stream the entire buffer.
s := NewStreamer(sb)
var result int
for p := range s.Stream() {
r := int(p[0])
if r == 10 {
s.Close()
}
result += r
}
assert.Equal(t, (size*(size+1))/2, result)
// Add 5 more
for i := 11; i < 16; i++ {
sb.Add([]byte{byte(i)})
}
// Streamer should only stream the last 10 items.
s = NewStreamer(sb)
result = 0
for p := range s.Stream() {
r := int(p[0])
if r == 15 {
s.Close()
}
result += r
}
assert.Equal(t, 105, result)
}
// TestSyncBuffer_Close mainly checks for goroutine leaks.
func TestSyncBuffer_Close(t *testing.T) {
size := 10
sb := NewSyncBuffer(time.Millisecond, size)
for i := 0; i < size; i++ {
sb.Add([]byte{byte(i)})
}
r := NewStreamer(sb)
sb.Close()
var result int
for range r.Stream() {
result++
}
assert.Equal(t, 0, result)
}
// TestSyncBuffer_Streamer mainly checks for goroutine leaks.
func TestSyncBuffer_Streamer(t *testing.T) {
size := 10
sb := NewSyncBuffer(time.Millisecond, size)
defer sb.Close()
for i := 0; i < size; i++ {
sb.Add([]byte{byte(i)})
}
r := NewStreamer(sb)
r.Close()
for range r.Stream() {
}
}
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}