forked from a8m/kinesis-producer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggregator_test.go
64 lines (59 loc) · 1.46 KB
/
aggregator_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
package producer
import (
"math/rand"
"strconv"
"sync"
"testing"
)
func assert(t *testing.T, val bool, msg string) {
if !val {
t.Error(msg)
}
}
func TestSizeAndCount(t *testing.T) {
a := new(Aggregator)
assert(t, a.Size()+a.Count() == 0, "size and count should equal to 0 at the beginning")
data := []byte("hello")
pkey := "world"
n := rand.Intn(100)
for i := 0; i < n; i++ {
a.Put(data, pkey)
}
assert(t, a.Size() == n+5*n+5*n+8*n, "size should equal to size of data, partition-keys, partition key indexes, and protobuf wire type")
assert(t, a.Count() == n, "count should be equal to the number of Put calls")
}
func TestAggregation(t *testing.T) {
var wg sync.WaitGroup
a := new(Aggregator)
n := 50
wg.Add(n)
for i := 0; i < n; i++ {
c := strconv.Itoa(i)
data := []byte("hello-" + c)
a.Put(data, c)
wg.Done()
}
wg.Wait()
record, err := a.Drain()
if err != nil {
t.Error(err)
}
assert(t, isAggregated(record), "should return an agregated record")
records := extractRecords(record)
for i := 0; i < n; i++ {
c := strconv.Itoa(i)
found := false
for _, record := range records {
if string(record.Data) == "hello-"+c {
assert(t, string(record.Data) == "hello-"+c, "`Data` field contains invalid value")
found = true
}
}
assert(t, found, "record not found after extracting: "+c)
}
}
func TestDrainEmptyAggregator(t *testing.T) {
a := new(Aggregator)
_, err := a.Drain()
assert(t, err == nil, "should not return an error")
}