-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempo_test.go
106 lines (97 loc) · 2.07 KB
/
tempo_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
package tempo
import (
"fmt"
"sync"
"testing"
"time"
)
func printf(s string, a ...interface{}) {
if testing.Verbose() {
fmt.Printf(s, a...)
}
}
func produce(q chan item, numItems, numGoroutines int, out chan []string) {
printf("=== Producing %d items.\n", numItems*numGoroutines)
done := make(chan bool, 1)
msgs := make(chan string)
var wg sync.WaitGroup
go func() {
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(routineIdx int) {
for j := 0; j < numItems; j++ {
m := fmt.Sprintf("producer#%d, item#%d", routineIdx, j)
q <- m
msgs <- m
}
wg.Done()
}(i)
}
wg.Wait()
done <- true
}()
var coll []string
L:
for {
select {
case m := <-msgs:
coll = append(coll, m)
case <-done:
break L
}
}
out <- coll
}
// Test cases:
//
// - Exceeding the batch limit, should trigger an immediate dispatch.
// - No items should be dropped in the dispatch-receive process.
// - When a receiver blocks, the rest of the items should be delivered.
func TestDispatchOrder(t *testing.T) {
d := NewDispatcher(&Config{
Interval: time.Duration(1) * time.Second,
MaxBatchItems: 500,
})
defer d.Stop()
go d.Start()
var (
numItems = 10
numGoroutines = 100
)
produced := make(chan []string, 1)
go produce(d.Q, numItems, numGoroutines, produced)
var dispatched []string
breakout := make(chan bool)
time.AfterFunc(time.Duration(3)*time.Second, func() {
breakout <- true
})
L:
for {
select {
case batch := <-d.Batch:
for _, b := range batch {
m := b.(string)
dispatched = append(dispatched, m)
}
case <-breakout:
break L
}
}
msgs := <-produced
t.Run("no lost items", func(t *testing.T) {
if len(msgs) != len(dispatched) {
t.Error("dispatched messages are missing")
}
})
t.Run("confirm dispatched items", func(t *testing.T) {
msgExists := make(map[string]bool)
for i := 0; i < len(msgs); i++ {
msgExists[msgs[i]] = false
}
for i := 0; i < len(dispatched); i++ {
if _, ok := msgExists[dispatched[i]]; !ok {
t.Errorf("item was not dispatched: %s", dispatched[i])
}
}
})
}