forked from r3labs/sse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_test.go
103 lines (81 loc) · 2.64 KB
/
stream_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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package sse
import (
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
// Tests are accessing subscriber in a non-threadsafe way.
// Maybe fix this in the future so we can test with -race enabled
func TestStream(t *testing.T) {
Convey("Given a new stream", t, func() {
// New Stream
s := newStream(1024, true)
s.run()
Convey("When adding a subscriber", func() {
s.event <- &Event{Data: []byte("test")}
sub := s.addSubscriber("0")
Convey("It should be stored", func() {
So(len(s.subscribers), ShouldEqual, 1)
})
Convey("It should receive messages", func() {
s.event <- &Event{Data: []byte("test")}
msg, err := wait(sub.connection, time.Second*1)
So(err, ShouldBeNil)
So(string(msg), ShouldEqual, "test")
})
Convey("It should receive the eventlog", func() {
So(len(sub.connection), ShouldEqual, 1)
})
})
Convey("When adding a subscriber with auto replay disabled", func() {
s.AutoReplay = false
s.event <- &Event{Data: []byte("test")}
time.Sleep(time.Millisecond * 100)
sub := s.addSubscriber("0")
Convey("It should not receive the eventlog", func() {
So(len(sub.connection), ShouldEqual, 0)
})
})
Convey("When removing a subscriber", func() {
s.addSubscriber("0")
time.Sleep(time.Millisecond * 100)
s.removeSubscriber(0)
Convey("It should be removed from the list of subscribers", func() {
So(len(s.subscribers), ShouldEqual, 0)
})
})
Convey("When closing a subscriber down gracefully", func() {
sub := s.addSubscriber("0")
sub.close()
time.Sleep(time.Millisecond * 100)
Convey("It should be removed from the list of subscribers", func() {
So(len(s.subscribers), ShouldEqual, 0)
})
})
Convey("When adding multiple subscribers", func() {
var subs []*Subscriber
for i := 0; i < 10; i++ {
subs = append(subs, s.addSubscriber("0"))
}
// Wait for all subscribers to be added
time.Sleep(time.Millisecond * 100)
Convey("They should all receive messages", func() {
s.event <- &Event{Data: []byte("test")}
for _, sub := range subs {
msg, err := wait(sub.connection, time.Second*1)
So(err, ShouldBeNil)
So(string(msg), ShouldEqual, "test")
}
})
Convey("They should all shutdown gracefully when the stream is closed", func() {
s.close()
// Wait for all subscribers to close
time.Sleep(time.Millisecond * 100)
So(len(s.subscribers), ShouldEqual, 0)
})
})
})
}