| 1 |
// +build integration |
| 2 |
|
| 3 |
package centrifuge |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"fmt" |
| 8 |
"math/rand" |
| 9 |
"strconv" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/centrifugal/protocol" |
| 15 |
"github.com/gomodule/redigo/redis" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
testRedisAddress = "127.0.0.1:6379" |
| 21 |
testRedisPassword = "" |
| 22 |
testRedisDB = 9 |
| 23 |
) |
| 24 |
|
| 25 |
func getUniquePrefix() string { |
| 26 |
return "centrifuge-test-" + randString(3) + "-" + strconv.FormatInt(time.Now().UnixNano(), 10) |
| 27 |
} |
| 28 |
|
| 29 |
func newTestRedisBroker(tb testing.TB, n *Node, useStreams bool, useCluster bool) *RedisBroker { |
| 30 |
if useCluster { |
| 31 |
return NewTestRedisBrokerClusterWithPrefix(tb, n, getUniquePrefix(), useStreams) |
| 32 |
} |
| 33 |
return NewTestRedisBrokerWithPrefix(tb, n, getUniquePrefix(), useStreams) |
| 34 |
} |
| 35 |
|
| 36 |
func testNode(tb testing.TB) *Node { |
| 37 |
conf := DefaultConfig |
| 38 |
conf.LogLevel = LogLevelDebug |
| 39 |
conf.LogHandler = func(entry LogEntry) {} |
| 40 |
node, err := New(conf) |
| 41 |
require.NoError(tb, err) |
| 42 |
return node |
| 43 |
} |
| 44 |
|
| 45 |
func testRedisConf() RedisShardConfig { |
| 46 |
return RedisShardConfig{ |
| 47 |
Address: testRedisAddress, |
| 48 |
DB: testRedisDB, |
| 49 |
Password: testRedisPassword, |
| 50 |
ReadTimeout: 100 * time.Second, |
| 51 |
ConnectTimeout: 10 * time.Second, |
| 52 |
WriteTimeout: 10 * time.Second, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func NewTestRedisBrokerWithPrefix(tb testing.TB, n *Node, prefix string, useStreams bool) *RedisBroker { |
| 57 |
redisConf := testRedisConf() |
| 58 |
s, err := NewRedisShard(n, redisConf) |
| 59 |
require.NoError(tb, err) |
| 60 |
e, err := NewRedisBroker(n, RedisBrokerConfig{ |
| 61 |
Prefix: prefix, |
| 62 |
UseStreams: useStreams, |
| 63 |
HistoryMetaTTL: 300 * time.Second, |
| 64 |
Shards: []*RedisShard{s}, |
| 65 |
}) |
| 66 |
if err != nil { |
| 67 |
tb.Fatal(err) |
| 68 |
} |
| 69 |
n.SetBroker(e) |
| 70 |
err = n.Run() |
| 71 |
if err != nil { |
| 72 |
tb.Fatal(err) |
| 73 |
} |
| 74 |
return e |
| 75 |
} |
| 76 |
|
| 77 |
func NewTestRedisBrokerClusterWithPrefix(tb testing.TB, n *Node, prefix string, useStreams bool) *RedisBroker { |
| 78 |
redisConf := RedisShardConfig{ |
| 79 |
ClusterAddresses: []string{"localhost:7000", "localhost:7001", "localhost:7002"}, |
| 80 |
Password: testRedisPassword, |
| 81 |
ReadTimeout: 100 * time.Second, |
| 82 |
} |
| 83 |
s, err := NewRedisShard(n, redisConf) |
| 84 |
require.NoError(tb, err) |
| 85 |
e, err := NewRedisBroker(n, RedisBrokerConfig{ |
| 86 |
Prefix: prefix, |
| 87 |
UseStreams: useStreams, |
| 88 |
HistoryMetaTTL: 300 * time.Second, |
| 89 |
Shards: []*RedisShard{s}, |
| 90 |
}) |
| 91 |
if err != nil { |
| 92 |
tb.Fatal(err) |
| 93 |
} |
| 94 |
n.SetBroker(e) |
| 95 |
err = n.Run() |
| 96 |
if err != nil { |
| 97 |
tb.Fatal(err) |
| 98 |
} |
| 99 |
return e |
| 100 |
} |
| 101 |
|
| 102 |
func NewTestRedisBrokerSentinel(tb testing.TB) *RedisBroker { |
| 103 |
n, _ := New(Config{}) |
| 104 |
redisConf := RedisShardConfig{ |
| 105 |
SentinelAddresses: []string{"127.0.0.1:26379"}, |
| 106 |
SentinelMasterName: "mymaster", |
| 107 |
ReadTimeout: 100 * time.Second, |
| 108 |
} |
| 109 |
s, err := NewRedisShard(n, redisConf) |
| 110 |
require.NoError(tb, err) |
| 111 |
e, err := NewRedisBroker(n, RedisBrokerConfig{ |
| 112 |
Shards: []*RedisShard{s}, |
| 113 |
}) |
| 114 |
if err != nil { |
| 115 |
tb.Fatal(err) |
| 116 |
} |
| 117 |
n.SetBroker(e) |
| 118 |
err = n.Run() |
| 119 |
if err != nil { |
| 120 |
tb.Fatal(err) |
| 121 |
} |
| 122 |
return e |
| 123 |
} |
| 124 |
|
| 125 |
func TestRedisBroker_NoShards(t *testing.T) { |
| 126 |
n, _ := New(Config{}) |
| 127 |
_, err := NewRedisBroker(n, RedisBrokerConfig{}) |
| 128 |
require.Error(t, err) |
| 129 |
} |
| 130 |
|
| 131 |
func TestRedisBrokerSentinel(t *testing.T) { |
| 132 |
e := NewTestRedisBrokerSentinel(t) |
| 133 |
_, _, err := e.History("test", HistoryFilter{ |
| 134 |
Limit: -1, |
| 135 |
}) |
| 136 |
require.NoError(t, err) |
| 137 |
} |
| 138 |
|
| 139 |
var redisTests = []struct { |
| 140 |
Name string |
| 141 |
UseStreams bool |
| 142 |
UseCluster bool |
| 143 |
}{ |
| 144 |
{"lists", false, false}, |
| 145 |
{"streams", true, false}, |
| 146 |
{"lists_cluster", false, true}, |
| 147 |
{"streams_cluster", true, true}, |
| 148 |
} |
| 149 |
|
| 150 |
var benchRedisTests = []struct { |
| 151 |
Name string |
| 152 |
UseStreams bool |
| 153 |
}{ |
| 154 |
{"lists", false}, |
| 155 |
{"streams", true}, |
| 156 |
} |
| 157 |
|
| 158 |
func TestRedisBroker(t *testing.T) { |
| 159 |
for _, tt := range redisTests { |
| 160 |
t.Run(tt.Name, func(t *testing.T) { |
| 161 |
node := testNode(t) |
| 162 |
e := newTestRedisBroker(t, node, tt.UseStreams, tt.UseCluster) |
| 163 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 164 |
|
| 165 |
_, err := e.Publish("channel", testPublicationData(), PublishOptions{}) |
| 166 |
require.NoError(t, err) |
| 167 |
_, err = e.Publish("channel", testPublicationData(), PublishOptions{}) |
| 168 |
require.NoError(t, err) |
| 169 |
require.NoError(t, e.Subscribe("channel")) |
| 170 |
require.NoError(t, e.Unsubscribe("channel")) |
| 171 |
|
| 172 |
rawData := []byte("{}") |
| 173 |
|
| 174 |
// test adding history |
| 175 |
_, err = e.Publish("channel", rawData, PublishOptions{HistorySize: 4, HistoryTTL: time.Second}) |
| 176 |
require.NoError(t, err) |
| 177 |
pubs, _, err := e.History("channel", HistoryFilter{ |
| 178 |
Limit: -1, |
| 179 |
}) |
| 180 |
require.NoError(t, err) |
| 181 |
require.Equal(t, 1, len(pubs)) |
| 182 |
require.Equal(t, pubs[0].Data, []byte("{}")) |
| 183 |
|
| 184 |
// test history limit |
| 185 |
_, err = e.Publish("channel", rawData, PublishOptions{HistorySize: 4, HistoryTTL: time.Second}) |
| 186 |
require.NoError(t, err) |
| 187 |
_, err = e.Publish("channel", rawData, PublishOptions{HistorySize: 4, HistoryTTL: time.Second}) |
| 188 |
require.NoError(t, err) |
| 189 |
_, err = e.Publish("channel", rawData, PublishOptions{HistorySize: 4, HistoryTTL: time.Second}) |
| 190 |
require.NoError(t, err) |
| 191 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 192 |
Limit: 2, |
| 193 |
}) |
| 194 |
require.NoError(t, err) |
| 195 |
require.Equal(t, 2, len(pubs)) |
| 196 |
|
| 197 |
// test history limit greater than history size |
| 198 |
_, err = e.Publish("channel", rawData, PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 199 |
require.NoError(t, err) |
| 200 |
_, err = e.Publish("channel", rawData, PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 201 |
require.NoError(t, err) |
| 202 |
_, err = e.Publish("channel", rawData, PublishOptions{HistorySize: 1, HistoryTTL: time.Second}) |
| 203 |
require.NoError(t, err) |
| 204 |
|
| 205 |
// ask all history. |
| 206 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 207 |
Limit: -1, |
| 208 |
}) |
| 209 |
require.NoError(t, err) |
| 210 |
require.Equal(t, 1, len(pubs)) |
| 211 |
|
| 212 |
// ask more history than history_size. |
| 213 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 214 |
Limit: 2, |
| 215 |
}) |
| 216 |
require.NoError(t, err) |
| 217 |
require.Equal(t, 1, len(pubs)) |
| 218 |
|
| 219 |
// test publishing control message. |
| 220 |
err = e.PublishControl([]byte(""), "") |
| 221 |
require.NoError(t, nil, err) |
| 222 |
|
| 223 |
// test publishing control message. |
| 224 |
err = e.PublishControl([]byte(""), "test") |
| 225 |
require.NoError(t, nil, err) |
| 226 |
|
| 227 |
require.NoError(t, e.PublishJoin("channel", &ClientInfo{})) |
| 228 |
require.NoError(t, e.PublishLeave("channel", &ClientInfo{})) |
| 229 |
}) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
func TestRedisCurrentPosition(t *testing.T) { |
| 234 |
for _, tt := range redisTests { |
| 235 |
t.Run(tt.Name, func(t *testing.T) { |
| 236 |
node := testNode(t) |
| 237 |
e := newTestRedisBroker(t, node, tt.UseStreams, tt.UseCluster) |
| 238 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 239 |
|
| 240 |
channel := "test-current-position" |
| 241 |
|
| 242 |
_, streamTop, err := e.History(channel, HistoryFilter{ |
| 243 |
Limit: 0, |
| 244 |
}) |
| 245 |
require.NoError(t, err) |
| 246 |
require.Equal(t, uint64(0), streamTop.Offset) |
| 247 |
|
| 248 |
rawData := []byte("{}") |
| 249 |
_, err = e.Publish(channel, rawData, PublishOptions{HistorySize: 10, HistoryTTL: 2 * time.Second}) |
| 250 |
require.NoError(t, err) |
| 251 |
|
| 252 |
_, streamTop, err = e.History(channel, HistoryFilter{ |
| 253 |
Limit: 0, |
| 254 |
}) |
| 255 |
require.NoError(t, err) |
| 256 |
require.Equal(t, uint64(1), streamTop.Offset) |
| 257 |
}) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
func TestRedisBrokerRecover(t *testing.T) { |
| 262 |
for _, tt := range redisTests { |
| 263 |
t.Run(tt.Name, func(t *testing.T) { |
| 264 |
node := testNode(t) |
| 265 |
e := newTestRedisBroker(t, node, tt.UseStreams, tt.UseCluster) |
| 266 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 267 |
|
| 268 |
rawData := []byte("{}") |
| 269 |
|
| 270 |
for i := 0; i < 5; i++ { |
| 271 |
_, err := e.Publish("channel", rawData, PublishOptions{HistorySize: 10, HistoryTTL: 2 * time.Second}) |
| 272 |
require.NoError(t, err) |
| 273 |
} |
| 274 |
|
| 275 |
_, streamTop, err := e.History("channel", HistoryFilter{ |
| 276 |
Limit: 0, |
| 277 |
Since: nil, |
| 278 |
}) |
| 279 |
require.NoError(t, err) |
| 280 |
|
| 281 |
pubs, _, err := e.History("channel", HistoryFilter{ |
| 282 |
Limit: -1, |
| 283 |
Since: &StreamPosition{Offset: 2, Epoch: streamTop.Epoch}, |
| 284 |
}) |
| 285 |
require.NoError(t, err) |
| 286 |
require.Equal(t, 3, len(pubs)) |
| 287 |
require.Equal(t, uint64(3), pubs[0].Offset) |
| 288 |
require.Equal(t, uint64(4), pubs[1].Offset) |
| 289 |
require.Equal(t, uint64(5), pubs[2].Offset) |
| 290 |
|
| 291 |
for i := 0; i < 10; i++ { |
| 292 |
_, err := e.Publish("channel", rawData, PublishOptions{HistorySize: 10, HistoryTTL: 2 * time.Second}) |
| 293 |
require.NoError(t, err) |
| 294 |
} |
| 295 |
|
| 296 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 297 |
Limit: -1, |
| 298 |
Since: &StreamPosition{Offset: 0, Epoch: streamTop.Epoch}, |
| 299 |
}) |
| 300 |
require.NoError(t, err) |
| 301 |
require.Equal(t, 10, len(pubs)) |
| 302 |
|
| 303 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 304 |
Limit: -1, |
| 305 |
Since: &StreamPosition{Offset: 100, Epoch: streamTop.Epoch}, |
| 306 |
}) |
| 307 |
require.NoError(t, err) |
| 308 |
require.Equal(t, 0, len(pubs)) |
| 309 |
|
| 310 |
require.NoError(t, e.RemoveHistory("channel")) |
| 311 |
pubs, _, err = e.History("channel", HistoryFilter{ |
| 312 |
Limit: -1, |
| 313 |
Since: &StreamPosition{Offset: 2, Epoch: streamTop.Epoch}, |
| 314 |
}) |
| 315 |
require.NoError(t, err) |
| 316 |
require.Equal(t, 0, len(pubs)) |
| 317 |
}) |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
func pubSubChannels(t *testing.T, e *RedisBroker) ([]string, error) { |
| 322 |
conn := e.shards[0].pool.Get() |
| 323 |
defer func() { require.NoError(t, conn.Close()) }() |
| 324 |
return redis.Strings(conn.Do("PUBSUB", "channels", e.messagePrefix+"*")) |
| 325 |
} |
| 326 |
|
| 327 |
func TestRedisBrokerSubscribeUnsubscribe(t *testing.T) { |
| 328 |
// Custom prefix to not collide with other tests. |
| 329 |
node := testNode(t) |
| 330 |
e := NewTestRedisBrokerWithPrefix(t, node, getUniquePrefix(), false) |
| 331 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 332 |
|
| 333 |
if e.shards[0].useCluster { |
| 334 |
t.Skip("Channels command is not supported when Redis Cluster is used") |
| 335 |
} |
| 336 |
|
| 337 |
require.NoError(t, e.Subscribe("1-test")) |
| 338 |
require.NoError(t, e.Subscribe("1-test")) |
| 339 |
channels, err := pubSubChannels(t, e) |
| 340 |
require.NoError(t, err) |
| 341 |
if len(channels) != 1 { |
| 342 |
// Redis PUBSUB CHANNELS command looks like eventual consistent, so sometimes |
| 343 |
// it returns wrong results, sleeping for a while helps in such situations. |
| 344 |
// See https://gist.github.com/FZambia/80a5241e06b4662f7fe89cfaf24072c3 |
| 345 |
time.Sleep(500 * time.Millisecond) |
| 346 |
channels, err := pubSubChannels(t, e) |
| 347 |
require.NoError(t, err) |
| 348 |
require.Equal(t, 1, len(channels), fmt.Sprintf("%#v", channels)) |
| 349 |
} |
| 350 |
|
| 351 |
require.NoError(t, e.Unsubscribe("1-test")) |
| 352 |
channels, err = pubSubChannels(t, e) |
| 353 |
require.NoError(t, err) |
| 354 |
if len(channels) != 0 { |
| 355 |
time.Sleep(500 * time.Millisecond) |
| 356 |
channels, _ := pubSubChannels(t, e) |
| 357 |
require.Equal(t, 0, len(channels), fmt.Sprintf("%#v", channels)) |
| 358 |
} |
| 359 |
|
| 360 |
var wg sync.WaitGroup |
| 361 |
|
| 362 |
// The same channel in parallel. |
| 363 |
for i := 0; i < 100; i++ { |
| 364 |
wg.Add(1) |
| 365 |
go func() { |
| 366 |
defer wg.Done() |
| 367 |
require.NoError(t, e.Subscribe("2-test")) |
| 368 |
require.NoError(t, e.Unsubscribe("2-test")) |
| 369 |
}() |
| 370 |
} |
| 371 |
wg.Wait() |
| 372 |
channels, err = pubSubChannels(t, e) |
| 373 |
require.NoError(t, err) |
| 374 |
|
| 375 |
if len(channels) != 0 { |
| 376 |
time.Sleep(500 * time.Millisecond) |
| 377 |
channels, _ := pubSubChannels(t, e) |
| 378 |
require.Equal(t, 0, len(channels), fmt.Sprintf("%#v", channels)) |
| 379 |
} |
| 380 |
|
| 381 |
// Different channels in parallel. |
| 382 |
for i := 0; i < 100; i++ { |
| 383 |
wg.Add(1) |
| 384 |
go func(i int) { |
| 385 |
defer wg.Done() |
| 386 |
require.NoError(t, e.Subscribe("3-test-"+strconv.Itoa(i))) |
| 387 |
require.NoError(t, e.Unsubscribe("3-test-"+strconv.Itoa(i))) |
| 388 |
}(i) |
| 389 |
} |
| 390 |
wg.Wait() |
| 391 |
channels, err = pubSubChannels(t, e) |
| 392 |
require.Equal(t, nil, err) |
| 393 |
if len(channels) != 0 { |
| 394 |
time.Sleep(500 * time.Millisecond) |
| 395 |
channels, err := pubSubChannels(t, e) |
| 396 |
require.NoError(t, err) |
| 397 |
require.Equal(t, 0, len(channels), fmt.Sprintf("%#v", channels)) |
| 398 |
} |
| 399 |
|
| 400 |
// The same channel sequential. |
| 401 |
for i := 0; i < 10000; i++ { |
| 402 |
require.NoError(t, e.Subscribe("4-test")) |
| 403 |
require.NoError(t, e.Unsubscribe("4-test")) |
| 404 |
} |
| 405 |
channels, err = pubSubChannels(t, e) |
| 406 |
require.NoError(t, err) |
| 407 |
if len(channels) != 0 { |
| 408 |
time.Sleep(500 * time.Millisecond) |
| 409 |
channels, _ := pubSubChannels(t, e) |
| 410 |
require.Equal(t, 0, len(channels), fmt.Sprintf("%#v", channels)) |
| 411 |
} |
| 412 |
|
| 413 |
// Different channels sequential. |
| 414 |
for j := 0; j < 10; j++ { |
| 415 |
for i := 0; i < 10000; i++ { |
| 416 |
require.NoError(t, e.Subscribe("5-test-"+strconv.Itoa(i))) |
| 417 |
require.NoError(t, e.Unsubscribe("5-test-"+strconv.Itoa(i))) |
| 418 |
} |
| 419 |
channels, err = pubSubChannels(t, e) |
| 420 |
require.NoError(t, err) |
| 421 |
if len(channels) != 0 { |
| 422 |
time.Sleep(500 * time.Millisecond) |
| 423 |
channels, err := pubSubChannels(t, e) |
| 424 |
require.NoError(t, err) |
| 425 |
require.Equal(t, 0, len(channels), fmt.Sprintf("%#v", channels)) |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
// Different channels subscribe only in parallel. |
| 430 |
for i := 0; i < 100; i++ { |
| 431 |
wg.Add(1) |
| 432 |
go func(i int) { |
| 433 |
defer wg.Done() |
| 434 |
require.NoError(t, e.Subscribe("6-test-"+strconv.Itoa(i))) |
| 435 |
}(i) |
| 436 |
} |
| 437 |
wg.Wait() |
| 438 |
channels, err = pubSubChannels(t, e) |
| 439 |
require.NoError(t, err) |
| 440 |
if len(channels) != 100 { |
| 441 |
time.Sleep(500 * time.Millisecond) |
| 442 |
channels, err := pubSubChannels(t, e) |
| 443 |
require.NoError(t, err) |
| 444 |
require.Equal(t, 100, len(channels), fmt.Sprintf("%#v", channels)) |
| 445 |
} |
| 446 |
|
| 447 |
// Different channels unsubscribe only in parallel. |
| 448 |
for i := 0; i < 100; i++ { |
| 449 |
wg.Add(1) |
| 450 |
go func(i int) { |
| 451 |
defer wg.Done() |
| 452 |
require.NoError(t, e.Unsubscribe("6-test-"+strconv.Itoa(i))) |
| 453 |
}(i) |
| 454 |
} |
| 455 |
wg.Wait() |
| 456 |
channels, err = pubSubChannels(t, e) |
| 457 |
require.NoError(t, err) |
| 458 |
if len(channels) != 0 { |
| 459 |
time.Sleep(500 * time.Millisecond) |
| 460 |
channels, _ := pubSubChannels(t, e) |
| 461 |
require.Equal(t, 0, len(channels), fmt.Sprintf("%#v", channels)) |
| 462 |
} |
| 463 |
|
| 464 |
for i := 0; i < 1000; i++ { |
| 465 |
wg.Add(1) |
| 466 |
go func(i int) { |
| 467 |
defer wg.Done() |
| 468 |
require.NoError(t, e.Unsubscribe("7-test-"+strconv.Itoa(i))) |
| 469 |
require.NoError(t, e.Unsubscribe("8-test-"+strconv.Itoa(i))) |
| 470 |
require.NoError(t, e.Subscribe("8-test-"+strconv.Itoa(i))) |
| 471 |
require.NoError(t, e.Unsubscribe("9-test-"+strconv.Itoa(i))) |
| 472 |
require.NoError(t, e.Subscribe("7-test-"+strconv.Itoa(i))) |
| 473 |
require.NoError(t, e.Unsubscribe("8-test-"+strconv.Itoa(i))) |
| 474 |
require.NoError(t, e.Subscribe("9-test-"+strconv.Itoa(i))) |
| 475 |
require.NoError(t, e.Unsubscribe("9-test-"+strconv.Itoa(i))) |
| 476 |
require.NoError(t, e.Unsubscribe("7-test-"+strconv.Itoa(i))) |
| 477 |
}(i) |
| 478 |
} |
| 479 |
wg.Wait() |
| 480 |
channels, err = pubSubChannels(t, e) |
| 481 |
require.NoError(t, err) |
| 482 |
if len(channels) != 0 { |
| 483 |
time.Sleep(500 * time.Millisecond) |
| 484 |
channels, err := pubSubChannels(t, e) |
| 485 |
require.NoError(t, err) |
| 486 |
require.Equal(t, 0, len(channels), fmt.Sprintf("%#v", channels)) |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
| 491 |
|
| 492 |
func randString(n int) string { |
| 493 |
random := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 494 |
b := make([]rune, n) |
| 495 |
for i := range b { |
| 496 |
b[i] = letterRunes[random.Intn(len(letterRunes))] |
| 497 |
} |
| 498 |
return string(b) |
| 499 |
} |
| 500 |
|
| 501 |
// TestRedisConsistentIndex exists to test consistent hashing algorithm we use. |
| 502 |
// As we use random in this test we carefully do requires here. |
| 503 |
// At least it can protect us from stupid mistakes to certain degree. |
| 504 |
// We just expect +-equal distribution and keeping most of chans on |
| 505 |
// the same shard after resharding. |
| 506 |
func TestRedisConsistentIndex(t *testing.T) { |
| 507 |
|
| 508 |
rand.Seed(time.Now().UnixNano()) |
| 509 |
numChans := 10000 |
| 510 |
numShards := 10 |
| 511 |
chans := make([]string, numChans) |
| 512 |
for i := 0; i < numChans; i++ { |
| 513 |
chans[i] = randString(rand.Intn(10) + 1) |
| 514 |
} |
| 515 |
chanToShard := make(map[string]int) |
| 516 |
chanToReshard := make(map[string]int) |
| 517 |
shardToChan := make(map[int][]string) |
| 518 |
|
| 519 |
for _, ch := range chans { |
| 520 |
shard := consistentIndex(ch, numShards) |
| 521 |
reshard := consistentIndex(ch, numShards+1) |
| 522 |
chanToShard[ch] = shard |
| 523 |
chanToReshard[ch] = reshard |
| 524 |
|
| 525 |
if _, ok := shardToChan[shard]; !ok { |
| 526 |
shardToChan[shard] = []string{} |
| 527 |
} |
| 528 |
shardChans := shardToChan[shard] |
| 529 |
shardChans = append(shardChans, ch) |
| 530 |
shardToChan[shard] = shardChans |
| 531 |
} |
| 532 |
|
| 533 |
for shard, shardChans := range shardToChan { |
| 534 |
shardFraction := float64(len(shardChans)) * 100 / float64(len(chans)) |
| 535 |
fmt.Printf("Shard %d: %f%%\n", shard, shardFraction) |
| 536 |
} |
| 537 |
|
| 538 |
sameShards := 0 |
| 539 |
|
| 540 |
// And test resharding. |
| 541 |
for ch, shard := range chanToShard { |
| 542 |
reshard := chanToReshard[ch] |
| 543 |
if shard == reshard { |
| 544 |
sameShards++ |
| 545 |
} |
| 546 |
} |
| 547 |
sameFraction := float64(sameShards) * 100 / float64(len(chans)) |
| 548 |
fmt.Printf("Same shards after resharding: %f%%\n", sameFraction) |
| 549 |
require.True(t, sameFraction > 0.7) |
| 550 |
} |
| 551 |
|
| 552 |
func TestRedisBrokerHandlePubSubMessage(t *testing.T) { |
| 553 |
node := testNode(t) |
| 554 |
e := NewTestRedisBrokerWithPrefix(t, node, getUniquePrefix(), false) |
| 555 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 556 |
err := e.handleRedisClientMessage(&testBrokerEventHandler{HandlePublicationFunc: func(ch string, pub *Publication, sp StreamPosition) error { |
| 557 |
require.Equal(t, "test", ch) |
| 558 |
require.Equal(t, uint64(16901), sp.Offset) |
| 559 |
require.Equal(t, "xyz", sp.Epoch) |
| 560 |
return nil |
| 561 |
}}, e.messageChannelID("test"), []byte("__p1:16901:xyz__dsdsd")) |
| 562 |
require.Error(t, err) |
| 563 |
|
| 564 |
err = e.handleRedisClientMessage(&testBrokerEventHandler{HandlePublicationFunc: func(ch string, pub *Publication, sp StreamPosition) error { |
| 565 |
return nil |
| 566 |
}}, e.messageChannelID("test"), []byte("__p1:16901")) |
| 567 |
require.Error(t, err) |
| 568 |
|
| 569 |
pub := &protocol.Publication{ |
| 570 |
Data: []byte("{}"), |
| 571 |
} |
| 572 |
data, err := pub.Marshal() |
| 573 |
require.NoError(t, err) |
| 574 |
var publicationHandlerCalled bool |
| 575 |
err = e.handleRedisClientMessage(&testBrokerEventHandler{HandlePublicationFunc: func(ch string, pub *Publication, sp StreamPosition) error { |
| 576 |
publicationHandlerCalled = true |
| 577 |
require.Equal(t, "test", ch) |
| 578 |
require.Equal(t, uint64(16901), sp.Offset) |
| 579 |
require.Equal(t, "xyz", sp.Epoch) |
| 580 |
return nil |
| 581 |
}}, e.messageChannelID("test"), []byte("__p1:16901:xyz__"+string(data))) |
| 582 |
require.NoError(t, err) |
| 583 |
require.True(t, publicationHandlerCalled) |
| 584 |
|
| 585 |
info := &protocol.ClientInfo{ |
| 586 |
User: "12", |
| 587 |
} |
| 588 |
data, err = info.Marshal() |
| 589 |
require.NoError(t, err) |
| 590 |
var joinHandlerCalled bool |
| 591 |
err = e.handleRedisClientMessage(&testBrokerEventHandler{HandleJoinFunc: func(ch string, info *ClientInfo) error { |
| 592 |
joinHandlerCalled = true |
| 593 |
require.Equal(t, "test", ch) |
| 594 |
require.Equal(t, "12", info.UserID) |
| 595 |
return nil |
| 596 |
}}, e.messageChannelID("test"), append(joinTypePrefix, data...)) |
| 597 |
require.NoError(t, err) |
| 598 |
require.True(t, joinHandlerCalled) |
| 599 |
|
| 600 |
var leaveHandlerCalled bool |
| 601 |
err = e.handleRedisClientMessage(&testBrokerEventHandler{HandleLeaveFunc: func(ch string, info *ClientInfo) error { |
| 602 |
leaveHandlerCalled = true |
| 603 |
require.Equal(t, "test", ch) |
| 604 |
require.Equal(t, "12", info.UserID) |
| 605 |
return nil |
| 606 |
}}, e.messageChannelID("test"), append(leaveTypePrefix, data...)) |
| 607 |
require.NoError(t, err) |
| 608 |
require.True(t, leaveHandlerCalled) |
| 609 |
} |
| 610 |
|
| 611 |
func BenchmarkRedisExtractPushData(b *testing.B) { |
| 612 |
data := []byte(`__p1:16901:xyz__\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`) |
| 613 |
b.ReportAllocs() |
| 614 |
b.ResetTimer() |
| 615 |
for i := 0; i < b.N; i++ { |
| 616 |
_, _, sp, ok := extractPushData(data) |
| 617 |
if !ok { |
| 618 |
b.Fatal("wrong data") |
| 619 |
} |
| 620 |
if sp.Offset != 16901 { |
| 621 |
b.Fatal("wrong offset") |
| 622 |
} |
| 623 |
if sp.Epoch != "xyz" { |
| 624 |
b.Fatal("wrong epoch") |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
func TestRedisExtractPushData(t *testing.T) { |
| 630 |
data := []byte(`__p1:16901:xyz.123__\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`) |
| 631 |
pushData, pushType, sp, ok := extractPushData(data) |
| 632 |
require.True(t, ok) |
| 633 |
require.Equal(t, pubPushType, pushType) |
| 634 |
require.Equal(t, uint64(16901), sp.Offset) |
| 635 |
require.Equal(t, "xyz.123", sp.Epoch) |
| 636 |
require.Equal(t, []byte(`\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`), pushData) |
| 637 |
|
| 638 |
data = []byte(`__16901__\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`) |
| 639 |
pushData, pushType, sp, ok = extractPushData(data) |
| 640 |
require.True(t, ok) |
| 641 |
require.Equal(t, pubPushType, pushType) |
| 642 |
require.Equal(t, uint64(16901), sp.Offset) |
| 643 |
require.Equal(t, "", sp.Epoch) |
| 644 |
require.Equal(t, []byte(`\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`), pushData) |
| 645 |
|
| 646 |
data = []byte(`\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`) |
| 647 |
pushData, pushType, sp, ok = extractPushData(data) |
| 648 |
require.True(t, ok) |
| 649 |
require.Equal(t, pubPushType, pushType) |
| 650 |
require.Equal(t, uint64(0), sp.Offset) |
| 651 |
require.Equal(t, []byte(`\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`), pushData) |
| 652 |
|
| 653 |
data = []byte(`__4294967337__\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`) |
| 654 |
pushData, pushType, sp, ok = extractPushData(data) |
| 655 |
require.True(t, ok) |
| 656 |
require.Equal(t, pubPushType, pushType) |
| 657 |
require.Equal(t, uint64(4294967337), sp.Offset) |
| 658 |
require.Equal(t, []byte(`\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`), pushData) |
| 659 |
|
| 660 |
data = []byte(`__j__\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`) |
| 661 |
pushData, pushType, sp, ok = extractPushData(data) |
| 662 |
require.True(t, ok) |
| 663 |
require.Equal(t, joinPushType, pushType) |
| 664 |
require.Equal(t, uint64(0), sp.Offset) |
| 665 |
require.Equal(t, []byte(`\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`), pushData) |
| 666 |
|
| 667 |
data = []byte(`__l__\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`) |
| 668 |
pushData, pushType, sp, ok = extractPushData(data) |
| 669 |
require.True(t, ok) |
| 670 |
require.Equal(t, leavePushType, pushType) |
| 671 |
require.Equal(t, uint64(0), sp.Offset) |
| 672 |
require.Equal(t, []byte(`\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`), pushData) |
| 673 |
|
| 674 |
data = []byte(`____\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`) |
| 675 |
_, _, _, ok = extractPushData(data) |
| 676 |
require.False(t, ok) |
| 677 |
|
| 678 |
data = []byte(`__a__\x12\nchat:index\x1aU\"\x0e{\"input\":\"__\"}*C\n\x0242\x12$37cb00a9-bcfa-4284-a1ae-607c7da3a8f4\x1a\x15{\"name\": \"Alexander\"}\"\x00`) |
| 679 |
_, _, _, ok = extractPushData(data) |
| 680 |
require.False(t, ok) |
| 681 |
} |
| 682 |
|
| 683 |
func TestNode_OnSurvey_TwoNodes(t *testing.T) { |
| 684 |
redisConf := testRedisConf() |
| 685 |
|
| 686 |
node1, _ := New(DefaultConfig) |
| 687 |
|
| 688 |
s, err := NewRedisShard(node1, redisConf) |
| 689 |
require.NoError(t, err) |
| 690 |
|
| 691 |
prefix := getUniquePrefix() |
| 692 |
|
| 693 |
e1, _ := NewRedisBroker(node1, RedisBrokerConfig{ |
| 694 |
Prefix: prefix, |
| 695 |
Shards: []*RedisShard{s}, |
| 696 |
}) |
| 697 |
node1.SetBroker(e1) |
| 698 |
_ = node1.Run() |
| 699 |
defer func() { _ = node1.Shutdown(context.Background()) }() |
| 700 |
|
| 701 |
node1.OnSurvey(func(event SurveyEvent, callback SurveyCallback) { |
| 702 |
require.Nil(t, event.Data) |
| 703 |
require.Equal(t, "test_op", event.Op) |
| 704 |
callback(SurveyReply{ |
| 705 |
Data: []byte("1"), |
| 706 |
Code: 1, |
| 707 |
}) |
| 708 |
}) |
| 709 |
|
| 710 |
node2, _ := New(DefaultConfig) |
| 711 |
|
| 712 |
s2, err := NewRedisShard(node2, redisConf) |
| 713 |
require.NoError(t, err) |
| 714 |
e2, _ := NewRedisBroker(node2, RedisBrokerConfig{ |
| 715 |
Prefix: prefix, |
| 716 |
Shards: []*RedisShard{s2}, |
| 717 |
}) |
| 718 |
node2.SetBroker(e2) |
| 719 |
_ = node2.Run() |
| 720 |
defer func() { _ = node2.Shutdown(context.Background()) }() |
| 721 |
|
| 722 |
node2.OnSurvey(func(event SurveyEvent, callback SurveyCallback) { |
| 723 |
require.Nil(t, event.Data) |
| 724 |
require.Equal(t, "test_op", event.Op) |
| 725 |
callback(SurveyReply{ |
| 726 |
Data: []byte("2"), |
| 727 |
Code: 2, |
| 728 |
}) |
| 729 |
}) |
| 730 |
|
| 731 |
waitAllNodes(t, node1, 2) |
| 732 |
|
| 733 |
results, err := node1.Survey(context.Background(), "test_op", nil) |
| 734 |
require.NoError(t, err) |
| 735 |
require.Len(t, results, 2) |
| 736 |
res, ok := results[node1.ID()] |
| 737 |
require.True(t, ok) |
| 738 |
require.Equal(t, uint32(1), res.Code) |
| 739 |
require.Equal(t, []byte("1"), res.Data) |
| 740 |
res, ok = results[node2.ID()] |
| 741 |
require.True(t, ok) |
| 742 |
require.Equal(t, uint32(2), res.Code) |
| 743 |
require.Equal(t, []byte("2"), res.Data) |
| 744 |
} |
| 745 |
|
| 746 |
func TestRedisPubSubTwoNodes(t *testing.T) { |
| 747 |
redisConf := testRedisConf() |
| 748 |
|
| 749 |
node1, _ := New(DefaultConfig) |
| 750 |
|
| 751 |
s, err := NewRedisShard(node1, redisConf) |
| 752 |
require.NoError(t, err) |
| 753 |
|
| 754 |
prefix := getUniquePrefix() |
| 755 |
|
| 756 |
e1, _ := NewRedisBroker(node1, RedisBrokerConfig{ |
| 757 |
Prefix: prefix, |
| 758 |
Shards: []*RedisShard{s}, |
| 759 |
}) |
| 760 |
messageCh := make(chan struct{}) |
| 761 |
joinCh := make(chan struct{}) |
| 762 |
leaveCh := make(chan struct{}) |
| 763 |
brokerEventHandler := &testBrokerEventHandler{ |
| 764 |
HandleControlFunc: func(bytes []byte) error { |
| 765 |
return nil |
| 766 |
}, |
| 767 |
HandlePublicationFunc: func(ch string, pub *Publication, sp StreamPosition) error { |
| 768 |
close(messageCh) |
| 769 |
return nil |
| 770 |
}, |
| 771 |
HandleJoinFunc: func(ch string, info *ClientInfo) error { |
| 772 |
close(joinCh) |
| 773 |
return nil |
| 774 |
}, |
| 775 |
HandleLeaveFunc: func(ch string, info *ClientInfo) error { |
| 776 |
close(leaveCh) |
| 777 |
return nil |
| 778 |
}, |
| 779 |
} |
| 780 |
_ = e1.Run(brokerEventHandler) |
| 781 |
require.NoError(t, e1.Subscribe("test")) |
| 782 |
|
| 783 |
node2, _ := New(DefaultConfig) |
| 784 |
s2, err := NewRedisShard(node2, redisConf) |
| 785 |
require.NoError(t, err) |
| 786 |
|
| 787 |
e2, _ := NewRedisBroker(node2, RedisBrokerConfig{ |
| 788 |
Prefix: prefix, |
| 789 |
Shards: []*RedisShard{s2}, |
| 790 |
}) |
| 791 |
node2.SetBroker(e2) |
| 792 |
_ = node2.Run() |
| 793 |
defer func() { _ = node2.Shutdown(context.Background()) }() |
| 794 |
|
| 795 |
_, err = node2.Publish("test", []byte("123")) |
| 796 |
require.NoError(t, err) |
| 797 |
err = e2.PublishJoin("test", &ClientInfo{}) |
| 798 |
require.NoError(t, err) |
| 799 |
err = e2.PublishLeave("test", &ClientInfo{}) |
| 800 |
require.NoError(t, err) |
| 801 |
|
| 802 |
select { |
| 803 |
case <-messageCh: |
| 804 |
case <-time.After(time.Second): |
| 805 |
require.Fail(t, "timeout waiting for PUB/SUB message") |
| 806 |
} |
| 807 |
select { |
| 808 |
case <-joinCh: |
| 809 |
case <-time.After(time.Second): |
| 810 |
require.Fail(t, "timeout waiting for PUB/SUB join message") |
| 811 |
} |
| 812 |
select { |
| 813 |
case <-leaveCh: |
| 814 |
case <-time.After(time.Second): |
| 815 |
require.Fail(t, "timeout waiting for PUB/SUB leave message") |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
func waitAllNodes(tb testing.TB, node *Node, numNodes int) { |
| 820 |
// Here we are waiting for 2 nodes join. |
| 821 |
// Note: maybe introduce events in future? |
| 822 |
i := 0 |
| 823 |
maxLoops := 20 |
| 824 |
time.Sleep(50 * time.Millisecond) |
| 825 |
for { |
| 826 |
info, err := node.Info() |
| 827 |
require.NoError(tb, err) |
| 828 |
if len(info.Nodes) == numNodes { |
| 829 |
break |
| 830 |
} |
| 831 |
i++ |
| 832 |
if i > maxLoops { |
| 833 |
require.Fail(tb, "timeout waiting for all nodes in cluster") |
| 834 |
} |
| 835 |
time.Sleep(250 * time.Millisecond) |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
var benchSurveyTests = []struct { |
| 840 |
Name string |
| 841 |
NumOtherNodes int |
| 842 |
DataSize int |
| 843 |
}{ |
| 844 |
{"1 node 512B", 0, 512}, |
| 845 |
{"2 nodes 512B", 1, 512}, |
| 846 |
{"3 nodes 512B", 2, 512}, |
| 847 |
{"4 nodes 512B", 3, 512}, |
| 848 |
{"5 nodes 512B", 4, 512}, |
| 849 |
{"10 nodes 512B", 9, 512}, |
| 850 |
{"100 nodes 512B", 99, 512}, |
| 851 |
{"1 node 4KB", 0, 4096}, |
| 852 |
{"2 nodes 4KB", 1, 4096}, |
| 853 |
{"3 nodes 4KB", 2, 4096}, |
| 854 |
{"4 nodes 4KB", 3, 4096}, |
| 855 |
{"5 nodes 4KB", 4, 4096}, |
| 856 |
{"10 nodes 4KB", 9, 4096}, |
| 857 |
{"100 nodes 4KB", 99, 4096}, |
| 858 |
} |
| 859 |
|
| 860 |
func BenchmarkRedisSurvey(b *testing.B) { |
| 861 |
for _, tt := range benchSurveyTests { |
| 862 |
b.Run(tt.Name, func(b *testing.B) { |
| 863 |
prefix := getUniquePrefix() |
| 864 |
redisConf := testRedisConf() |
| 865 |
data := make([]byte, tt.DataSize) |
| 866 |
|
| 867 |
for i := 0; i < tt.NumOtherNodes; i++ { |
| 868 |
node, _ := New(DefaultConfig) |
| 869 |
s, err := NewRedisShard(node, redisConf) |
| 870 |
if err != nil { |
| 871 |
b.Fatal(err) |
| 872 |
} |
| 873 |
broker, _ := NewRedisBroker(node, RedisBrokerConfig{ |
| 874 |
Prefix: prefix, |
| 875 |
Shards: []*RedisShard{s}, |
| 876 |
}) |
| 877 |
node.SetBroker(broker) |
| 878 |
_ = node.Run() |
| 879 |
|
| 880 |
node.OnSurvey(func(event SurveyEvent, callback SurveyCallback) { |
| 881 |
callback(SurveyReply{ |
| 882 |
Data: data, |
| 883 |
Code: 1, |
| 884 |
}) |
| 885 |
}) |
| 886 |
} |
| 887 |
|
| 888 |
node, _ := New(DefaultConfig) |
| 889 |
s, err := NewRedisShard(node, redisConf) |
| 890 |
if err != nil { |
| 891 |
b.Fatal(err) |
| 892 |
} |
| 893 |
broker, _ := NewRedisBroker(node, RedisBrokerConfig{ |
| 894 |
Prefix: prefix, |
| 895 |
Shards: []*RedisShard{s}, |
| 896 |
}) |
| 897 |
node.SetBroker(broker) |
| 898 |
_ = node.Run() |
| 899 |
|
| 900 |
node.OnSurvey(func(event SurveyEvent, callback SurveyCallback) { |
| 901 |
callback(SurveyReply{ |
| 902 |
Data: data, |
| 903 |
Code: 2, |
| 904 |
}) |
| 905 |
}) |
| 906 |
|
| 907 |
waitAllNodes(b, node, tt.NumOtherNodes+1) |
| 908 |
|
| 909 |
b.ResetTimer() |
| 910 |
for i := 0; i < b.N; i++ { |
| 911 |
_, err := node.Survey(context.Background(), "test_op", nil) |
| 912 |
if err != nil { |
| 913 |
b.Fatal(err) |
| 914 |
} |
| 915 |
} |
| 916 |
b.StopTimer() |
| 917 |
}) |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
func BenchmarkRedisConsistentIndex(b *testing.B) { |
| 922 |
for i := 0; i < b.N; i++ { |
| 923 |
consistentIndex(strconv.Itoa(i), 4) |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
func BenchmarkRedisIndex(b *testing.B) { |
| 928 |
for i := 0; i < b.N; i++ { |
| 929 |
index(strconv.Itoa(i), 4) |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
func BenchmarkRedisPublish_1Ch(b *testing.B) { |
| 934 |
node := testNode(b) |
| 935 |
e := newTestRedisBroker(b, node, false, false) |
| 936 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 937 |
rawData := []byte(`{"bench": true}`) |
| 938 |
b.ResetTimer() |
| 939 |
for i := 0; i < b.N; i++ { |
| 940 |
_, err := e.Publish("channel", rawData, PublishOptions{}) |
| 941 |
if err != nil { |
| 942 |
b.Fatal(err) |
| 943 |
} |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
func BenchmarkRedisPublish_1Ch_Parallel(b *testing.B) { |
| 948 |
node := testNode(b) |
| 949 |
e := newTestRedisBroker(b, node, false, false) |
| 950 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 951 |
rawData := []byte(`{"bench": true}`) |
| 952 |
b.SetParallelism(128) |
| 953 |
b.ResetTimer() |
| 954 |
b.RunParallel(func(pb *testing.PB) { |
| 955 |
for pb.Next() { |
| 956 |
_, err := e.Publish("channel", rawData, PublishOptions{}) |
| 957 |
if err != nil { |
| 958 |
b.Fatal(err) |
| 959 |
} |
| 960 |
} |
| 961 |
}) |
| 962 |
} |
| 963 |
|
| 964 |
const benchmarkNumDifferentChannels = 1000 |
| 965 |
|
| 966 |
func BenchmarkRedisPublish_ManyCh(b *testing.B) { |
| 967 |
node := testNode(b) |
| 968 |
e := newTestRedisBroker(b, node, false, false) |
| 969 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 970 |
rawData := []byte(`{"bench": true}`) |
| 971 |
j := 0 |
| 972 |
b.ResetTimer() |
| 973 |
for i := 0; i < b.N; i++ { |
| 974 |
channel := "channel" + strconv.Itoa(j%benchmarkNumDifferentChannels) |
| 975 |
j++ |
| 976 |
_, err := e.Publish(channel, rawData, PublishOptions{}) |
| 977 |
if err != nil { |
| 978 |
b.Fatal(err) |
| 979 |
} |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
func BenchmarkRedisPublish_ManyCh_Parallel(b *testing.B) { |
| 984 |
node := testNode(b) |
| 985 |
e := newTestRedisBroker(b, node, false, false) |
| 986 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 987 |
rawData := []byte(`{"bench": true}`) |
| 988 |
b.SetParallelism(128) |
| 989 |
j := 0 |
| 990 |
b.ResetTimer() |
| 991 |
b.RunParallel(func(pb *testing.PB) { |
| 992 |
for pb.Next() { |
| 993 |
channel := "channel" + strconv.Itoa(j%benchmarkNumDifferentChannels) |
| 994 |
j++ |
| 995 |
_, err := e.Publish(channel, rawData, PublishOptions{}) |
| 996 |
if err != nil { |
| 997 |
b.Fatal(err) |
| 998 |
} |
| 999 |
} |
| 1000 |
}) |
| 1001 |
} |
| 1002 |
|
| 1003 |
func BenchmarkRedisPublish_History_1Ch(b *testing.B) { |
| 1004 |
for _, tt := range benchRedisTests { |
| 1005 |
b.Run(tt.Name, func(b *testing.B) { |
| 1006 |
node := testNode(b) |
| 1007 |
e := newTestRedisBroker(b, node, tt.UseStreams, false) |
| 1008 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1009 |
rawData := []byte(`{"bench": true}`) |
| 1010 |
b.ResetTimer() |
| 1011 |
for i := 0; i < b.N; i++ { |
| 1012 |
chOpts := PublishOptions{HistorySize: 100, HistoryTTL: 100 * time.Second} |
| 1013 |
var err error |
| 1014 |
pos, err := e.Publish("channel", rawData, chOpts) |
| 1015 |
if err != nil { |
| 1016 |
b.Fatal(err) |
| 1017 |
} |
| 1018 |
if pos.Offset == 0 { |
| 1019 |
b.Fail() |
| 1020 |
} |
| 1021 |
} |
| 1022 |
}) |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
func BenchmarkRedisPublish_History_1Ch_Parallel(b *testing.B) { |
| 1027 |
for _, tt := range benchRedisTests { |
| 1028 |
b.Run(tt.Name, func(b *testing.B) { |
| 1029 |
node := testNode(b) |
| 1030 |
e := newTestRedisBroker(b, node, tt.UseStreams, false) |
| 1031 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1032 |
rawData := []byte(`{"bench": true}`) |
| 1033 |
chOpts := PublishOptions{HistorySize: 100, HistoryTTL: 100 * time.Second} |
| 1034 |
b.SetParallelism(128) |
| 1035 |
b.ResetTimer() |
| 1036 |
b.RunParallel(func(pb *testing.PB) { |
| 1037 |
for pb.Next() { |
| 1038 |
var err error |
| 1039 |
pos, err := e.Publish("channel", rawData, chOpts) |
| 1040 |
if err != nil { |
| 1041 |
b.Fatal(err) |
| 1042 |
} |
| 1043 |
if pos.Offset == 0 { |
| 1044 |
b.Fail() |
| 1045 |
} |
| 1046 |
} |
| 1047 |
}) |
| 1048 |
}) |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
func BenchmarkRedisPublish_History_ManyCh(b *testing.B) { |
| 1053 |
for _, tt := range benchRedisTests { |
| 1054 |
b.Run(tt.Name, func(b *testing.B) { |
| 1055 |
node := testNode(b) |
| 1056 |
e := newTestRedisBroker(b, node, tt.UseStreams, false) |
| 1057 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1058 |
rawData := []byte(`{"bench": true}`) |
| 1059 |
j := 0 |
| 1060 |
b.ResetTimer() |
| 1061 |
for i := 0; i < b.N; i++ { |
| 1062 |
j++ |
| 1063 |
channel := "channel" + strconv.Itoa(j%benchmarkNumDifferentChannels) |
| 1064 |
chOpts := PublishOptions{HistorySize: 100, HistoryTTL: 100 * time.Second} |
| 1065 |
var err error |
| 1066 |
pos, err := e.Publish(channel, rawData, chOpts) |
| 1067 |
if err != nil { |
| 1068 |
b.Fatal(err) |
| 1069 |
} |
| 1070 |
if pos.Offset == 0 { |
| 1071 |
b.Fail() |
| 1072 |
} |
| 1073 |
} |
| 1074 |
}) |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
func BenchmarkRedisPub_History_ManyCh_Parallel(b *testing.B) { |
| 1079 |
for _, tt := range benchRedisTests { |
| 1080 |
b.Run(tt.Name, func(b *testing.B) { |
| 1081 |
node := testNode(b) |
| 1082 |
e := newTestRedisBroker(b, node, tt.UseStreams, false) |
| 1083 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1084 |
rawData := []byte(`{"bench": true}`) |
| 1085 |
chOpts := PublishOptions{HistorySize: 100, HistoryTTL: 100 * time.Second} |
| 1086 |
b.SetParallelism(128) |
| 1087 |
j := 0 |
| 1088 |
b.ResetTimer() |
| 1089 |
b.RunParallel(func(pb *testing.PB) { |
| 1090 |
for pb.Next() { |
| 1091 |
j++ |
| 1092 |
channel := "channel" + strconv.Itoa(j%benchmarkNumDifferentChannels) |
| 1093 |
var err error |
| 1094 |
pos, err := e.Publish(channel, rawData, chOpts) |
| 1095 |
if err != nil { |
| 1096 |
b.Fatal(err) |
| 1097 |
} |
| 1098 |
if pos.Offset == 0 { |
| 1099 |
b.Fail() |
| 1100 |
} |
| 1101 |
} |
| 1102 |
}) |
| 1103 |
}) |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
func BenchmarkRedisSubscribe(b *testing.B) { |
| 1108 |
node := testNode(b) |
| 1109 |
e := newTestRedisBroker(b, node, false, false) |
| 1110 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1111 |
j := 0 |
| 1112 |
b.ResetTimer() |
| 1113 |
for i := 0; i < b.N; i++ { |
| 1114 |
j++ |
| 1115 |
err := e.Subscribe("subscribe" + strconv.Itoa(j)) |
| 1116 |
if err != nil { |
| 1117 |
b.Fatal(err) |
| 1118 |
} |
| 1119 |
} |
| 1120 |
} |
| 1121 |
|
| 1122 |
func BenchmarkRedisSubscribe_Parallel(b *testing.B) { |
| 1123 |
node := testNode(b) |
| 1124 |
e := newTestRedisBroker(b, node, false, false) |
| 1125 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1126 |
i := 0 |
| 1127 |
b.SetParallelism(128) |
| 1128 |
b.ResetTimer() |
| 1129 |
b.RunParallel(func(pb *testing.PB) { |
| 1130 |
for pb.Next() { |
| 1131 |
i++ |
| 1132 |
err := e.Subscribe("subscribe" + strconv.Itoa(i)) |
| 1133 |
if err != nil { |
| 1134 |
b.Fatal(err) |
| 1135 |
} |
| 1136 |
} |
| 1137 |
}) |
| 1138 |
} |
| 1139 |
|
| 1140 |
func BenchmarkRedisHistory_1Ch(b *testing.B) { |
| 1141 |
for _, tt := range benchRedisTests { |
| 1142 |
b.Run(tt.Name, func(b *testing.B) { |
| 1143 |
node := testNode(b) |
| 1144 |
e := newTestRedisBroker(b, node, tt.UseStreams, false) |
| 1145 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1146 |
rawData := []byte("{}") |
| 1147 |
for i := 0; i < 4; i++ { |
| 1148 |
_, _ = e.Publish("channel", rawData, PublishOptions{HistorySize: 4, HistoryTTL: 300 * time.Second}) |
| 1149 |
} |
| 1150 |
b.ResetTimer() |
| 1151 |
for i := 0; i < b.N; i++ { |
| 1152 |
_, _, err := e.History("channel", HistoryFilter{ |
| 1153 |
Limit: -1, |
| 1154 |
}) |
| 1155 |
if err != nil { |
| 1156 |
b.Fatal(err) |
| 1157 |
} |
| 1158 |
} |
| 1159 |
}) |
| 1160 |
} |
| 1161 |
} |
| 1162 |
|
| 1163 |
func BenchmarkRedisHistory_1Ch_Parallel(b *testing.B) { |
| 1164 |
for _, tt := range benchRedisTests { |
| 1165 |
b.Run(tt.Name, func(b *testing.B) { |
| 1166 |
node := testNode(b) |
| 1167 |
e := newTestRedisBroker(b, node, tt.UseStreams, false) |
| 1168 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1169 |
rawData := []byte("{}") |
| 1170 |
for i := 0; i < 4; i++ { |
| 1171 |
_, err := e.Publish("channel", rawData, PublishOptions{HistorySize: 4, HistoryTTL: 300 * time.Second}) |
| 1172 |
require.NoError(b, err) |
| 1173 |
} |
| 1174 |
b.ResetTimer() |
| 1175 |
b.RunParallel(func(pb *testing.PB) { |
| 1176 |
for pb.Next() { |
| 1177 |
_, _, err := e.History("channel", HistoryFilter{ |
| 1178 |
Limit: -1, |
| 1179 |
}) |
| 1180 |
if err != nil { |
| 1181 |
b.Fatal(err) |
| 1182 |
} |
| 1183 |
} |
| 1184 |
}) |
| 1185 |
}) |
| 1186 |
} |
| 1187 |
} |
| 1188 |
|
| 1189 |
func BenchmarkRedisRecover_1Ch_Parallel(b *testing.B) { |
| 1190 |
for _, tt := range benchRedisTests { |
| 1191 |
b.Run(tt.Name, func(b *testing.B) { |
| 1192 |
node := testNode(b) |
| 1193 |
e := newTestRedisBroker(b, node, tt.UseStreams, false) |
| 1194 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1195 |
rawData := []byte("{}") |
| 1196 |
numMessages := 1000 |
| 1197 |
numMissing := 5 |
| 1198 |
for i := 1; i <= numMessages; i++ { |
| 1199 |
_, err := e.Publish("channel", rawData, PublishOptions{HistorySize: numMessages, HistoryTTL: 300 * time.Second}) |
| 1200 |
require.NoError(b, err) |
| 1201 |
} |
| 1202 |
b.ResetTimer() |
| 1203 |
b.RunParallel(func(pb *testing.PB) { |
| 1204 |
for pb.Next() { |
| 1205 |
pubs, _, err := e.History("channel", HistoryFilter{ |
| 1206 |
Limit: -1, |
| 1207 |
Since: &StreamPosition{Offset: uint64(numMessages - numMissing), Epoch: ""}, |
| 1208 |
}) |
| 1209 |
if err != nil { |
| 1210 |
b.Fatal(err) |
| 1211 |
} |
| 1212 |
if len(pubs) != numMissing { |
| 1213 |
b.Fail() |
| 1214 |
} |
| 1215 |
} |
| 1216 |
}) |
| 1217 |
}) |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
func nodeWithRedisBroker(tb testing.TB, useStreams bool, useCluster bool) *Node { |
| 1222 |
c := DefaultConfig |
| 1223 |
n, err := New(c) |
| 1224 |
if err != nil { |
| 1225 |
tb.Fatal(err) |
| 1226 |
} |
| 1227 |
e := newTestRedisBroker(tb, n, useStreams, useCluster) |
| 1228 |
n.SetBroker(e) |
| 1229 |
err = n.Run() |
| 1230 |
if err != nil { |
| 1231 |
tb.Fatal(err) |
| 1232 |
} |
| 1233 |
n.OnConnect(func(client *Client) { |
| 1234 |
client.OnSubscribe(func(e SubscribeEvent, cb SubscribeCallback) { |
| 1235 |
cb(SubscribeReply{}, nil) |
| 1236 |
}) |
| 1237 |
client.OnPublish(func(e PublishEvent, cb PublishCallback) { |
| 1238 |
cb(PublishReply{}, nil) |
| 1239 |
}) |
| 1240 |
}) |
| 1241 |
return n |
| 1242 |
} |
| 1243 |
|
| 1244 |
func testRedisClientSubscribeRecover(t *testing.T, tt recoverTest, useStreams bool, useCluster bool) { |
| 1245 |
node := nodeWithRedisBroker(t, useStreams, useCluster) |
| 1246 |
node.config.RecoveryMaxPublicationLimit = tt.Limit |
| 1247 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1248 |
|
| 1249 |
channel := "test_recovery_redis_" + tt.Name |
| 1250 |
|
| 1251 |
for i := 1; i <= tt.NumPublications; i++ { |
| 1252 |
_, err := node.Publish(channel, []byte(`{"n": `+strconv.Itoa(i)+`}`), WithHistory(tt.HistorySize, time.Duration(tt.HistoryLifetime)*time.Second)) |
| 1253 |
require.NoError(t, err) |
| 1254 |
} |
| 1255 |
|
| 1256 |
time.Sleep(time.Duration(tt.Sleep) * time.Second) |
| 1257 |
|
| 1258 |
_, streamTop, err := node.broker.History(channel, HistoryFilter{ |
| 1259 |
Limit: 0, |
| 1260 |
Since: nil, |
| 1261 |
}) |
| 1262 |
require.NoError(t, err) |
| 1263 |
|
| 1264 |
historyResult, err := node.recoverHistory(channel, StreamPosition{tt.SinceOffset, streamTop.Epoch}) |
| 1265 |
require.NoError(t, err) |
| 1266 |
recoveredPubs, recovered := isRecovered(historyResult, tt.SinceOffset, streamTop.Epoch) |
| 1267 |
require.Equal(t, tt.NumRecovered, len(recoveredPubs)) |
| 1268 |
require.Equal(t, tt.Recovered, recovered) |
| 1269 |
} |
| 1270 |
|
| 1271 |
func TestRedisClientSubscribeRecoverStreams(t *testing.T) { |
| 1272 |
for _, tt := range recoverTests { |
| 1273 |
t.Run(tt.Name, func(t *testing.T) { |
| 1274 |
testRedisClientSubscribeRecover(t, tt, true, false) |
| 1275 |
}) |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
func TestRedisClientSubscribeRecoverLists(t *testing.T) { |
| 1280 |
for _, tt := range recoverTests { |
| 1281 |
t.Run(tt.Name, func(t *testing.T) { |
| 1282 |
testRedisClientSubscribeRecover(t, tt, false, false) |
| 1283 |
}) |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
func TestRedisClientSubscribeRecoverStreamsCluster(t *testing.T) { |
| 1288 |
for _, tt := range recoverTests { |
| 1289 |
t.Run(tt.Name, func(t *testing.T) { |
| 1290 |
testRedisClientSubscribeRecover(t, tt, true, true) |
| 1291 |
}) |
| 1292 |
} |
| 1293 |
} |
| 1294 |
|
| 1295 |
func TestRedisClientSubscribeRecoverListsCluster(t *testing.T) { |
| 1296 |
for _, tt := range recoverTests { |
| 1297 |
t.Run(tt.Name, func(t *testing.T) { |
| 1298 |
testRedisClientSubscribeRecover(t, tt, false, true) |
| 1299 |
}) |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
func TestRedisHistoryIteration(t *testing.T) { |
| 1304 |
for _, tt := range redisTests { |
| 1305 |
t.Run(tt.Name, func(t *testing.T) { |
| 1306 |
node := testNode(t) |
| 1307 |
e := newTestRedisBroker(t, node, tt.UseStreams, tt.UseCluster) |
| 1308 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1309 |
it := historyIterationTest{10000, 100} |
| 1310 |
startPosition := it.prepareHistoryIteration(t, e.node) |
| 1311 |
it.testHistoryIteration(t, e.node, startPosition) |
| 1312 |
}) |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
func BenchmarkRedisHistoryIteration(b *testing.B) { |
| 1317 |
for _, tt := range benchRedisTests { |
| 1318 |
b.Run(tt.Name, func(b *testing.B) { |
| 1319 |
node := testNode(b) |
| 1320 |
e := newTestRedisBroker(b, node, tt.UseStreams, false) |
| 1321 |
defer func() { _ = node.Shutdown(context.Background()) }() |
| 1322 |
it := historyIterationTest{10000, 100} |
| 1323 |
startPosition := it.prepareHistoryIteration(b, e.node) |
| 1324 |
b.ResetTimer() |
| 1325 |
for i := 0; i < b.N; i++ { |
| 1326 |
it.testHistoryIteration(b, e.node, startPosition) |
| 1327 |
} |
| 1328 |
}) |
| 1329 |
} |
| 1330 |
} |