-
Notifications
You must be signed in to change notification settings - Fork 0
/
fanout_test.go
85 lines (67 loc) · 1.59 KB
/
fanout_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
package pgxlisten_test
import (
"testing"
"github.com/rnovatorov/pgxlisten"
"github.com/stretchr/testify/suite"
)
type FanoutSuite struct {
suite.Suite
channel channel
fanout *pgxlisten.Fanout
}
func (s *FanoutSuite) SetupTest() {
s.channel = channel{
notifications: make(chan pgxlisten.Notification),
done: make(chan struct{}),
}
s.fanout = pgxlisten.StartFanout(s.channel)
}
func (s *FanoutSuite) TearDownTest() {
s.fanout.Stop()
}
func (s *FanoutSuite) TestOneSub() {
sub := s.fanout.Listen()
defer sub.Unlisten()
s.channel.notifications <- pgxlisten.Notification{Payload: "foo"}
n := <-sub.Notifications()
s.Require().Equal("foo", n.Payload)
select {
case <-sub.Notifications():
s.FailNow("unexpected sub notification")
default:
}
}
func (s *FanoutSuite) TestTwoSubs() {
sub1 := s.fanout.Listen()
defer sub1.Unlisten()
sub2 := s.fanout.Listen()
defer sub2.Unlisten()
s.channel.notifications <- pgxlisten.Notification{Payload: "foo"}
n1 := <-sub1.Notifications()
s.Require().Equal("foo", n1.Payload)
n2 := <-sub2.Notifications()
s.Require().Equal("foo", n2.Payload)
select {
case <-sub1.Notifications():
s.FailNow("unexpected sub1 notification")
default:
}
select {
case <-sub2.Notifications():
s.FailNow("unexpected sub2 notification")
default:
}
}
func TestFanout(t *testing.T) {
suite.Run(t, new(FanoutSuite))
}
type channel struct {
notifications chan pgxlisten.Notification
done chan struct{}
}
func (c channel) Notifications() <-chan pgxlisten.Notification {
return c.notifications
}
func (c channel) Unlisten() {
close(c.done)
}