File tree Expand file tree Collapse file tree 3 files changed +30
-3
lines changed Expand file tree Collapse file tree 3 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -228,6 +228,27 @@ describe('services/socket', () => {
228228 expect ( ioServerUseStub ) . to . calledTwice ;
229229 } ) ;
230230
231+ it ( 'should correctly start microservice with async io params' , async ( ) => {
232+ const listenStub = sandbox . stub ( ms . getIoServer ( ) , 'listen' ) ;
233+
234+ // Configure io server stubs
235+ const ioServerUseStub = sandbox . stub ( ms . getIoServer ( ) , 'use' ) ;
236+ const ioServerOnStub = sandbox . stub ( ms . getIoServer ( ) , 'on' ) ;
237+
238+ ms . setParams ( {
239+ ioServerOptions : ( ) => ioServerOptions ,
240+ } ) ;
241+
242+ // Skip startWorkers
243+ sandbox . stub ( axios , 'request' ) . rejects ( new Error ( 'ECONNREFUSED' ) ) ;
244+
245+ await ms . start ( ) ;
246+
247+ expect ( listenStub ) . to . calledOnceWith ( ms . getHttpServer ( ) , ioServerOptions ) ;
248+ expect ( ioServerOnStub ) . to . calledOnce ;
249+ expect ( ioServerUseStub ) . to . calledTwice ;
250+ } ) ;
251+
231252 it ( 'should correctly apply connection middlewares' , async ( ) => {
232253 const socket = { handshake : { headers : { } } , data : { } } as IoSocket ;
233254 const nextStub = sandbox . stub ( ) ;
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import type {
77 IAbstractMicroserviceParams ,
88} from '@interfaces/services/i-abstract-microservice' ;
99import type { TJsonRPC } from '@interfaces/services/i-gateway' ;
10+ import type SocketMs from '@services/socket' ;
1011
1112export interface ISocketOptions extends IAbstractMicroserviceOptions {
1213 listener : string ;
@@ -16,7 +17,9 @@ export interface ISocketOptions extends IAbstractMicroserviceOptions {
1617}
1718
1819export interface ISocketParams extends IAbstractMicroserviceParams {
19- ioServerOptions ?: Partial < ServerOptions > ;
20+ ioServerOptions ?:
21+ | Partial < ServerOptions >
22+ | ( ( ms : SocketMs ) => Partial < ServerOptions > | Promise < Partial < ServerOptions > > ) ;
2023 signRoomOptions : { secretKey : string } ;
2124 makeRoomName ?: (
2225 roomName : string ,
Original file line number Diff line number Diff line change @@ -533,15 +533,18 @@ class Socket extends AbstractMicroservice {
533533 /**
534534 * Run microservice
535535 */
536- public start ( ) : Promise < void | void [ ] > {
536+ public async start ( ) : Promise < void | void [ ] > {
537537 const { name, version, listener, eventWorkers } = this . options ;
538538 const { ioServerOptions } = this . params ;
539539 const [ host , port ] = listener . split ( ':' ) ;
540540
541541 this . logDriver ( ( ) => `${ name } start. Version: ${ version } ` , LogType . INFO ) ;
542542
543543 this . configureIoServer ( ) ;
544- this . ioServer . listen ( this . httpServer , ioServerOptions ) ;
544+ this . ioServer . listen (
545+ this . httpServer ,
546+ typeof ioServerOptions === 'function' ? await ioServerOptions ( this ) : ioServerOptions ,
547+ ) ;
545548
546549 const server = this . httpServer . listen ( Number ( port ) , host , ( ) => {
547550 this . logDriver (
You can’t perform that action at this time.
0 commit comments