-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcached_test.go
More file actions
180 lines (147 loc) · 4.12 KB
/
Copy pathcached_test.go
File metadata and controls
180 lines (147 loc) · 4.12 KB
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
package sseserver
import (
"bytes"
"errors"
"io"
"log/slog"
"net/http/httptest"
"strconv"
"testing"
"time"
)
var (
_ MultiStream = &CachedStream{}
_ Stream = &CachedStream{}
)
var (
topic = "benchmark_topic"
localCache = newCache(time.Minute, time.Minute)
)
func init() {
slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil)))
for n := 0; n < 10000; n++ {
event := &Event{
ID: strconv.Itoa(n + 1),
}
localCache.add(topic, strconv.Itoa(n), event)
}
}
func BenchmarkLocalCacheAdd(b *testing.B) {
for n := 0; n < b.N; n++ {
localCache.add(topic, strconv.Itoa(n-1), &Event{
ID: strconv.Itoa(n),
})
}
}
func BenchmarkLocalCacheGet(b *testing.B) {
for n := 0; n < b.N; n++ {
events, _ := localCache.get(topic, "0", "5000", 5000, nil)
for range events {
}
}
}
func assertReceivedEvents(t *testing.T, resp *httptest.ResponseRecorder, events ...Event) {
t.Helper()
var buf bytes.Buffer
for _, event := range events {
_ = write(&buf, &event)
}
if buf.String() != resp.Body.String() {
t.Errorf("Expected response body: %q, got: %q", buf.String(), resp.Body.String())
}
}
func TestCachedResync(t *testing.T) {
t.Parallel()
stream := NewCached(Config{
Reconnect: 0,
KeepAlive: 0,
Lifetime: 10 * time.Millisecond,
QueueLength: 32,
ResyncEventsThreshold: 2,
}, "first", time.Minute, time.Minute)
defer stream.Stop()
// Publish two events
event1 := Event{ID: "16"}
stream.Publish(&event1)
event2 := Event{ID: "32"}
stream.Publish(&event2)
w := httptest.NewRecorder()
// connect with initial last event ID to receive both cached events
_ = stream.Subscribe(t.Context(), w, "first")
// Assert both events were received
assertReceivedEvents(t, w, event1, event2)
}
func TestCachedResyncWithBroadcast(t *testing.T) {
t.Parallel()
stream := NewCached(Config{
Reconnect: 0,
KeepAlive: 0,
Lifetime: 10 * time.Millisecond,
QueueLength: 32,
ResyncEventsThreshold: 2,
}, "first", time.Minute, time.Minute)
defer stream.Stop()
// Publish two events, with broadcast in between
event1 := Event{ID: "16"}
stream.Publish(&event1)
stream.PublishBroadcast(&Event{ID: "999"})
event2 := Event{ID: "32"}
stream.Publish(&event2)
w := httptest.NewRecorder()
// connect with initial last event ID to receive both cached events,
// broadcasted event should be excluded
_ = stream.Subscribe(t.Context(), w, "first")
// Assert both events were received
assertReceivedEvents(t, w, event1, event2)
}
func TestCachedError(t *testing.T) {
t.Parallel()
stream := NewCached(Config{
Reconnect: 0,
KeepAlive: 0,
Lifetime: 10 * time.Millisecond,
QueueLength: 32,
}, "8", time.Minute, time.Minute)
defer stream.Stop()
w := httptest.NewRecorder()
// resyncing from non-existent event ID should return error
err := stream.Subscribe(t.Context(), w, "non-existent")
assertReceivedEvents(t, w, ResyncRequiredErrorEvent())
if !errors.Is(err, ErrCacheMiss) {
t.Errorf("Expected error: %v, got: %v", ErrCacheMiss, err)
}
}
func TestCachedResyncTopics(t *testing.T) {
t.Parallel()
stream := NewCachedMultiStream(Config{
Reconnect: 0,
KeepAlive: 0,
Lifetime: 10 * time.Millisecond,
QueueLength: 32,
ResyncEventsThreshold: 5,
}, map[string]string{
"topic1": "first1",
"topic2": "first2",
}, time.Minute, time.Minute)
defer stream.Stop()
// Generate two sub-streams of events
var events1, events2 []Event
for i := 0; i < 5; i++ {
event1 := Event{ID: strconv.Itoa(i * 10)}
events1 = append(events1, event1)
stream.PublishTopic("topic1", &event1)
event2 := Event{ID: strconv.Itoa(i * 20)}
events2 = append(events2, event2)
stream.PublishTopic("topic2", &event2)
}
t.Run("with topic1", func(t *testing.T) {
w := httptest.NewRecorder()
_ = stream.SubscribeTopic(t.Context(), w, "topic1", "first1")
assertReceivedEvents(t, w, events1...)
})
t.Run("with topic2", func(t *testing.T) {
w := httptest.NewRecorder()
_ = stream.SubscribeTopic(t.Context(), w, "topic2", "first2")
assertReceivedEvents(t, w, events2...)
})
}