1
1
import { expect , use } from "chai" ;
2
+ import * as calculateKeySlot from 'cluster-key-slot' ;
3
+
2
4
import { default as Cluster } from "../../../lib/cluster" ;
3
5
import MockServer from "../../helpers/mock_server" ;
4
6
@@ -395,14 +397,29 @@ describe("autoPipelining for cluster", function () {
395
397
await new Promise ( ( resolve ) => cluster . once ( "connect" , resolve ) ) ;
396
398
397
399
const promise1 = cluster . set ( "foo1" , "bar" ) ;
398
- const promise2 = cluster . set ( "foo2" , "bar" ) ;
400
+ const promise2 = cluster . set ( "foo5" , "bar" ) ;
401
+ const promise3 = cluster . set ( "foo2" , "bar" ) ;
402
+ const promise4 = cluster . set ( "foo6" , "bar" ) ;
403
+
404
+ // Override slots to induce a failure
405
+ const key1Slot = calculateKeySlot ( 'foo1' ) ;
406
+ const key2Slot = calculateKeySlot ( 'foo2' ) ;
407
+ const key5Slot = calculateKeySlot ( 'foo5' ) ;
408
+ cluster . slots [ key1Slot ] = cluster . slots [ key2Slot ] ;
409
+ cluster . slots [ key2Slot ] = cluster . slots [ key5Slot ] ;
399
410
400
411
await expect ( promise1 ) . to . eventually . be . rejectedWith (
401
412
"All keys in the pipeline should belong to the same slots allocation group"
402
413
) ;
403
414
await expect ( promise2 ) . to . eventually . be . rejectedWith (
404
415
"All keys in the pipeline should belong to the same slots allocation group"
405
416
) ;
417
+ await expect ( promise3 ) . to . eventually . be . rejectedWith (
418
+ "All keys in the pipeline should belong to the same slots allocation group"
419
+ ) ;
420
+ await expect ( promise4 ) . to . eventually . be . rejectedWith (
421
+ "All keys in the pipeline should belong to the same slots allocation group"
422
+ ) ;
406
423
407
424
cluster . disconnect ( ) ;
408
425
} ) ;
@@ -411,7 +428,7 @@ describe("autoPipelining for cluster", function () {
411
428
const cluster = new Cluster ( hosts , { enableAutoPipelining : true } ) ;
412
429
413
430
cluster . once ( "connect" , ( ) => {
414
- let err1 , err2 ;
431
+ let err1 , err2 , err3 , err4 ;
415
432
416
433
function cb ( ) {
417
434
expect ( err1 . message ) . to . eql (
@@ -420,6 +437,12 @@ describe("autoPipelining for cluster", function () {
420
437
expect ( err2 . message ) . to . eql (
421
438
"All keys in the pipeline should belong to the same slots allocation group"
422
439
) ;
440
+ expect ( err3 . message ) . to . eql (
441
+ "All keys in the pipeline should belong to the same slots allocation group"
442
+ ) ;
443
+ expect ( err4 . message ) . to . eql (
444
+ "All keys in the pipeline should belong to the same slots allocation group"
445
+ ) ;
423
446
expect ( cluster . autoPipelineQueueSize ) . to . eql ( 0 ) ;
424
447
425
448
cluster . disconnect ( ) ;
@@ -431,22 +454,49 @@ describe("autoPipelining for cluster", function () {
431
454
cluster . set ( "foo1" , "bar1" , ( err ) => {
432
455
err1 = err ;
433
456
434
- if ( err1 && err2 ) {
457
+ if ( err1 && err2 && err3 && err4 ) {
435
458
cb ( ) ;
436
459
}
437
460
} ) ;
438
461
439
462
expect ( cluster . autoPipelineQueueSize ) . to . eql ( 1 ) ;
440
463
441
- cluster . set ( "foo2" , ( err ) => {
464
+ cluster . set ( "foo2" , "bar2" , ( err ) => {
442
465
err2 = err ;
443
466
444
- if ( err1 && err2 ) {
467
+ if ( err1 && err2 && err3 && err4 ) {
445
468
cb ( ) ;
446
469
}
447
470
} ) ;
448
471
449
472
expect ( cluster . autoPipelineQueueSize ) . to . eql ( 2 ) ;
473
+
474
+ cluster . set ( "foo5" , "bar5" , ( err ) => {
475
+ err3 = err ;
476
+
477
+ if ( err1 && err2 && err3 && err4 ) {
478
+ cb ( ) ;
479
+ }
480
+ } ) ;
481
+
482
+ expect ( cluster . autoPipelineQueueSize ) . to . eql ( 3 ) ;
483
+
484
+ cluster . set ( "foo6" , "bar6" , ( err ) => {
485
+ err4 = err ;
486
+
487
+ if ( err1 && err2 && err3 && err4 ) {
488
+ cb ( ) ;
489
+ }
490
+ } ) ;
491
+
492
+ expect ( cluster . autoPipelineQueueSize ) . to . eql ( 4 ) ;
493
+
494
+ // Override slots to induce a failure
495
+ const key1Slot = calculateKeySlot ( 'foo1' ) ;
496
+ const key2Slot = calculateKeySlot ( 'foo2' ) ;
497
+ const key5Slot = calculateKeySlot ( 'foo5' ) ;
498
+ cluster . slots [ key1Slot ] = cluster . slots [ key2Slot ] ;
499
+ cluster . slots [ key2Slot ] = cluster . slots [ key5Slot ] ;
450
500
} ) ;
451
501
} ) ;
452
502
@@ -457,13 +507,16 @@ describe("autoPipelining for cluster", function () {
457
507
process . removeAllListeners ( "uncaughtException" ) ;
458
508
459
509
cluster . once ( "connect" , ( ) => {
460
- let err1 ;
510
+ let err1 , err5 ;
461
511
462
512
process . once ( "uncaughtException" , ( err ) => {
463
513
expect ( err . message ) . to . eql ( "ERROR" ) ;
464
514
expect ( err1 . message ) . to . eql (
465
515
"All keys in the pipeline should belong to the same slots allocation group"
466
516
) ;
517
+ expect ( err5 . message ) . to . eql (
518
+ "All keys in the pipeline should belong to the same slots allocation group"
519
+ ) ;
467
520
468
521
for ( const listener of listeners ) {
469
522
process . on ( "uncaughtException" , listener ) ;
@@ -476,14 +529,21 @@ describe("autoPipelining for cluster", function () {
476
529
cluster . set ( "foo1" , "bar1" , ( err ) => {
477
530
err1 = err ;
478
531
} ) ;
532
+ cluster . set ( "foo5" , "bar5" , ( err ) => {
533
+ err5 = err ;
534
+ } ) ;
479
535
480
- expect ( cluster . autoPipelineQueueSize ) . to . eql ( 1 ) ;
536
+ expect ( cluster . autoPipelineQueueSize ) . to . eql ( 2 ) ;
481
537
482
538
cluster . set ( "foo2" , ( err ) => {
483
539
throw new Error ( "ERROR" ) ;
484
540
} ) ;
485
541
486
- expect ( cluster . autoPipelineQueueSize ) . to . eql ( 2 ) ;
542
+ expect ( cluster . autoPipelineQueueSize ) . to . eql ( 3 ) ;
543
+
544
+ const key1Slot = calculateKeySlot ( 'foo1' ) ;
545
+ const key2Slot = calculateKeySlot ( 'foo2' ) ;
546
+ cluster . slots [ key1Slot ] = cluster . slots [ key2Slot ] ;
487
547
} ) ;
488
548
} ) ;
489
549
} ) ;
0 commit comments