Skip to content

Commit c66e383

Browse files
committed
More thorough testing, with ws.terminate
1 parent adc5324 commit c66e383

3 files changed

Lines changed: 53 additions & 19 deletions

File tree

‎src/WebSocketWrapper.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ struct WebSocketWrapper {
2121
args.Holder()->SetAlignedPointerInInternalField(0, nullptr);
2222
}
2323

24+
/* It would make sense to call terminate "close" and call close "end" to line up with HTTP */
25+
/* That also makes sense seince close takes message and code -> you can end with a string message */
26+
27+
/* Takes nothing returns nothing */
28+
template <bool SSL>
29+
static void uWS_WebSocket_terminate(const FunctionCallbackInfo<Value> &args) {
30+
auto *ws = getWebSocket<SSL>(args);
31+
if (ws) {
32+
invalidateWsObject(args);
33+
ws->terminate();
34+
}
35+
}
36+
2437
/* Takes code, message, returns undefined */
2538
template <bool SSL>
2639
static void uWS_WebSocket_close(const FunctionCallbackInfo<Value> &args) {
@@ -80,6 +93,7 @@ struct WebSocketWrapper {
8093
/* Register our functions */
8194
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send"), FunctionTemplate::New(isolate, uWS_WebSocket_send<SSL>));
8295
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "close"), FunctionTemplate::New(isolate, uWS_WebSocket_close<SSL>));
96+
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "terminate"), FunctionTemplate::New(isolate, uWS_WebSocket_terminate<SSL>));
8397
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getBufferedAmount"), FunctionTemplate::New(isolate, uWS_WebSocket_getBufferedAmount<SSL>));
8498

8599
/* Create the template */

‎tests/Hammer.js‎

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,41 @@ const port = 9001;
1515
let openedClientConnections = 0;
1616
let closedClientConnections = 0;
1717

18+
let listenSocket;
19+
20+
const maxBackpressure = 50 * 1024 * 1024;
21+
const maxPayloadSize = 5 * 1024 * 1024;
22+
const largeBuffer = new ArrayBuffer(maxPayloadSize);
23+
1824
function printStatistics() {
1925
console.log('Opened clients ' + openedClientConnections);
2026
console.log('Closed clients ' + closedClientConnections + '\n');
2127
}
2228

23-
let listenSocket;
24-
2529
/* 0 to 1-less than max */
2630
function getRandomInt(max) {
2731
return Math.floor(Math.random() * Math.floor(max));
2832
}
2933

34+
/* This gets called every time we either open a new connection,
35+
* or it immediately land in websockets/ws error handler */
36+
function accountForConnection() {
37+
/* Open more connections */
38+
if (++openedClientConnections < /*100*/ 1000) {
39+
establishNewConnection();
40+
} else {
41+
/* Stop listening */
42+
uWS.us_listen_socket_close(listenSocket);
43+
}
44+
}
45+
3046
function establishNewConnection() {
3147
const ws = new WebSocket('ws://localhost:' + port);
3248

3349
ws.on('open', () => {
34-
/* Open more connections */
35-
if (++openedClientConnections < 100) {
36-
establishNewConnection();
37-
} else {
38-
/* Stop listening */
39-
uWS.us_listen_socket_close(listenSocket);
40-
}
50+
/* Mark this socket as opened */
51+
ws._opened = true;
52+
accountForConnection();
4153

4254
printStatistics();
4355
performRandomClientAction(ws);
@@ -48,21 +60,30 @@ function establishNewConnection() {
4860
performRandomClientAction(ws);
4961
});
5062

63+
/* Close can be called more times than open, websockets/ws is really messy! */
5164
ws.on('close', () => {
65+
/* Check if this websocket really was opened first */
66+
if (!ws._opened) {
67+
accountForConnection();
68+
}
69+
5270
closedClientConnections++;
5371
printStatistics();
72+
/* I guess there is no need for us to call anything
73+
* here, websockets/ws will not send anything to us anyways */
5474
//performRandomClientAction(ws);
5575
});
56-
}
5776

58-
const maxBackpressure = 50 * 1024 * 1024;
59-
const maxPayloadSize = 5 * 1024 * 1024;
60-
const largeBuffer = new ArrayBuffer(maxPayloadSize);
77+
ws.on('error', () => {
78+
/* We simply ignore errors. websockets/ws will call our close handler
79+
* on errors, potentially before even calling the open handler */
80+
});
81+
}
6182

6283
/* Perform random websockets/ws action */
6384
function performRandomClientAction(ws) {
64-
/* 0, 1 but never 2 */
65-
let action = getRandomInt(2);
85+
/* 0, 1, 2 but never 3 */
86+
let action = getRandomInt(3);
6687

6788
/* Sending a message should have higher probability */
6889
if (getRandomInt(100) < 80) {
@@ -85,7 +106,6 @@ function performRandomClientAction(ws) {
85106
break;
86107
}
87108
case 2: {
88-
/* This should correspond to hard us_socket_close */
89109
ws.terminate();
90110
break;
91111
}
@@ -94,7 +114,8 @@ function performRandomClientAction(ws) {
94114

95115
/* Perform random uWebSockets.js action */
96116
function performRandomServerAction(ws, uniform) {
97-
let action = getRandomInt(2);
117+
/* 0, 1, 2 but never 3 */
118+
let action = getRandomInt(3);
98119

99120
/* Sending a message should have higher probability,
100121
* except for when we are draining. */
@@ -104,7 +125,6 @@ function performRandomServerAction(ws, uniform) {
104125
}
105126
}
106127

107-
/* 0, 1 but never 2 */
108128
switch (action) {
109129
case 0: {
110130
ws.close();

‎uWebSockets‎

0 commit comments

Comments
 (0)