@@ -53,7 +53,7 @@ var connectionHandler = function (socket) {
5353} ;
5454
5555describe ( 'integration tests' , function ( ) {
56- before ( 'run the server before start' , function ( done ) {
56+ beforeEach ( 'run the server before start' , function ( done ) {
5757 server = socketClusterServer . listen ( PORT , serverOptions ) ;
5858 server . on ( 'connection' , connectionHandler ) ;
5959
@@ -67,40 +67,41 @@ describe('integration tests', function () {
6767 }
6868 } ) ;
6969
70- server . once ( 'ready' , function ( ) {
71- done ( ) ;
72- } ) ;
73- } ) ;
74-
75- after ( 'shut down server afterwards' , function ( done ) {
76- server . close ( ) ;
77- done ( ) ;
78- } ) ;
79-
80- beforeEach ( 'Prepare data for test case' , function ( done ) {
8170 clientOptions = {
8271 hostname : '127.0.0.1' ,
8372 port : PORT ,
8473 multiplex : false ,
8574 ackTimeout : 200
8675 } ;
87- done ( ) ;
76+
77+ server . once ( 'ready' , function ( ) {
78+ done ( ) ;
79+ } ) ;
8880 } ) ;
8981
90- afterEach ( 'shut down client after each test' , function ( done ) {
82+ afterEach ( 'shut down server afterwards' , function ( ) {
83+ var cleanupTasks = [ ] ;
9184 global . localStorage . removeItem ( 'socketCluster.authToken' ) ;
9285 if ( client && client . state != client . CLOSED ) {
93- client . once ( 'disconnect' , function ( ) {
94- done ( ) ;
95- } ) ;
96- client . once ( 'connectAbort' , function ( ) {
97- done ( ) ;
98- } ) ;
86+ cleanupTasks . push ( new Promise ( function ( resolve , reject ) {
87+ client . once ( 'disconnect' , function ( ) {
88+ resolve ( ) ;
89+ } ) ;
90+ client . once ( 'connectAbort' , function ( ) {
91+ resolve ( ) ;
92+ } ) ;
93+ } ) ) ;
9994 client . destroy ( ) ;
10095 } else {
10196 client . destroy ( ) ;
102- done ( ) ;
10397 }
98+ cleanupTasks . push ( new Promise ( function ( resolve ) {
99+ server . close ( function ( ) {
100+ PORT ++ ;
101+ resolve ( ) ;
102+ } ) ;
103+ } ) ) ;
104+ return Promise . all ( cleanupTasks ) ;
104105 } ) ;
105106
106107 describe ( 'authentication' , function ( ) {
@@ -183,7 +184,7 @@ describe('integration tests', function () {
183184 } ) ;
184185
185186 it ( 'token should be available inside login callback if token engine signing is synchronous' , function ( done ) {
186- var port = 8009 ;
187+ var port = 8509 ;
187188 server = socketClusterServer . listen ( port , {
188189 authKey : serverOptions . authKey ,
189190 authSignAsync : false
@@ -207,7 +208,7 @@ describe('integration tests', function () {
207208 } ) ;
208209
209210 it ( 'if token engine signing is asynchronous, authentication can be captured using the authenticate event' , function ( done ) {
210- var port = 8010 ;
211+ var port = 8510 ;
211212 server = socketClusterServer . listen ( port , {
212213 authKey : serverOptions . authKey ,
213214 authSignAsync : true
@@ -232,7 +233,7 @@ describe('integration tests', function () {
232233 } ) ;
233234
234235 it ( 'should still work if token verification is asynchronous' , function ( done ) {
235- var port = 8011 ;
236+ var port = 8511 ;
236237 server = socketClusterServer . listen ( port , {
237238 authKey : serverOptions . authKey ,
238239 authVerifyAsync : false
@@ -978,6 +979,58 @@ describe('integration tests', function () {
978979 } ) ;
979980 } ) ;
980981 } ) ;
982+
983+ it ( 'should reconnect if emit is called on a disconnected socket' , function ( done ) {
984+ var fooEventTriggered = false ;
985+ server . on ( 'connection' , function ( socket ) {
986+ socket . on ( 'foo' , function ( ) {
987+ fooEventTriggered = true ;
988+ } ) ;
989+ } ) ;
990+
991+ client = socketClusterClient . create ( clientOptions ) ;
992+
993+ var clientError ;
994+ client . on ( 'error' , function ( err ) {
995+ clientError = err ;
996+ } ) ;
997+
998+ var eventList = [ ] ;
999+
1000+ client . on ( 'connecting' , function ( ) {
1001+ eventList . push ( 'connecting' ) ;
1002+ } ) ;
1003+ client . on ( 'connect' , function ( ) {
1004+ eventList . push ( 'connect' ) ;
1005+ } ) ;
1006+ client . on ( 'disconnect' , function ( ) {
1007+ eventList . push ( 'disconnect' ) ;
1008+ } ) ;
1009+ client . on ( 'close' , function ( ) {
1010+ eventList . push ( 'close' ) ;
1011+ } ) ;
1012+ client . on ( 'connectAbort' , function ( ) {
1013+ eventList . push ( 'connectAbort' ) ;
1014+ } ) ;
1015+
1016+ client . once ( 'connect' , function ( ) {
1017+ client . disconnect ( ) ;
1018+ client . emit ( 'foo' , 123 ) ;
1019+ } ) ;
1020+
1021+ setTimeout ( function ( ) {
1022+ var expectedEventList = [ 'connect' , 'disconnect' , 'close' , 'connecting' , 'connect' ] ;
1023+ assert . equal ( JSON . stringify ( eventList ) , JSON . stringify ( expectedEventList ) ) ;
1024+ assert . equal ( fooEventTriggered , true ) ;
1025+ done ( ) ;
1026+ } , 1000 ) ;
1027+ } ) ;
1028+
1029+ it ( 'TODO: should throw an error if emit is called on a destroyed socket' , function ( done ) {
1030+ setTimeout ( function ( ) {
1031+ done ( ) ;
1032+ } , 200 ) ;
1033+ } ) ;
9811034 } ) ;
9821035
9831036 describe ( 'destroying a channel' , function ( ) {
0 commit comments