-
Notifications
You must be signed in to change notification settings - Fork 20
/
producer_test.go
68 lines (58 loc) · 1.25 KB
/
producer_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
package nsq
import (
"fmt"
"strconv"
"testing"
"time"
)
func TestProducer(t *testing.T) {
for _, n := range []int{1, 10, 100, 1000} {
count := n
topic := fmt.Sprintf("test-publisher-%d", n)
t.Run(topic, func(t *testing.T) {
t.Parallel()
c, _ := StartConsumer(ConsumerConfig{
Topic: topic,
Channel: "channel",
Address: "localhost:4150",
})
defer c.Stop()
// Give some time for the consumer to connect.
time.Sleep(100 * time.Millisecond)
p, _ := StartProducer(ProducerConfig{
Address: "localhost:4150",
Topic: topic,
MaxConcurrency: 3,
})
defer p.Stop()
for i := 0; i != count; i++ {
if err := p.Publish([]byte(strconv.Itoa(i))); err != nil {
t.Error(err)
return
}
}
buckets := make([]int, count)
deadline := time.NewTimer(10 * time.Second)
defer deadline.Stop()
for i := 0; i != count; i++ {
select {
case msg := <-c.Messages():
b, err := strconv.Atoi(string(msg.Body))
if err != nil {
t.Error(err)
}
buckets[b]++
msg.Finish()
case <-deadline.C:
t.Error("timeout")
return
}
}
for i, b := range buckets {
if b != 1 {
t.Errorf("bucket at index %d has value %d", i, b)
}
}
})
}
}