File tree 2 files changed +28
-5
lines changed
2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -84,12 +84,12 @@ class BalancerManager {
84
84
private onBalancerDisconnect ( conn : BalancerConnection , code : number , reason : string ) {
85
85
log . debug ( `Disconnected from balancer ${ conn . id } : ${ code } ${ reason } ` ) ;
86
86
this . emit ( "disconnect" , conn ) ;
87
- for ( const conn of this . balancerConnections ) {
88
- if ( conn . id === conn . id ) {
89
- this . balancerConnections . splice ( this . balancerConnections . indexOf ( conn ) , 1 ) ;
90
- break ;
91
- }
87
+ const idx = this . balancerConnections . indexOf ( conn ) ;
88
+ if ( idx === - 1 ) {
89
+ log . error ( `Balancer ${ conn . id } was not found in balancerConnections` ) ;
90
+ return ;
92
91
}
92
+ this . balancerConnections . splice ( idx , 1 ) ;
93
93
}
94
94
95
95
private onBalancerMessage ( conn : BalancerConnection , message : MsgB2M ) {
Original file line number Diff line number Diff line change @@ -118,3 +118,26 @@ describe("ClientManager", () => {
118
118
expect ( joins2 ) . toHaveLength ( 0 ) ;
119
119
} ) ;
120
120
} ) ;
121
+
122
+ describe ( "BalancerManager" , ( ) => {
123
+ beforeEach ( ( ) => {
124
+ balancerManager . balancerConnections . splice ( 0 , balancerManager . balancerConnections . length ) ;
125
+ } ) ;
126
+
127
+ it ( "should remove the correct balancer from the list when it disconnects" , ( ) => {
128
+ const con1 = new BalancerConnectionMock ( ) ;
129
+ const con2 = new BalancerConnectionMock ( ) ;
130
+ const con3 = new BalancerConnectionMock ( ) ;
131
+
132
+ balancerManager . addBalancerConnection ( con1 ) ;
133
+ balancerManager . addBalancerConnection ( con2 ) ;
134
+ balancerManager . addBalancerConnection ( con3 ) ;
135
+
136
+ expect ( balancerManager . balancerConnections ) . toHaveLength ( 3 ) ;
137
+
138
+ con2 . emit ( "disconnect" , 1000 , "reason" ) ;
139
+
140
+ expect ( balancerManager . balancerConnections ) . toHaveLength ( 2 ) ;
141
+ expect ( balancerManager . balancerConnections ) . not . toContain ( con2 ) ;
142
+ } ) ;
143
+ } ) ;
You can’t perform that action at this time.
0 commit comments