@@ -510,24 +510,32 @@ describe('WebSocket API Live Tests', function () {
510510 let executionReceived = false ;
511511 let balanceReceived = false ;
512512 let subscriptionReady = false ;
513+ let finished = false ;
514+
515+ const finish = ( err ?: Error ) => {
516+ if ( finished ) return ;
517+ finished = true ;
518+ stopWsApiConnections ( true ) ;
519+ if ( err ) return done ( err ) ;
520+ done ( ) ;
521+ } ;
522+
523+ const checkCompletion = ( ) => {
524+ if ( executionReceived && balanceReceived && subscriptionReady ) {
525+ console . log ( 'Both execution and balance events received!' ) ;
526+ setTimeout ( ( ) => finish ( ) , 2000 ) ;
527+ }
528+ } ;
513529
514530 binance . websockets . userData (
515531 ( data ) => {
516532 // All updates callback
517533 console . log ( 'Event received:' , data . e , data ) ;
518-
519- // Check if we received both events
520- if ( executionReceived && balanceReceived && subscriptionReady ) {
521- console . log ( '✅ Both execution and balance events received!' ) ;
522- setTimeout ( ( ) => {
523- stopWsApiConnections ( true ) ;
524- done ( ) ;
525- } , 2000 ) ;
526- }
534+ checkCompletion ( ) ;
527535 } ,
528536 ( balance ) => {
529537 // Balance callback
530- console . log ( '📊 Balance update received:' , balance ) ;
538+ console . log ( 'Balance update received:' , balance ) ;
531539 balanceReceived = true ;
532540
533541 assert ( balance !== null , WARN_SHOULD_BE_NOT_NULL ) ;
@@ -548,18 +556,11 @@ describe('WebSocket API Live Tests', function () {
548556 assert ( Array . isArray ( balance . B ) , 'Balances should be an array' ) ;
549557 }
550558
551- // Check if both events received
552- if ( executionReceived && balanceReceived && subscriptionReady ) {
553- console . log ( '✅ Both execution and balance events received!' ) ;
554- setTimeout ( ( ) => {
555- stopWsApiConnections ( true ) ;
556- done ( ) ;
557- } , 2000 ) ;
558- }
559+ checkCompletion ( ) ;
559560 } ,
560561 ( execution ) => {
561562 // Execution callback
562- console . log ( '📈 Execution report received:' , execution ) ;
563+ console . log ( 'Execution report received:' , execution ) ;
563564 executionReceived = true ;
564565
565566 assert ( execution !== null , WARN_SHOULD_BE_NOT_NULL ) ;
@@ -579,14 +580,7 @@ describe('WebSocket API Live Tests', function () {
579580 console . log ( ` Execution Type: ${ execution . x } ` ) ;
580581 console . log ( ` Order Status: ${ execution . X } ` ) ;
581582
582- // Check if both events received
583- if ( executionReceived && balanceReceived && subscriptionReady ) {
584- console . log ( '✅ Both execution and balance events received!' ) ;
585- setTimeout ( ( ) => {
586- stopWsApiConnections ( true ) ;
587- done ( ) ;
588- } , 2000 ) ;
589- }
583+ checkCompletion ( ) ;
590584 } ,
591585 async ( endpoint ) => {
592586 // Subscribed callback
@@ -617,18 +611,16 @@ describe('WebSocket API Live Tests', function () {
617611 // Set a backup timeout in case events are not received
618612 setTimeout ( ( ) => {
619613 if ( ! executionReceived || ! balanceReceived ) {
620- console . error ( '⚠️ Timeout: Not all events received' ) ;
614+ console . error ( 'Timeout: Not all events received' ) ;
621615 console . error ( ` Execution received: ${ executionReceived } ` ) ;
622616 console . error ( ` Balance received: ${ balanceReceived } ` ) ;
623- stopWsApiConnections ( true ) ;
624- done ( new Error ( 'Did not receive all expected events within timeout' ) ) ;
617+ finish ( new Error ( 'Did not receive all expected events within timeout' ) ) ;
625618 }
626619 } , 25000 ) ; // 25 second timeout
627620
628621 } catch ( error : any ) {
629622 console . error ( 'Error creating order:' , error . message ) ;
630- stopWsApiConnections ( true ) ;
631- done ( error ) ;
623+ finish ( error ) ;
632624 }
633625 } ,
634626 ( listStatus ) => {
0 commit comments