| 1 |
package centrifuge |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"strconv" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/centrifugal/protocol" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
) |
| 12 |
|
| 13 |
func testMemoryBroker() *MemoryBroker { |
| 14 |
conf := DefaultConfig |
| 15 |
conf.LogLevel = LogLevelDebug |
| 16 |
conf.LogHandler = func(entry LogEntry) {} |
| 17 |
n, _ := New(conf) |
| 18 |
e, _ := NewMemoryBroker(n, MemoryBrokerConfig{}) |
| 19 |
n.SetBroker(e) |
| 20 |
err := n.Run() |
| 21 |
if err != nil { |
| 22 |
panic(err) |
| 23 |
} |
| 24 |
return e |
| 25 |
} |
| 26 |
|
| 27 |
func newTestPublication() *Publication { |
| 28 |
return &Publication{Data: []byte("{}")} |
| 29 |
} |
| 30 |
|
| 31 |
func testPublicationData() []byte { |
| 32 |
return []byte("{}") |
| 33 |
} |
| 34 |
|
| 35 |
func TestMemoryBrokerPublishHistory(t *testing.T) { |
| 36 |
e := testMemoryBroker() |
| 37 |
|
| 38 |
require.NotEqual(t, nil, e.historyHub) |
| 39 |
|
| 40 |
_, err := e.Publish("channel", testPublicationData(), PublishOptions{}) |
| 41 |
require.NoError(t, err) |
| 42 |
|
| 43 |
err = e.PublishJoin("channel", &ClientInfo{}) |
| 44 |
require.NoError(t, err) |
| 45 |
|
| 46 |
err = e.PublishLeave("channel", &ClientInfo{}) |
| 47 |
require.NoError(t, err) |
| 48 |
|
| 49 |
pub := newTestPublication() |
| 50 |
|
| 51 |
// test adding history. |
| 52 |
_, err = e.Publish("channel", testPublicationData(), PublishOptions{HistorySize: 4, HistoryTTL: time.Second}) |
| 53 |
require.NoError(t, err) |
| 54 |
pubs, _, err := e.History("channel", HistoryFilter{ |
| 55 |
Limit: -1, |
| 56 |
Since: nil, |
| 57 |
}) |
| 58 |
require.NoError(t, err) |
| 59 |
require.Equal(t, 1, len(pubs)) |
| 60 |
require.Equal(t, pubs[0].Data, pub.Data) |
| 61 |
|
| 62 |
// test history limit. |
| 63 |
_, err = e.Publish("channel", testPublicationData(), PublishOptions{HistorySize: 4, HistoryTTL: time.Second}) |
| 64 |
require.NoError(t, err) |
| 65 |
_, err = e.Publish("channel", testPublicationData(), PublishOptions{HistorySize: 4, HistoryTTL: time.Second}) |
| 66 |
require.NoError(t, err) |
| 67 |
_, err = e.Publish("channel", testPublicationData(), PublishOptions{HistorySize: 4, HistoryTTL: time.Second}) |
| 68 |
require.NoError(t, err) |
| 69 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 70 |
Limit: 2, |
| 71 |
Since: nil, |
| 72 |
}) |
| 73 |
require.NoError(t, err) |
| 74 |
require.Equal(t, 2, len(pubs)) |
| 75 |
|
| 76 |
// test history limit greater than history size |
| 77 |
_, err = e.Publish("channel", testPublicationData(), PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 78 |
require.NoError(t, err) |
| 79 |
_, err = e.Publish("channel", testPublicationData(), PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 80 |
require.NoError(t, err) |
| 81 |
_, err = e.Publish("channel", testPublicationData(), PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 82 |
require.NoError(t, err) |
| 83 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 84 |
Limit: 2, |
| 85 |
Since: nil, |
| 86 |
}) |
| 87 |
require.NoError(t, err) |
| 88 |
require.Equal(t, 1, len(pubs)) |
| 89 |
} |
| 90 |
|
| 91 |
func TestMemoryEngineSubscribeUnsubscribe(t *testing.T) { |
| 92 |
e := testMemoryBroker() |
| 93 |
require.NoError(t, e.Subscribe("channel")) |
| 94 |
require.NoError(t, e.Unsubscribe("channel")) |
| 95 |
} |
| 96 |
|
| 97 |
func TestMemoryHistoryHub(t *testing.T) { |
| 98 |
h := newHistoryHub(0) |
| 99 |
h.runCleanups() |
| 100 |
h.RLock() |
| 101 |
require.Equal(t, 0, len(h.streams)) |
| 102 |
h.RUnlock() |
| 103 |
ch1 := "channel1" |
| 104 |
ch2 := "channel2" |
| 105 |
pub := newTestPublication() |
| 106 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 107 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 108 |
_, _ = h.add(ch2, pub, PublishOptions{HistorySize: 2, HistoryTTL: time.Second}) |
| 109 |
_, _ = h.add(ch2, pub, PublishOptions{HistorySize: 2, HistoryTTL: time.Second}) |
| 110 |
|
| 111 |
hist, _, err := h.get(ch1, HistoryFilter{ |
| 112 |
Limit: -1, |
| 113 |
}) |
| 114 |
require.Equal(t, nil, err) |
| 115 |
require.Equal(t, 1, len(hist)) |
| 116 |
hist, _, err = h.get(ch2, HistoryFilter{ |
| 117 |
Limit: -1, |
| 118 |
}) |
| 119 |
require.Equal(t, nil, err) |
| 120 |
require.Equal(t, 2, len(hist)) |
| 121 |
time.Sleep(2 * time.Second) |
| 122 |
|
| 123 |
// test that stream data cleaned up by periodic task |
| 124 |
h.RLock() |
| 125 |
require.Equal(t, 2, len(h.streams)) |
| 126 |
items, top, err := h.streams[ch1].Get(0, -1) |
| 127 |
require.NoError(t, err) |
| 128 |
require.Nil(t, items) |
| 129 |
require.NotZero(t, top) |
| 130 |
items, top, err = h.streams[ch2].Get(0, -1) |
| 131 |
require.NoError(t, err) |
| 132 |
require.Nil(t, items) |
| 133 |
require.NotZero(t, top) |
| 134 |
h.RUnlock() |
| 135 |
hist, _, err = h.get(ch1, HistoryFilter{ |
| 136 |
Limit: -1, |
| 137 |
}) |
| 138 |
require.Equal(t, nil, err) |
| 139 |
require.Equal(t, 0, len(hist)) |
| 140 |
|
| 141 |
// test history messages limit |
| 142 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 10, HistoryTTL: time.Second}) |
| 143 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 10, HistoryTTL: time.Second}) |
| 144 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 10, HistoryTTL: time.Second}) |
| 145 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 10, HistoryTTL: time.Second}) |
| 146 |
hist, _, err = h.get(ch1, HistoryFilter{ |
| 147 |
Limit: -1, |
| 148 |
}) |
| 149 |
require.Equal(t, nil, err) |
| 150 |
require.Equal(t, 4, len(hist)) |
| 151 |
hist, _, err = h.get(ch1, HistoryFilter{ |
| 152 |
Limit: 1, |
| 153 |
}) |
| 154 |
require.Equal(t, nil, err) |
| 155 |
require.Equal(t, 1, len(hist)) |
| 156 |
|
| 157 |
// test history limit greater than history size |
| 158 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 159 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 160 |
hist, _, err = h.get(ch1, HistoryFilter{ |
| 161 |
Limit: 2, |
| 162 |
}) |
| 163 |
require.Equal(t, nil, err) |
| 164 |
require.Equal(t, 1, len(hist)) |
| 165 |
} |
| 166 |
|
| 167 |
func TestMemoryHistoryHubMetaTTL(t *testing.T) { |
| 168 |
h := newHistoryHub(1 * time.Second) |
| 169 |
h.runCleanups() |
| 170 |
|
| 171 |
ch1 := "channel1" |
| 172 |
ch2 := "channel2" |
| 173 |
pub := newTestPublication() |
| 174 |
h.RLock() |
| 175 |
require.Equal(t, int64(0), h.nextRemoveCheck) |
| 176 |
h.RUnlock() |
| 177 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 178 |
_, _ = h.add(ch1, pub, PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 179 |
_, _ = h.add(ch2, pub, PublishOptions{HistorySize: 2, HistoryTTL: time.Second}) |
| 180 |
_, _ = h.add(ch2, pub, PublishOptions{HistorySize: 2, HistoryTTL: time.Second}) |
| 181 |
h.RLock() |
| 182 |
require.True(t, h.nextRemoveCheck > 0) |
| 183 |
require.Equal(t, 2, len(h.streams)) |
| 184 |
h.RUnlock() |
| 185 |
pubs, _, err := h.get(ch1, HistoryFilter{Limit: -1}) |
| 186 |
require.NoError(t, err) |
| 187 |
require.Len(t, pubs, 1) |
| 188 |
pubs, _, err = h.get(ch2, HistoryFilter{Limit: -1}) |
| 189 |
require.NoError(t, err) |
| 190 |
require.Len(t, pubs, 2) |
| 191 |
|
| 192 |
time.Sleep(2 * time.Second) |
| 193 |
|
| 194 |
// test that stream cleaned up by periodic task |
| 195 |
h.RLock() |
| 196 |
require.Equal(t, 0, len(h.streams)) |
| 197 |
require.Equal(t, int64(0), h.nextRemoveCheck) |
| 198 |
h.RUnlock() |
| 199 |
} |
| 200 |
|
| 201 |
func TestMemoryBrokerRecover(t *testing.T) { |
| 202 |
e := testMemoryBroker() |
| 203 |
|
| 204 |
for i := 0; i < 5; i++ { |
| 205 |
_, err := e.Publish("channel", testPublicationData(), PublishOptions{HistorySize: 10, HistoryTTL: 2 * time.Second}) |
| 206 |
require.NoError(t, err) |
| 207 |
} |
| 208 |
|
| 209 |
_, streamTop, err := e.History("channel", HistoryFilter{ |
| 210 |
Limit: 0, |
| 211 |
Since: nil, |
| 212 |
}) |
| 213 |
require.NoError(t, err) |
| 214 |
|
| 215 |
pubs, _, err := e.History("channel", HistoryFilter{ |
| 216 |
Limit: -1, |
| 217 |
Since: &StreamPosition{Offset: 2, Epoch: streamTop.Epoch}, |
| 218 |
}) |
| 219 |
require.NoError(t, err) |
| 220 |
require.Equal(t, 3, len(pubs)) |
| 221 |
require.Equal(t, uint64(3), pubs[0].Offset) |
| 222 |
require.Equal(t, uint64(4), pubs[1].Offset) |
| 223 |
require.Equal(t, uint64(5), pubs[2].Offset) |
| 224 |
|
| 225 |
for i := 0; i < 10; i++ { |
| 226 |
_, err := e.Publish("channel", testPublicationData(), PublishOptions{HistorySize: 10, HistoryTTL: 2 * time.Second}) |
| 227 |
require.NoError(t, err) |
| 228 |
} |
| 229 |
|
| 230 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 231 |
Limit: -1, |
| 232 |
Since: &StreamPosition{Offset: 0, Epoch: streamTop.Epoch}, |
| 233 |
}) |
| 234 |
require.NoError(t, err) |
| 235 |
require.Equal(t, 10, len(pubs)) |
| 236 |
|
| 237 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 238 |
Limit: -1, |
| 239 |
Since: &StreamPosition{Offset: 100, Epoch: streamTop.Epoch}, |
| 240 |
}) |
| 241 |
require.NoError(t, err) |
| 242 |
require.Equal(t, 0, len(pubs)) |
| 243 |
|
| 244 |
require.NoError(t, e.RemoveHistory("channel")) |
| 245 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 246 |
Limit: -1, |
| 247 |
Since: &StreamPosition{Offset: 2, Epoch: streamTop.Epoch}, |
| 248 |
}) |
| 249 |
require.NoError(t, err) |
| 250 |
require.Equal(t, 0, len(pubs)) |
| 251 |
} |
| 252 |
|
| 253 |
func BenchmarkMemoryPublish_OneChannel(b *testing.B) { |
| 254 |
e := testMemoryBroker() |
| 255 |
rawData := protocol.Raw(`{"bench": true}`) |
| 256 |
b.ResetTimer() |
| 257 |
for i := 0; i < b.N; i++ { |
| 258 |
_, err := e.Publish("channel", rawData, PublishOptions{}) |
| 259 |
if err != nil { |
| 260 |
b.Fatal(err) |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func BenchmarkMemoryPublish_OneChannel_Parallel(b *testing.B) { |
| 266 |
e := testMemoryBroker() |
| 267 |
rawData := protocol.Raw(`{"bench": true}`) |
| 268 |
b.SetParallelism(128) |
| 269 |
b.ResetTimer() |
| 270 |
b.RunParallel(func(pb *testing.PB) { |
| 271 |
for pb.Next() { |
| 272 |
_, err := e.Publish("channel", rawData, PublishOptions{}) |
| 273 |
if err != nil { |
| 274 |
b.Fatal(err) |
| 275 |
} |
| 276 |
} |
| 277 |
}) |
| 278 |
} |
| 279 |
|
| 280 |
func BenchmarkMemoryPublish_History_OneChannel(b *testing.B) { |
| 281 |
e := testMemoryBroker() |
| 282 |
rawData := protocol.Raw(`{"bench": true}`) |
| 283 |
b.ResetTimer() |
| 284 |
for i := 0; i < b.N; i++ { |
| 285 |
chOpts := PublishOptions{HistorySize: 100, HistoryTTL: 60 * time.Second} |
| 286 |
var err error |
| 287 |
streamTop, err := e.Publish("channel", rawData, chOpts) |
| 288 |
if err != nil { |
| 289 |
b.Fatal(err) |
| 290 |
} |
| 291 |
if streamTop.Offset == 0 { |
| 292 |
b.Fatal("zero offset") |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
func BenchmarkMemoryPublish_History_OneChannel_Parallel(b *testing.B) { |
| 298 |
e := testMemoryBroker() |
| 299 |
rawData := protocol.Raw(`{"bench": true}`) |
| 300 |
chOpts := PublishOptions{HistorySize: 100, HistoryTTL: 60 * time.Second} |
| 301 |
b.SetParallelism(128) |
| 302 |
b.ResetTimer() |
| 303 |
b.RunParallel(func(pb *testing.PB) { |
| 304 |
for pb.Next() { |
| 305 |
var err error |
| 306 |
streamTop, err := e.Publish("channel", rawData, chOpts) |
| 307 |
if err != nil { |
| 308 |
b.Fatal(err) |
| 309 |
} |
| 310 |
if streamTop.Offset == 0 { |
| 311 |
b.Fatal("zero offset") |
| 312 |
} |
| 313 |
} |
| 314 |
}) |
| 315 |
} |
| 316 |
|
| 317 |
func BenchmarkMemoryHistory_OneChannel(b *testing.B) { |
| 318 |
e := testMemoryBroker() |
| 319 |
rawData := protocol.Raw("{}") |
| 320 |
for i := 0; i < 4; i++ { |
| 321 |
_, _ = e.Publish("channel", rawData, PublishOptions{HistorySize: 4, HistoryTTL: 300 * time.Second}) |
| 322 |
} |
| 323 |
b.ResetTimer() |
| 324 |
for i := 0; i < b.N; i++ { |
| 325 |
_, _, err := e.History("channel", HistoryFilter{ |
| 326 |
Limit: -1, |
| 327 |
Since: nil, |
| 328 |
}) |
| 329 |
if err != nil { |
| 330 |
b.Fatal(err) |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
func BenchmarkMemoryHistory_OneChannel_Parallel(b *testing.B) { |
| 336 |
e := testMemoryBroker() |
| 337 |
rawData := protocol.Raw("{}") |
| 338 |
for i := 0; i < 4; i++ { |
| 339 |
_, _ = e.Publish("channel", rawData, PublishOptions{HistorySize: 4, HistoryTTL: 300 * time.Second}) |
| 340 |
} |
| 341 |
b.ResetTimer() |
| 342 |
b.RunParallel(func(pb *testing.PB) { |
| 343 |
for pb.Next() { |
| 344 |
_, _, err := e.History("channel", HistoryFilter{ |
| 345 |
Limit: -1, |
| 346 |
Since: nil, |
| 347 |
}) |
| 348 |
if err != nil { |
| 349 |
b.Fatal(err) |
| 350 |
} |
| 351 |
} |
| 352 |
}) |
| 353 |
} |
| 354 |
|
| 355 |
func BenchmarkMemoryRecover_OneChannel_Parallel(b *testing.B) { |
| 356 |
e := testMemoryBroker() |
| 357 |
rawData := protocol.Raw("{}") |
| 358 |
numMessages := 1000 |
| 359 |
numMissing := 5 |
| 360 |
for i := 1; i <= numMessages; i++ { |
| 361 |
_, _ = e.Publish("channel", rawData, PublishOptions{HistorySize: numMessages, HistoryTTL: 300 * time.Second}) |
| 362 |
} |
| 363 |
b.ResetTimer() |
| 364 |
b.RunParallel(func(pb *testing.PB) { |
| 365 |
for pb.Next() { |
| 366 |
pubs, _, err := e.History("channel", HistoryFilter{ |
| 367 |
Limit: -1, |
| 368 |
Since: &StreamPosition{Offset: uint64(numMessages - numMissing), Epoch: ""}, |
| 369 |
}) |
| 370 |
if err != nil { |
| 371 |
b.Fatal(err) |
| 372 |
} |
| 373 |
if len(pubs) != numMissing { |
| 374 |
b.Fail() |
| 375 |
} |
| 376 |
} |
| 377 |
}) |
| 378 |
} |
| 379 |
|
| 380 |
type recoverTest struct { |
| 381 |
Name string |
| 382 |
HistorySize int |
| 383 |
HistoryLifetime int |
| 384 |
NumPublications int |
| 385 |
SinceOffset uint64 |
| 386 |
NumRecovered int |
| 387 |
Sleep int |
| 388 |
Limit int |
| 389 |
Recovered bool |
| 390 |
} |
| 391 |
|
| 392 |
var recoverTests = []recoverTest{ |
| 393 |
{"empty_stream", 10, 60, 0, 0, 0, 0, 0, true}, |
| 394 |
{"from_position", 10, 60, 10, 8, 2, 0, 0, true}, |
| 395 |
{"from_position_limited", 10, 60, 10, 5, 2, 0, 2, false}, |
| 396 |
{"from_position_with_server_limit", 10, 60, 10, 5, 1, 0, 1, false}, |
| 397 |
{"from_position_that_already_gone", 10, 60, 20, 8, 10, 0, 0, false}, |
| 398 |
{"from_position_that_not_exist_yet", 10, 60, 20, 108, 0, 0, 0, false}, |
| 399 |
{"same_position_no_pubs_expected", 10, 60, 7, 7, 0, 0, 0, true}, |
| 400 |
{"empty_position_recover_expected", 10, 60, 4, 0, 4, 0, 0, true}, |
| 401 |
{"from_position_in_expired_stream", 10, 1, 10, 8, 0, 3, 0, false}, |
| 402 |
{"from_same_position_in_expired_stream", 10, 1, 1, 1, 0, 3, 0, true}, |
| 403 |
} |
| 404 |
|
| 405 |
type recoverTestChannel struct { |
| 406 |
ChannelPrefix string |
| 407 |
IsSeq bool |
| 408 |
} |
| 409 |
|
| 410 |
var recoverTestChannels = []recoverTestChannel{ |
| 411 |
{"test_recovery_memory_offset_", false}, |
| 412 |
{"test_recovery_memory_seq_", true}, |
| 413 |
} |
| 414 |
|
| 415 |
func TestMemoryClientSubscribeRecover(t *testing.T) { |
| 416 |
for _, tt := range recoverTests { |
| 417 |
t.Run(tt.Name, func(t *testing.T) { |
| 418 |
node := defaultNodeNoHandlers() |
| 419 |
node.config.RecoveryMaxPublicationLimit = tt.Limit |
| 420 |
node.OnConnect(func(client *Client) { |
| 421 |
client.OnSubscribe(func(event SubscribeEvent, cb SubscribeCallback) { |
| 422 |
opts := SubscribeOptions{Recover: true} |
| 423 |
cb(SubscribeReply{Options: opts}, nil) |
| 424 |
}) |
| 425 |
}) |
| 426 |
|
| 427 |
for _, recoverTestChannel := range recoverTestChannels { |
| 428 |
channel := recoverTestChannel.ChannelPrefix + tt.Name |
| 429 |
|
| 430 |
client := newTestClient(t, node, "42") |
| 431 |
|
| 432 |
for i := 1; i <= tt.NumPublications; i++ { |
| 433 |
_, _ = node.Publish(channel, []byte(`{"n": `+strconv.Itoa(i)+`}`), WithHistory(tt.HistorySize, time.Duration(tt.HistoryLifetime)*time.Second)) |
| 434 |
} |
| 435 |
|
| 436 |
time.Sleep(time.Duration(tt.Sleep) * time.Second) |
| 437 |
|
| 438 |
connectClient(t, client) |
| 439 |
|
| 440 |
_, streamTop, err := node.broker.History(channel, HistoryFilter{ |
| 441 |
Limit: 0, |
| 442 |
Since: nil, |
| 443 |
}) |
| 444 |
require.NoError(t, err) |
| 445 |
|
| 446 |
subscribeCmd := &protocol.SubscribeRequest{ |
| 447 |
Channel: channel, |
| 448 |
Recover: true, |
| 449 |
Epoch: streamTop.Epoch, |
| 450 |
} |
| 451 |
if recoverTestChannel.IsSeq { |
| 452 |
subscribeCmd.Seq = uint32(tt.SinceOffset) |
| 453 |
} else { |
| 454 |
subscribeCmd.Offset = tt.SinceOffset |
| 455 |
} |
| 456 |
|
| 457 |
rwWrapper := testReplyWriterWrapper() |
| 458 |
|
| 459 |
disconnect := client.handleSubscribe(getJSONEncodedParams(t, subscribeCmd), rwWrapper.rw) |
| 460 |
require.Nil(t, disconnect) |
| 461 |
require.Nil(t, rwWrapper.replies[0].Error) |
| 462 |
res := extractSubscribeResult(rwWrapper.replies, client.Transport().Protocol()) |
| 463 |
require.Equal(t, tt.NumRecovered, len(res.Publications)) |
| 464 |
require.Equal(t, tt.Recovered, res.Recovered) |
| 465 |
if len(res.Publications) > 1 { |
| 466 |
require.True(t, res.Publications[0].Offset < res.Publications[1].Offset) |
| 467 |
} |
| 468 |
} |
| 469 |
_ = node.Shutdown(context.Background()) |
| 470 |
}) |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
const historyIterationChannel = "test" |
| 475 |
|
| 476 |
type historyIterationTest struct { |
| 477 |
NumMessages int |
| 478 |
IterateBy int |
| 479 |
} |
| 480 |
|
| 481 |
func (it *historyIterationTest) prepareHistoryIteration(t testing.TB, node *Node) StreamPosition { |
| 482 |
numMessages := it.NumMessages |
| 483 |
|
| 484 |
channel := historyIterationChannel |
| 485 |
|
| 486 |
historyResult, err := node.History(channel) |
| 487 |
require.NoError(t, err) |
| 488 |
startPosition := historyResult.StreamPosition |
| 489 |
|
| 490 |
for i := 1; i <= numMessages; i++ { |
| 491 |
_, err := node.Publish(channel, []byte(`{}`), WithHistory(numMessages, time.Minute)) |
| 492 |
require.NoError(t, err) |
| 493 |
} |
| 494 |
|
| 495 |
historyResult, err = node.History(channel, WithLimit(NoLimit)) |
| 496 |
require.NoError(t, err) |
| 497 |
require.Equal(t, numMessages, len(historyResult.Publications)) |
| 498 |
return startPosition |
| 499 |
} |
| 500 |
|
| 501 |
func (it *historyIterationTest) testHistoryIteration(t testing.TB, node *Node, startPosition StreamPosition) { |
| 502 |
var ( |
| 503 |
n int |
| 504 |
offset = startPosition.Offset |
| 505 |
epoch = startPosition.Epoch |
| 506 |
iterateBy = it.IterateBy |
| 507 |
) |
| 508 |
for { |
| 509 |
res, err := node.History( |
| 510 |
historyIterationChannel, |
| 511 |
Since(&StreamPosition{Offset: offset, Epoch: epoch}), |
| 512 |
WithLimit(iterateBy), |
| 513 |
) |
| 514 |
if err != nil { |
| 515 |
t.Fatal(err) |
| 516 |
} |
| 517 |
offset += uint64(iterateBy) |
| 518 |
if len(res.Publications) == 0 { |
| 519 |
break |
| 520 |
} |
| 521 |
n += len(res.Publications) |
| 522 |
} |
| 523 |
if n != it.NumMessages { |
| 524 |
t.Fail() |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
func TestMemoryBrokerHistoryIteration(t *testing.T) { |
| 529 |
e := testMemoryBroker() |
| 530 |
it := historyIterationTest{10000, 100} |
| 531 |
startPosition := it.prepareHistoryIteration(t, e.node) |
| 532 |
it.testHistoryIteration(t, e.node, startPosition) |
| 533 |
} |
| 534 |
|
| 535 |
func BenchmarkMemoryBrokerHistoryIteration(b *testing.B) { |
| 536 |
e := testMemoryBroker() |
| 537 |
it := historyIterationTest{10000, 100} |
| 538 |
startPosition := it.prepareHistoryIteration(b, e.node) |
| 539 |
b.ResetTimer() |
| 540 |
for i := 0; i < b.N; i++ { |
| 541 |
it.testHistoryIteration(b, e.node, startPosition) |
| 542 |
} |
| 543 |
} |