@@ -4,7 +4,7 @@ import { io as ioc, Socket as ClientSocket } from "socket.io-client";
44import expect = require( "expect.js" ) ;
55import { createAdapter , PostgresAdapter } from "../lib" ;
66import type { AddressInfo } from "net" ;
7- import { times , sleep } from "./util" ;
7+ import { times , sleep , shouldNotHappen } from "./util" ;
88import { Pool } from "pg" ;
99
1010const NODES_COUNT = 3 ;
@@ -126,17 +126,9 @@ describe("@socket.io/postgres-adapter", () => {
126126 it ( "broadcasts to all clients in a room" , ( done ) => {
127127 serverSockets [ 1 ] . join ( "room1" ) ;
128128
129- clientSockets [ 0 ] . on ( "test" , ( ) => {
130- done ( new Error ( "should not happen" ) ) ;
131- } ) ;
132-
133- clientSockets [ 1 ] . on ( "test" , ( ) => {
134- done ( ) ;
135- } ) ;
136-
137- clientSockets [ 2 ] . on ( "test" , ( ) => {
138- done ( new Error ( "should not happen" ) ) ;
139- } ) ;
129+ clientSockets [ 0 ] . on ( "test" , shouldNotHappen ( done ) ) ;
130+ clientSockets [ 1 ] . on ( "test" , ( ) => done ( ) ) ;
131+ clientSockets [ 2 ] . on ( "test" , shouldNotHappen ( done ) ) ;
140132
141133 servers [ 0 ] . to ( "room1" ) . emit ( "test" ) ;
142134 } ) ;
@@ -145,49 +137,25 @@ describe("@socket.io/postgres-adapter", () => {
145137 const partialDone = times ( 2 , done ) ;
146138 serverSockets [ 1 ] . join ( "room1" ) ;
147139
148- clientSockets [ 0 ] . on ( "test" , ( ) => {
149- partialDone ( ) ;
150- } ) ;
151-
152- clientSockets [ 1 ] . on ( "test" , ( ) => {
153- done ( new Error ( "should not happen" ) ) ;
154- } ) ;
155-
156- clientSockets [ 2 ] . on ( "test" , ( ) => {
157- partialDone ( ) ;
158- } ) ;
140+ clientSockets [ 0 ] . on ( "test" , ( ) => partialDone ( ) ) ;
141+ clientSockets [ 1 ] . on ( "test" , shouldNotHappen ( done ) ) ;
142+ clientSockets [ 2 ] . on ( "test" , ( ) => partialDone ( ) ) ;
159143
160144 servers [ 0 ] . of ( "/" ) . except ( "room1" ) . emit ( "test" ) ;
161145 } ) ;
162146
163147 it ( "broadcasts to local clients only" , ( done ) => {
164- clientSockets [ 0 ] . on ( "test" , ( ) => {
165- done ( ) ;
166- } ) ;
167-
168- clientSockets [ 1 ] . on ( "test" , ( ) => {
169- done ( new Error ( "should not happen" ) ) ;
170- } ) ;
171-
172- clientSockets [ 2 ] . on ( "test" , ( ) => {
173- done ( new Error ( "should not happen" ) ) ;
174- } ) ;
148+ clientSockets [ 0 ] . on ( "test" , ( ) => done ( ) ) ;
149+ clientSockets [ 1 ] . on ( "test" , shouldNotHappen ( done ) ) ;
150+ clientSockets [ 2 ] . on ( "test" , shouldNotHappen ( done ) ) ;
175151
176152 servers [ 0 ] . local . emit ( "test" ) ;
177153 } ) ;
178154
179155 it ( "broadcasts with multiple acknowledgements" , ( done ) => {
180- clientSockets [ 0 ] . on ( "test" , ( cb ) => {
181- cb ( 1 ) ;
182- } ) ;
183-
184- clientSockets [ 1 ] . on ( "test" , ( cb ) => {
185- cb ( 2 ) ;
186- } ) ;
187-
188- clientSockets [ 2 ] . on ( "test" , ( cb ) => {
189- cb ( 3 ) ;
190- } ) ;
156+ clientSockets [ 0 ] . on ( "test" , ( cb ) => cb ( 1 ) ) ;
157+ clientSockets [ 1 ] . on ( "test" , ( cb ) => cb ( 2 ) ) ;
158+ clientSockets [ 2 ] . on ( "test" , ( cb ) => cb ( 3 ) ) ;
191159
192160 servers [ 0 ] . timeout ( 500 ) . emit ( "test" , ( err : Error , responses : any [ ] ) => {
193161 expect ( err ) . to . be ( null ) ;
@@ -205,17 +173,9 @@ describe("@socket.io/postgres-adapter", () => {
205173 } ) ;
206174
207175 it ( "broadcasts with multiple acknowledgements (binary content)" , ( done ) => {
208- clientSockets [ 0 ] . on ( "test" , ( cb ) => {
209- cb ( Buffer . from ( [ 1 ] ) ) ;
210- } ) ;
211-
212- clientSockets [ 1 ] . on ( "test" , ( cb ) => {
213- cb ( Buffer . from ( [ 2 ] ) ) ;
214- } ) ;
215-
216- clientSockets [ 2 ] . on ( "test" , ( cb ) => {
217- cb ( Buffer . from ( [ 3 ] ) ) ;
218- } ) ;
176+ clientSockets [ 0 ] . on ( "test" , ( cb ) => cb ( Buffer . from ( [ 1 ] ) ) ) ;
177+ clientSockets [ 1 ] . on ( "test" , ( cb ) => cb ( Buffer . from ( [ 2 ] ) ) ) ;
178+ clientSockets [ 2 ] . on ( "test" , ( cb ) => cb ( Buffer . from ( [ 3 ] ) ) ) ;
219179
220180 servers [ 0 ] . timeout ( 500 ) . emit ( "test" , ( err : Error , responses : any [ ] ) => {
221181 expect ( err ) . to . be ( null ) ;
@@ -240,15 +200,9 @@ describe("@socket.io/postgres-adapter", () => {
240200 } ) ;
241201
242202 it ( "broadcasts with multiple acknowledgements (timeout)" , ( done ) => {
243- clientSockets [ 0 ] . on ( "test" , ( cb ) => {
244- cb ( 1 ) ;
245- } ) ;
246-
247- clientSockets [ 1 ] . on ( "test" , ( cb ) => {
248- cb ( 2 ) ;
249- } ) ;
250-
251- clientSockets [ 2 ] . on ( "test" , ( cb ) => {
203+ clientSockets [ 0 ] . on ( "test" , ( cb ) => cb ( 1 ) ) ;
204+ clientSockets [ 1 ] . on ( "test" , ( cb ) => cb ( 2 ) ) ;
205+ clientSockets [ 2 ] . on ( "test" , ( _cb ) => {
252206 // do nothing
253207 } ) ;
254208
@@ -390,9 +344,7 @@ describe("@socket.io/postgres-adapter", () => {
390344
391345 servers [ 0 ] . serverSideEmit ( "hello" , "world" , 1 , "2" ) ;
392346
393- servers [ 0 ] . on ( "hello" , ( ) => {
394- done ( new Error ( "should not happen" ) ) ;
395- } ) ;
347+ servers [ 0 ] . on ( "hello" , shouldNotHappen ( done ) ) ;
396348
397349 servers [ 1 ] . on ( "hello" , ( arg1 , arg2 , arg3 ) => {
398350 expect ( arg1 ) . to . eql ( "world" ) ;
@@ -401,9 +353,7 @@ describe("@socket.io/postgres-adapter", () => {
401353 partialDone ( ) ;
402354 } ) ;
403355
404- servers [ 2 ] . of ( "/" ) . on ( "hello" , ( ) => {
405- partialDone ( ) ;
406- } ) ;
356+ servers [ 2 ] . of ( "/" ) . on ( "hello" , ( ) => partialDone ( ) ) ;
407357 } ) ;
408358
409359 it ( "sends an event and receives a response from the other server instances" , ( done ) => {
@@ -415,17 +365,9 @@ describe("@socket.io/postgres-adapter", () => {
415365 done ( ) ;
416366 } ) ;
417367
418- servers [ 0 ] . on ( "hello" , ( ) => {
419- done ( new Error ( "should not happen" ) ) ;
420- } ) ;
421-
422- servers [ 1 ] . on ( "hello" , ( cb ) => {
423- cb ( 2 ) ;
424- } ) ;
425-
426- servers [ 2 ] . on ( "hello" , ( cb ) => {
427- cb ( "3" ) ;
428- } ) ;
368+ servers [ 0 ] . on ( "hello" , shouldNotHappen ( done ) ) ;
369+ servers [ 1 ] . on ( "hello" , ( cb ) => cb ( 2 ) ) ;
370+ servers [ 2 ] . on ( "hello" , ( cb ) => cb ( "3" ) ) ;
429371 } ) ;
430372
431373 it ( "sends an event but timeout if one server does not respond" , ( done ) => {
@@ -440,14 +382,8 @@ describe("@socket.io/postgres-adapter", () => {
440382 done ( ) ;
441383 } ) ;
442384
443- servers [ 0 ] . on ( "hello" , ( ) => {
444- done ( new Error ( "should not happen" ) ) ;
445- } ) ;
446-
447- servers [ 1 ] . on ( "hello" , ( cb ) => {
448- cb ( 2 ) ;
449- } ) ;
450-
385+ servers [ 0 ] . on ( "hello" , shouldNotHappen ( done ) ) ;
386+ servers [ 1 ] . on ( "hello" , ( cb ) => cb ( 2 ) ) ;
451387 servers [ 2 ] . on ( "hello" , ( ) => {
452388 // do nothing
453389 } ) ;
0 commit comments