Make tests more interesting

This commit is contained in:
Alex Hultman 2019-01-26 14:52:43 +01:00
parent f41c4e2a60
commit 282609bfb4
2 changed files with 51 additions and 23 deletions

View File

@ -17,25 +17,17 @@ let closedClientConnections = 0;
let listenSocket; let listenSocket;
/* 0 to 1-less than max */
function getRandomInt(max) { function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max)); return Math.floor(Math.random() * Math.floor(max));
} }
/* Perform random websockets/ws action */
function performRandomClientAction(ws) {
uWS.print('Random client action');
ws.send('test message from client');
uWS.print('Random client action, done');
}
/* Perform random uWebSockets.js action */
function establishNewConnection() { function establishNewConnection() {
const ws = new WebSocket('ws://localhost:' + port); const ws = new WebSocket('ws://localhost:' + port);
ws.on('open', () => { ws.on('open', () => {
/* Open more connections */ /* Open more connections */
if (++openedClientConnections < 1) { if (++openedClientConnections < 1000) {
establishNewConnection(); establishNewConnection();
} else { } else {
/* Stop listening */ /* Stop listening */
@ -46,8 +38,8 @@ function establishNewConnection() {
performRandomClientAction(ws); performRandomClientAction(ws);
}); });
/* It seems you can get messages after close?! */
ws.on('message', (data) => { ws.on('message', (data) => {
console.log('client got message: ' + data);
performRandomClientAction(ws); performRandomClientAction(ws);
}); });
@ -57,20 +49,51 @@ function establishNewConnection() {
}); });
} }
/* Perform random websockets/ws action */
function performRandomServerAction(ws) { function performRandomClientAction(ws) {
//uWS.print('Random server action'); /* 0, 1 but never 2 */
switch (getRandomInt(1)) { switch (getRandomInt(2)) {
case 0: { case 0: {
ws.close(); ws.close();
break; break;
} }
case 1: { case 1: {
ws.send('a test message', false); /* Length should be random from small to huge */
try {
ws.send('a test message');
} catch (e) {
/* Apparently websockets/ws can throw at any moment */
}
break;
}
case 2: {
/* This should correspond to hard us_socket_close */
ws.terminate();
break;
}
}
}
/* Perform random uWebSockets.js action */
function performRandomServerAction(ws) {
/* 0, 1 but never 2 */
switch (getRandomInt(2)) {
case 0: {
ws.close();
break;
}
case 1: {
/* Length should be random from small to huge */
ws.send('a test message', false);
break;
}
case 2: {
/* This should correspond to hard us_socket_close */
ws.terminate();
break; break;
} }
} }
//uWS.print('Done with random server action');
} }
const app = uWS./*SSL*/App({ const app = uWS./*SSL*/App({
@ -83,12 +106,10 @@ const app = uWS./*SSL*/App({
idleTimeout: 10, idleTimeout: 10,
open: (ws, req) => { open: (ws, req) => {
uWS.print('Server open event'); /* Doing a terminate here will be interesting */
performRandomServerAction(ws); performRandomServerAction(ws);
uWS.print('Server open event, returning');
}, },
message: (ws, message, isBinary) => { message: (ws, message, isBinary) => {
console.log('server got message: ' + message.byteLength);
performRandomServerAction(ws); performRandomServerAction(ws);
}, },
drain: (ws) => { drain: (ws) => {
@ -96,8 +117,15 @@ const app = uWS./*SSL*/App({
//performRandomServerAction(ws); //performRandomServerAction(ws);
}, },
close: (ws, code, message) => { close: (ws, code, message) => {
console.log('server was closed'); try {
//performRandomServerAction(ws); performRandomServerAction(ws);
} catch (e) {
/* We expect to land here always, since socket is invalid */
return;
}
/* We should never land here */
uWS.print('ERROR: Did not throw in close!');
process.exit(-1);
} }
}).any('/*', (res, req) => { }).any('/*', (res, req) => {
res.end('Nothing to see here!'); res.end('Nothing to see here!');

@ -1 +1 @@
Subproject commit d331ec3e458ad04b53c27baf5d53675fe105c9b0 Subproject commit 80322a5af73ed97b739315327e2cc5af65bc2f1e