-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyed_test.go
81 lines (69 loc) · 2.17 KB
/
keyed_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
package keyeddistributor_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/memsql/keyeddistributor"
)
type MyEvent struct {
id int
}
func TestKeyed(t *testing.T) {
distributor := keyeddistributor.New(func(e MyEvent) int {
return e.id % 4
})
t.Log("subscribe to various keys")
r0 := distributor.Subscribe(0)
r1 := distributor.Subscribe(1)
r5 := distributor.Subscribe(5) // should get no events
t.Log("none are ready")
require.False(t, ready(r0.WaitChan()))
require.False(t, ready(r1.WaitChan()))
require.False(t, ready(r5.WaitChan()))
t.Log("submit an event that doesn't match any keys")
s0 := distributor.Submit(MyEvent{id: 2}) // nobody
require.False(t, ready(r0.WaitChan()))
require.False(t, ready(r1.WaitChan()))
require.False(t, ready(r5.WaitChan()))
require.True(t, ready(s0))
t.Log("submit an event that matches one key (0)")
s1 := distributor.Submit(MyEvent{id: 0})
require.True(t, ready(r0.WaitChan()))
require.False(t, ready(r1.WaitChan()))
require.False(t, ready(r5.WaitChan()))
require.False(t, ready(s1))
assert.Equal(t, MyEvent{id: 0}, r0.Consume())
require.False(t, ready(r0.WaitChan()))
require.False(t, ready(r1.WaitChan()))
require.False(t, ready(r5.WaitChan()))
require.True(t, ready(s1))
t.Log("submit an event that matches one key (1)")
s2 := distributor.Submit(MyEvent{id: 1})
require.False(t, ready(r0.WaitChan()))
require.True(t, ready(r1.WaitChan()))
require.False(t, ready(r5.WaitChan()))
require.False(t, ready(s2))
assert.Equal(t, MyEvent{id: 1}, r1.Consume())
require.False(t, ready(r0.WaitChan()))
require.False(t, ready(r1.WaitChan()))
require.False(t, ready(r5.WaitChan()))
require.True(t, ready(s2))
t.Log("note that unsubscribe releases the submission")
s3 := distributor.Submit(MyEvent{id: 1})
require.False(t, ready(r0.WaitChan()))
require.True(t, ready(r1.WaitChan()))
require.False(t, ready(r5.WaitChan()))
require.False(t, ready(s3))
r1.Unsubscribe()
require.False(t, ready(r0.WaitChan()))
require.False(t, ready(r5.WaitChan()))
require.True(t, ready(s3))
}
func ready(c <-chan struct{}) bool {
select {
case <-c:
return true
default:
return false
}
}