-
Notifications
You must be signed in to change notification settings - Fork 4
/
batch_test.go
113 lines (79 loc) · 2.26 KB
/
batch_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
package cloudwatch
import (
"testing"
"time"
. "gopkg.in/check.v1"
)
func TestBatch(t *testing.T) {
TestingT(t)
}
type BatchSuite struct {
in chan Log
out chan []Log
}
var _ = Suite(&BatchSuite{})
func (s *BatchSuite) SetUpTest(c *C) {
s.in = make(chan Log)
s.out = make(chan []Log)
}
// TODO: Test closing channel.
func (s *BatchSuite) TestBatcherWhenNotFull(c *C) {
batcher := NewBatcher(s.in, s.out, Capacity{Length: 2, Size: 2})
go batcher.Start()
s.in <- &FakeLog{size: 0}
c.Assert(batcher.Length(), Equals, 1)
}
func (s *BatchSuite) TestFlushWhenCountReached(c *C) {
batcher := NewBatcher(s.in, s.out, Capacity{Length: 2, Size: 4})
go batcher.Start()
s.in <- &FakeLog{size: 1}
s.in <- &FakeLog{size: 1}
messages := <-s.out
c.Assert(messages, HasLen, 2)
c.Assert(batcher.Length(), Equals, 0)
}
func (s *BatchSuite) TestFlushWhenSizeReached(c *C) {
batcher := NewBatcher(s.in, s.out, Capacity{Length: 4, Size: 2})
go batcher.Start()
s.in <- &FakeLog{size: 1}
s.in <- &FakeLog{size: 1}
messages := <-s.out
c.Assert(messages, HasLen, 2)
c.Assert(batcher.Length(), Equals, 0)
}
func (s *BatchSuite) TestFlushWhenSizeExceeded(c *C) {
batcher := NewBatcher(s.in, s.out, Capacity{Length: 4, Size: 3})
go batcher.Start()
s.in <- &FakeLog{size: 2}
s.in <- &FakeLog{size: 2}
messages := <-s.out
c.Assert(messages, HasLen, 1)
c.Assert(batcher.Length(), Equals, 1)
}
func (s *BatchSuite) TestFlushOversizedMessage(c *C) {
batcher := NewBatcher(s.in, s.out, Capacity{Length: 4, Size: 1})
go batcher.Start()
s.in <- &FakeLog{size: 2}
messages := <-s.out
c.Assert(messages, HasLen, 1)
c.Assert(batcher.Length(), Equals, 0)
}
func (s *BatchSuite) TestFlushWhenDurationExceeded(c *C) {
duration := 1 * time.Millisecond
batcher := NewBatcher(s.in, s.out, Capacity{Length: 2, Size: 2, Duration: duration})
go batcher.Start()
s.in <- &FakeLog{size: 1}
time.Sleep(2 * time.Millisecond)
messages := <-s.out
c.Assert(messages, HasLen, 1)
c.Assert(batcher.Length(), Equals, 0)
}
func (s *BatchSuite) TestFlushWhenClosed(c *C) {
batcher := NewBatcher(s.in, s.out, Capacity{Length: 2, Size: 2})
go batcher.Start()
s.in <- &FakeLog{size: 0}
close(s.in)
messages := <-s.out
c.Assert(messages, HasLen, 1)
c.Assert(batcher.Length(), Equals, 0)
}