@@ -473,6 +473,24 @@ describe("namespaces", () => {
473
473
io . of ( "/nsp" ) ;
474
474
} ) ;
475
475
476
+ it ( "should not clean up a non-dynamic namespace" , ( done ) => {
477
+ const io = new Server ( 0 , { cleanupEmptyChildNamespaces : true } ) ;
478
+ const c1 = createClient ( io , "/chat" ) ;
479
+
480
+ c1 . on ( "connect" , ( ) => {
481
+ c1 . disconnect ( ) ;
482
+
483
+ // Give it some time to disconnect the client
484
+ setTimeout ( ( ) => {
485
+ expect ( io . _nsps . has ( "/chat" ) ) . to . be ( true ) ;
486
+ expect ( io . _nsps . get ( "/chat" ) ! . sockets . size ) . to . be ( 0 ) ;
487
+ success ( done , io ) ;
488
+ } , 100 ) ;
489
+ } ) ;
490
+
491
+ io . of ( "/chat" ) ;
492
+ } ) ;
493
+
476
494
describe ( "dynamic namespaces" , ( ) => {
477
495
it ( "should allow connections to dynamic namespaces with a regex" , ( done ) => {
478
496
const io = new Server ( 0 ) ;
@@ -571,5 +589,68 @@ describe("namespaces", () => {
571
589
one . on ( "message" , handler ) ;
572
590
two . on ( "message" , handler ) ;
573
591
} ) ;
592
+
593
+ it ( "should clean up namespace when cleanupEmptyChildNamespaces is on and there are no more sockets in a namespace" , ( done ) => {
594
+ const io = new Server ( 0 , { cleanupEmptyChildNamespaces : true } ) ;
595
+ const c1 = createClient ( io , "/dynamic-101" ) ;
596
+
597
+ c1 . on ( "connect" , ( ) => {
598
+ c1 . disconnect ( ) ;
599
+
600
+ // Give it some time to disconnect and clean up the namespace
601
+ setTimeout ( ( ) => {
602
+ expect ( io . _nsps . has ( "/dynamic-101" ) ) . to . be ( false ) ;
603
+ success ( done , io ) ;
604
+ } , 100 ) ;
605
+ } ) ;
606
+
607
+ io . of ( / ^ \/ d y n a m i c - \d + $ / ) ;
608
+ } ) ;
609
+
610
+ it ( "should allow a client to connect to a cleaned up namespace" , ( done ) => {
611
+ const io = new Server ( 0 , { cleanupEmptyChildNamespaces : true } ) ;
612
+ const c1 = createClient ( io , "/dynamic-101" ) ;
613
+
614
+ c1 . on ( "connect" , ( ) => {
615
+ c1 . disconnect ( ) ;
616
+
617
+ // Give it some time to disconnect and clean up the namespace
618
+ setTimeout ( ( ) => {
619
+ expect ( io . _nsps . has ( "/dynamic-101" ) ) . to . be ( false ) ;
620
+
621
+ const c2 = createClient ( io , "/dynamic-101" ) ;
622
+
623
+ c2 . on ( "connect" , ( ) => {
624
+ success ( done , io , c2 ) ;
625
+ } ) ;
626
+
627
+ c2 . on ( "connect_error" , ( ) => {
628
+ done (
629
+ new Error ( "Client got error when connecting to dynamic namespace" )
630
+ ) ;
631
+ } ) ;
632
+ } , 100 ) ;
633
+ } ) ;
634
+
635
+ io . of ( / ^ \/ d y n a m i c - \d + $ / ) ;
636
+ } ) ;
637
+
638
+ it ( "should not clean up namespace when cleanupEmptyChildNamespaces is off and there are no more sockets in a namespace" , ( done ) => {
639
+ const io = new Server ( 0 ) ;
640
+ const c1 = createClient ( io , "/dynamic-101" ) ;
641
+
642
+ c1 . on ( "connect" , ( ) => {
643
+ c1 . disconnect ( ) ;
644
+
645
+ // Give it some time to disconnect and clean up the namespace
646
+ setTimeout ( ( ) => {
647
+ expect ( io . _nsps . has ( "/dynamic-101" ) ) . to . be ( true ) ;
648
+ expect ( io . _nsps . get ( "/dynamic-101" ) ! . sockets . size ) . to . be ( 0 ) ;
649
+ success ( done , io ) ;
650
+ } , 100 ) ;
651
+ } ) ;
652
+
653
+ io . of ( / ^ \/ d y n a m i c - \d + $ / ) ;
654
+ } ) ;
574
655
} ) ;
575
656
} ) ;
0 commit comments