|
1 | 1 | import { test } from '@japa/runner' |
2 | 2 | import { sleep } from '@julr/utils/misc' |
3 | 3 | import { MemoryTransport } from '@boringnode/bus/transports/memory' |
| 4 | +import { Redis as IoRedis, Cluster as IoRedisCluster } from 'ioredis' |
4 | 5 |
|
5 | 6 | import { ChaosBus } from '../helpers/chaos/chaos_bus.js' |
6 | 7 | import { ChaosCache } from '../helpers/chaos/chaos_cache.js' |
@@ -411,4 +412,70 @@ test.group('Bus synchronization', () => { |
411 | 412 |
|
412 | 413 | await sleep(200) |
413 | 414 | }).waitForDone() |
| 415 | + |
| 416 | + test('works with an existing Redis instance', async ({ assert }, done) => { |
| 417 | + const redis = new IoRedis(REDIS_CREDENTIALS) |
| 418 | + |
| 419 | + const bus1 = redisBusDriver({ connection: redis }) |
| 420 | + .factory(null as any) |
| 421 | + .setId('foo') |
| 422 | + |
| 423 | + const bus2 = redisBusDriver({ connection: redis }) |
| 424 | + .factory(null as any) |
| 425 | + .setId('bar') |
| 426 | + |
| 427 | + const data = { |
| 428 | + keys: ['foo'], |
| 429 | + type: CacheBusMessageType.Set, |
| 430 | + } |
| 431 | + |
| 432 | + bus1.subscribe('foo', (message: any) => { |
| 433 | + assert.deepInclude(message, data) |
| 434 | + done() |
| 435 | + }) |
| 436 | + |
| 437 | + await sleep(200) |
| 438 | + |
| 439 | + await bus2.publish('foo', data) |
| 440 | + |
| 441 | + await bus1.disconnect() |
| 442 | + await bus2.disconnect() |
| 443 | + await redis.quit() |
| 444 | + |
| 445 | + await sleep(200) |
| 446 | + }).waitForDone() |
| 447 | + |
| 448 | + test('works with an existing Cluster instance', async ({ assert }, done) => { |
| 449 | + const cluster = new IoRedisCluster([{ host: '127.0.0.1', port: 7000 }]) |
| 450 | + |
| 451 | + const bus1 = redisBusDriver({ connection: cluster }) |
| 452 | + .factory(null as any) |
| 453 | + .setId('foo') |
| 454 | + |
| 455 | + const bus2 = redisBusDriver({ connection: cluster }) |
| 456 | + .factory(null as any) |
| 457 | + .setId('bar') |
| 458 | + |
| 459 | + const data = { |
| 460 | + keys: ['foo'], |
| 461 | + type: CacheBusMessageType.Set, |
| 462 | + } |
| 463 | + |
| 464 | + bus1.subscribe('foo', (message: any) => { |
| 465 | + assert.deepInclude(message, data) |
| 466 | + done() |
| 467 | + }) |
| 468 | + |
| 469 | + await sleep(200) |
| 470 | + |
| 471 | + await bus2.publish('foo', data) |
| 472 | + |
| 473 | + await bus1.disconnect() |
| 474 | + await bus2.disconnect() |
| 475 | + await cluster.quit() |
| 476 | + |
| 477 | + await sleep(200) |
| 478 | + }) |
| 479 | + .waitForDone() |
| 480 | + .skip(!!process.env.CI, 'Skipping cluster test on CI') |
414 | 481 | }) |
0 commit comments