Skip to content

Commit 130f5e8

Browse files
committed
nit pick tests
1 parent 0d378db commit 130f5e8

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"ws-tests-spot": "mocha ./tests/binance-ws-spot.test.ts --exit",
3434
"ws-tests-futures": "mocha ./tests/binance-ws-futures.test.ts --exit",
3535
"ws-api-userdata-tests": "mocha ./tests/binance-ws-api-userdata.test.ts --exit",
36-
"ws-live-tests": "mocha ./tests/binance-ws-spot.test.ts ./tests/binance-ws-futures.test.ts ./tests/binance-ws-api-userdata.test.ts --exit",
36+
"ws-live-tests": "mocha ./tests/binance-ws-spot.test.ts ./tests/binance-ws-futures.test.ts ./tests/binance-ws-api-userdata.test.ts ./tests/binance-ws-api-ticker.test.ts --exit",
3737
"test-debug": "mocha --inspect-brk",
3838
"lint": "eslint src/",
3939
"cover": "istanbul cover _mocha --report lcovonly",

src/node-binance-api.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,11 +1554,22 @@ export default class Binance {
15541554

15551555
ws.on('open', () => {
15561556
if (this.Options.verbose) this.Options.log('WebSocket API: Connected to ' + this.getWsApiUrl());
1557-
this.handleSocketOpen(ws, null);
1557+
(ws as any).isAlive = true;
1558+
});
1559+
ws.on('pong', () => { (ws as any).isAlive = true; });
1560+
ws.on('error', (err) => {
1561+
this.Options.log('WebSocket API error: ' + (ws as any).connectionId + ' error: ' + err.message);
1562+
});
1563+
ws.on('close', (code, reason) => {
1564+
if (this.Options.verbose) this.Options.log('WebSocket API closed: ' + (ws as any).connectionId +
1565+
(code ? ' (' + code + ')' : '') +
1566+
(reason ? ' ' + reason : ''));
1567+
delete this.wsApiConnections[connectionId];
1568+
if ((ws as any).reconnect && typeof reconnect === 'function') {
1569+
if (this.Options.verbose) this.Options.log('WebSocket API reconnecting: ' + connectionId);
1570+
reconnect();
1571+
}
15581572
});
1559-
ws.on('pong', this.handleSocketHeartbeat.bind(this, ws));
1560-
ws.on('error', this.handleSocketError.bind(this, ws));
1561-
ws.on('close', this.handleSocketClose.bind(this, ws, reconnect));
15621573
ws.on('message', data => {
15631574
try {
15641575
if (this.Options.verbose) this.Options.log('WebSocket API data:', data);
@@ -1659,9 +1670,10 @@ export default class Binance {
16591670
delete this.wsApiConnections[connectionId];
16601671

16611672
// Reject all pending requests for this connection
1662-
for (const requestId in this.wsApiPendingRequests) {
1673+
const pendingIds = Object.keys(this.wsApiPendingRequests);
1674+
for (const requestId of pendingIds) {
16631675
const pending = this.wsApiPendingRequests[requestId];
1664-
if (pending.connectionId === connectionId) {
1676+
if (pending && pending.connectionId === connectionId) {
16651677
pending.reject(new Error('WebSocket API connection terminated'));
16661678
}
16671679
}

tests/binance-ws-api-userdata.test.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)