-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathregistry_test.go
122 lines (80 loc) · 1.68 KB
/
registry_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package vega
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRegistryPoll(t *testing.T) {
r := NewMemRegistry()
v, _ := r.Poll("a")
assert.Nil(t, v)
msg := Msg([]byte("hello"))
r.Declare("a")
r.Push("a", msg)
ret, _ := r.Poll("a")
assert.NotNil(t, ret)
assert.True(t, msg.Equal(ret.Message))
}
func TestLongPollRegistry(t *testing.T) {
r := NewMemRegistry()
msg := Msg([]byte("hello"))
r.Declare("a")
var got *Delivery
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
dur, _ := time.ParseDuration("2s")
got, _ = r.LongPoll("a", dur)
}()
time.Sleep(500 * time.Millisecond)
r.Push("a", msg)
wg.Wait()
assert.NotNil(t, got)
assert.True(t, msg.Equal(got.Message))
}
func TestLongPollRegistryTimeout(t *testing.T) {
r := NewMemRegistry()
r.Declare("a")
var got *Delivery
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
dur, _ := time.ParseDuration("1s")
got, _ = r.LongPoll("a", dur)
}()
wg.Wait()
}
func TestLongPollRegistryIsCancelable(t *testing.T) {
r := NewMemRegistry()
msg := Msg([]byte("hello"))
r.Declare("a")
done := make(chan struct{})
var wg sync.WaitGroup
fin := make(chan *Delivery)
wg.Add(1)
go func() {
defer wg.Done()
dur, _ := time.ParseDuration("2s")
got, _ := r.LongPollCancelable("a", dur, done)
fin <- got
}()
time.Sleep(100 * time.Millisecond)
close(done)
r.Push("a", msg)
select {
case got := <-fin:
assert.Nil(t, got)
case <-time.Tick(1 * time.Second):
t.Fatal("long poll did not cancel")
}
wg.Wait()
try, err := r.Poll("a")
if err != nil {
panic(err)
}
assert.NotNil(t, try)
assert.True(t, msg.Equal(try.Message))
}