From 4056bde03b1e123a7ce8145a1d8a8c097fc451bb Mon Sep 17 00:00:00 2001 From: Antti Risteli Date: Tue, 15 Nov 2016 23:58:23 +0200 Subject: [PATCH] [test] Fix Autobahn tests (#894) --- test/autobahn-server.js | 32 ++++++-------------------- test/autobahn.js | 51 ++++++++++++----------------------------- 2 files changed, 22 insertions(+), 61 deletions(-) diff --git a/test/autobahn-server.js b/test/autobahn-server.js index 73655d8b3..7d3e92915 100644 --- a/test/autobahn-server.js +++ b/test/autobahn-server.js @@ -1,31 +1,13 @@ 'use strict'; -var WebSocket = require('../'); -var WebSocketServer = WebSocket.Server; +const WebSocket = require('../'); -process.on('uncaughtException', function (err) { - console.log('Caught exception: ', err, err.stack); +const port = process.argv.length > 2 ? parseInt(process.argv[2]) : 9001; +const wss = new WebSocket.Server({ port }, () => { + console.log(`Listening to port ${port}. Use extra argument to define the port`); }); -process.on('SIGINT', function () { - try { - console.log('Updating reports and shutting down'); - var ws = new WebSocket('ws://localhost:9001/updateReports?agent=ws'); - ws.on('close', function () { - process.exit(); - }); - } catch (e) { - process.exit(); - } -}); - -var wss = new WebSocketServer({port: 8181}); -wss.on('connection', function (ws) { - console.log('new connection'); - ws.on('message', function (data, flags) { - ws.send(flags.buffer, {binary: flags.binary === true}); - }); - ws.on('error', function () { - console.log('error', arguments); - }); +wss.on('connection', (ws) => { + ws.on('message', (data) => ws.send(data)); + ws.on('error', (e) => console.error(e)); }); diff --git a/test/autobahn.js b/test/autobahn.js index dbe3fefd5..cf2492494 100644 --- a/test/autobahn.js +++ b/test/autobahn.js @@ -1,55 +1,34 @@ 'use strict'; -var WebSocket = require('../'); -var currentTest = 1; -var lastTest = -1; -var testCount = null; +const WebSocket = require('../'); -process.on('uncaughtException', function (err) { - console.log('Caught exception: ', err, err.stack); -}); - -process.on('SIGINT', function () { - try { - console.log('Updating reports and shutting down'); - var ws = new WebSocket('ws://localhost:9001/updateReports?agent=ws'); - ws.on('close', function () { - process.exit(); - }); - } catch (e) { - process.exit(); - } -}); +let currentTest = 1; +let testCount; function nextTest () { - var ws; + let ws; - if (currentTest > testCount || (lastTest !== -1 && currentTest > lastTest)) { - console.log('Updating reports and shutting down'); + if (currentTest > testCount) { ws = new WebSocket('ws://localhost:9001/updateReports?agent=ws'); - ws.on('close', function () { - process.exit(); - }); return; } - console.log('Running test case ' + currentTest + '/' + testCount); - ws = new WebSocket('ws://localhost:9001/runCase?case=' + currentTest + '&agent=ws'); - ws.on('message', function (data, flags) { - ws.send(flags.buffer, {binary: flags.binary === true, mask: true}); - }); - ws.on('close', function (data) { - currentTest += 1; + console.log(`Running test case ${currentTest}/${testCount}`); + + ws = new WebSocket(`ws://localhost:9001/runCase?case=${currentTest}&agent=ws`); + ws.on('message', (data) => ws.send(data)); + ws.on('close', () => { + currentTest++; process.nextTick(nextTest); }); - ws.on('error', function (e) {}); + ws.on('error', (e) => console.error(e)); } -var ws = new WebSocket('ws://localhost:9001/getCaseCount'); -ws.on('message', function (data, flags) { +const ws = new WebSocket('ws://localhost:9001/getCaseCount'); +ws.on('message', (data) => { testCount = parseInt(data); }); -ws.on('close', function () { +ws.on('close', () => { if (testCount > 0) { nextTest(); }