-
Notifications
You must be signed in to change notification settings - Fork 19
/
consumer_registries_test.go
69 lines (55 loc) · 1.44 KB
/
consumer_registries_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
package mq
import (
"fmt"
"sync/atomic"
"testing"
)
func TestConsumersRegistry_Get(t *testing.T) {
expectedConsumer := &consumer{}
registry := newConsumersRegistry(1)
registry.Set("name", expectedConsumer)
actualConsumer, ok := registry.Get("name")
if !ok {
t.Error("Expected consumer was not found")
}
if expectedConsumer != actualConsumer {
t.Errorf("Expected and actual consumers are not eqaul")
}
}
func TestConsumersRegistry_Get_NonExistent(t *testing.T) {
registry := newConsumersRegistry(1)
consumer, ok := registry.Get("name")
if ok || consumer != nil {
t.Error("Registry found a non-registered consumer")
}
}
func TestConsumersRegistry_Concurrent(t *testing.T) {
size := 1000
registry := newConsumersRegistry(size)
t.Run("concurrent write", func(t *testing.T) {
t.Parallel()
for i := 0; i < size; i++ {
registry.Set(fmt.Sprintf("name-%d", i), nil)
}
})
t.Run("concurrent read", func(t *testing.T) {
t.Parallel()
for i := 0; i < size; i++ {
registry.Get(fmt.Sprintf("name-%d", i))
}
})
}
func TestConsumersRegistry_GoEach(t *testing.T) {
registry := newConsumersRegistry(1)
registry.Set("1", &consumer{})
registry.Set("2", &consumer{})
registry.Set("3", &consumer{})
registry.Set("4", &consumer{})
var counter int32
registry.GoEach(func(consumer *consumer) {
atomic.AddInt32(&counter, 1)
})
if atomic.LoadInt32(&counter) != 4 {
t.Error("Go each must wait until all functions will finish")
}
}