-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
replay_test.go
221 lines (163 loc) · 5.56 KB
/
replay_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package sse_test
import (
"fmt"
"strconv"
"testing"
"time"
"github.com/tmaxmax/go-sse"
"github.com/tmaxmax/go-sse/internal/tests"
)
func replay(tb testing.TB, p sse.ReplayProvider, lastEventID sse.EventID, topics ...string) []*sse.Message {
tb.Helper()
if len(topics) == 0 {
topics = []string{sse.DefaultTopic}
}
var replayed []*sse.Message
cb := mockClient(func(m *sse.Message) error {
if m != nil {
replayed = append(replayed, m)
}
return nil
})
sub := sse.Subscription{
Client: cb,
LastEventID: lastEventID,
Topics: topics,
}
_ = p.Replay(sub)
sub.LastEventID = sse.EventID{}
_ = p.Replay(sub)
sub.LastEventID = sse.ID("mama")
_ = p.Replay(sub)
sub.LastEventID = sse.ID("10")
_ = p.Replay(sub)
return replayed
}
func testReplayError(tb testing.TB, p sse.ReplayProvider, tm *tests.Time) {
tb.Helper()
tm.Reset()
tm.Add(time.Hour)
p.Put(msg(tb, "a", "1"), []string{sse.DefaultTopic})
p.Put(msg(tb, "b", "2"), []string{sse.DefaultTopic})
cb := mockClient(func(_ *sse.Message) error { return nil })
tm.Rewind()
err := p.Replay(sse.Subscription{
Client: cb,
LastEventID: sse.ID("1"),
Topics: []string{sse.DefaultTopic},
})
tests.Equal(tb, err, nil, "received invalid error")
}
func TestValidReplayProvider(t *testing.T) {
t.Parallel()
tm := &tests.Time{}
p := &sse.ValidReplayProvider{
TTL: time.Millisecond * 5,
AutoIDs: true,
Now: tm.Now,
GCInterval: -1,
}
tests.Equal(t, p.Replay(sse.Subscription{}), nil, "replay failed on provider without messages")
now := time.Now()
initialNow := now
tm.Set(now)
p.Put(msg(t, "hi", ""), []string{sse.DefaultTopic})
p.Put(msg(t, "there", ""), []string{"t"})
tm.Add(p.TTL)
p.Put(msg(t, "world", ""), []string{sse.DefaultTopic})
p.Put(msg(t, "again", ""), []string{"t"})
tm.Add(p.TTL * 3)
p.Put(msg(t, "world", ""), []string{sse.DefaultTopic})
p.Put(msg(t, "x", ""), []string{"t"})
tm.Add(p.TTL * 5)
p.Put(msg(t, "again", ""), []string{"t"})
tm.Set(initialNow.Add(p.TTL))
p.GC()
tm.Set(now.Add(p.TTL))
replayed := replay(t, p, sse.ID("3"), sse.DefaultTopic, "topic with no messages")[0]
tests.Equal(t, replayed.String(), "id: 4\ndata: world\n\n", "invalid message received")
testReplayError(t, &sse.ValidReplayProvider{Now: tm.Now}, tm)
}
func TestFiniteReplayProvider(t *testing.T) {
t.Parallel()
_, err := sse.NewFiniteReplayProvider(1, false)
if err == nil {
t.Fatal("should not create FiniteReplayProvider with count less than 2")
}
p, err := sse.NewFiniteReplayProvider(3, false)
tests.Equal(t, err, nil, "should create new FiniteReplayProvider")
tests.Equal(t, p.Replay(sse.Subscription{}), nil, "replay failed on provider without messages")
r := tests.Panics(t, func() {
p.Put(&sse.Message{Type: sse.Type("panic")}, []string{sse.DefaultTopic})
}, "messages without IDs cannot be put in a replay provider")
rerr, isErr := r.(error)
tests.Expect(t, isErr, "should panic with error")
tests.Equal(t, rerr.Error(), `go-sse: a Message without an ID was given to a provider that doesn't set IDs automatically.
The message is the following:
│ event: panic
└─■`, "invalid panic error message")
r = tests.Panics(t, func() {
p.Put(&sse.Message{ID: sse.ID("5"), Type: sse.Type("panic")}, nil)
}, "messages cannot be put without a topic")
rerr, isErr = r.(error)
tests.Expect(t, isErr, "should panic with error")
tests.Equal(t, rerr.Error(), `go-sse: no topics provided for Message.
The message is the following:
│ id: 5
│ event: panic
└─■`, "invalid panic error message")
p.Put(msg(t, "", "1"), []string{sse.DefaultTopic})
p.Put(msg(t, "hello", "2"), []string{sse.DefaultTopic})
p.Put(msg(t, "there", "3"), []string{"t"})
p.Put(msg(t, "world", "4"), []string{sse.DefaultTopic})
replayed := replay(t, p, sse.ID("2"))[0]
tests.Equal(t, replayed.String(), "id: 4\ndata: world\n\n", "invalid replayed message")
p.Put(msg(t, "", "5"), []string{"t"})
p.Put(msg(t, "again", "6"), []string{sse.DefaultTopic})
replayed = replay(t, p, sse.ID("4"), sse.DefaultTopic, "topic with no messages")[0]
tests.Equal(t, replayed.String(), "id: 6\ndata: again\n\n", "invalid replayed message")
tr, err := sse.NewFiniteReplayProvider(10, false)
tests.Equal(t, err, nil, "should create new FiniteReplayProvider")
testReplayError(t, tr, nil)
}
func TestFiniteReplayProvider_allocations(t *testing.T) {
p, err := sse.NewFiniteReplayProvider(3, false)
tests.Equal(t, err, nil, "should create new FiniteReplayProvider")
const runs = 100
topics := []string{sse.DefaultTopic}
// Add one to the number of runs to take the warmup run of
// AllocsPerRun() into account.
queue := make([]*sse.Message, runs+1)
lastID := runs
for i := 0; i < len(queue); i++ {
queue[i] = msg(t,
fmt.Sprintf("message %d", i),
strconv.Itoa(i),
)
}
var run int
avgAllocs := testing.AllocsPerRun(runs, func() {
_ = p.Put(queue[run], topics)
run++
})
tests.Equal(t, avgAllocs, 0, "no allocations should be made on Put()")
var replayCount int
cb := mockClient(func(m *sse.Message) error {
if m != nil {
replayCount++
}
return nil
})
sub := sse.Subscription{
Client: cb,
Topics: topics,
}
sub.LastEventID = sse.ID(strconv.Itoa(lastID - 3))
err = p.Replay(sub)
tests.Equal(t, err, nil, "replay from fourth last should succeed")
tests.Equal(t, replayCount, 0, "replay from fourth last should not yield messages")
sub.LastEventID = sse.ID(strconv.Itoa(lastID - 2))
err = p.Replay(sub)
tests.Equal(t, err, nil, "replay from third last should succeed")
tests.Equal(t, replayCount, 2, "replay from third last should yield 2 messages")
}