Skip to content

Commit

Permalink
test(core): improve teardown of servers in ws engine test
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardobridge committed May 29, 2024
1 parent 20a83ee commit c3bcf6c
Showing 1 changed file with 40 additions and 64 deletions.
104 changes: 40 additions & 64 deletions packages/core/test/acceptance/ws-engine.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
'use strict';

const { test } = require('tap');
const { test, beforeEach, afterEach } = require('tap');
const runner = require('../..').runner.runner;
const http = require('http');
const WebSocket = require('ws');

test('should match a websocket response without capture', (t) => {
const server = http.createServer();
const wss = new WebSocket.Server({ server: server });
const targetServer = server.listen(0);
let server;
let wsServer;
beforeEach(async () => {
const httpServer = http.createServer();
wsServer = new WebSocket.Server({ server: httpServer });
server = httpServer.listen(0);
});

afterEach(async () => {
server.close();
});

wss.on('connection', function (ws) {
test('should match a websocket response without capture', (t) => {
wsServer.on('connection', function (ws) {
ws.on('message', function () {
ws.send(JSON.stringify({ foo: 'bar' }));
});
});

const script = {
config: {
target: `ws://127.0.0.1:${targetServer.address().port}`,
target: `ws://127.0.0.1:${server.address().port}`,
phases: [{ duration: 1, arrivalCount: 1 }]
},
scenarios: [
Expand All @@ -43,7 +51,7 @@ test('should match a websocket response without capture', (t) => {
t.equal(c['websocket.messages_sent'], 1, 'All messages should be sent');

ee.stop().then(() => {
targetServer.close(t.end);
t.end();
});
});

Expand All @@ -52,11 +60,7 @@ test('should match a websocket response without capture', (t) => {
});

test('should wait for a websocket response without send', (t) => {
const server = http.createServer();
const wss = new WebSocket.Server({ server: server });
const targetServer = server.listen(0);

wss.on('connection', function (ws) {
wsServer.on('connection', function (ws) {
ws.on('message', function () {
ws.send(JSON.stringify({ bar: 'foo' }));
setTimeout(() => {
Expand All @@ -67,7 +71,7 @@ test('should wait for a websocket response without send', (t) => {

const script = {
config: {
target: `ws://127.0.0.1:${targetServer.address().port}`,
target: `ws://127.0.0.1:${server.address().port}`,
phases: [{ duration: 1, arrivalCount: 1 }]
},
scenarios: [
Expand Down Expand Up @@ -102,7 +106,7 @@ test('should wait for a websocket response without send', (t) => {
);

ee.stop().then(() => {
targetServer.close(t.end);
t.end();
});
});

Expand All @@ -111,19 +115,15 @@ test('should wait for a websocket response without send', (t) => {
});

test('should wait for multiple websocket responses in a loop', (t) => {
const server = http.createServer();
const wss = new WebSocket.Server({ server: server });
const targetServer = server.listen(0);

wss.on('connection', function (ws) {
wsServer.on('connection', function (ws) {
ws.on('message', function () {
ws.send(JSON.stringify({ baz: 'foo' }));
});
});

const script = {
config: {
target: `ws://127.0.0.1:${targetServer.address().port}`,
target: `ws://127.0.0.1:${server.address().port}`,
phases: [{ duration: 1, arrivalCount: 1 }]
},
scenarios: [
Expand Down Expand Up @@ -154,7 +154,7 @@ test('should wait for multiple websocket responses in a loop', (t) => {
t.equal(c['websocket.messages_sent'], 5, 'All messages should be sent');

ee.stop().then(() => {
targetServer.close(t.end);
t.end();
});
});

Expand All @@ -163,11 +163,7 @@ test('should wait for multiple websocket responses in a loop', (t) => {
});

test('should use config.ws.timeout on capture', (t) => {
const server = http.createServer();
const wss = new WebSocket.Server({ server: server });
const targetServer = server.listen(0);

wss.on('connection', function (ws) {
wsServer.on('connection', function (ws) {
ws.on('message', function () {
setTimeout(() => {
ws.send(JSON.stringify({ foo: 'bar' }));
Expand All @@ -177,7 +173,7 @@ test('should use config.ws.timeout on capture', (t) => {

const script = {
config: {
target: `ws://127.0.0.1:${targetServer.address().port}`,
target: `ws://127.0.0.1:${server.address().port}`,
phases: [{ duration: 1, arrivalCount: 1 }],
ws: { timeout: 1 }
},
Expand All @@ -202,7 +198,7 @@ test('should use config.ws.timeout on capture', (t) => {
t.equal(c['vusers.failed'], 1, 'There should be one failure');
t.equal(c['websocket.messages_sent'], 1, 'All messages should be sent');
ee.stop().then(() => {
targetServer.close(t.end);
t.end();
});
});

Expand All @@ -211,11 +207,7 @@ test('should use config.ws.timeout on capture', (t) => {
});

test('should use config.timeout on capture', (t) => {
const server = http.createServer();
const wss = new WebSocket.Server({ server: server });
const targetServer = server.listen(0);

wss.on('connection', function (ws) {
wsServer.on('connection', function (ws) {
ws.on('message', function () {
setTimeout(() => {
ws.send(JSON.stringify({ foo: 'bar' }));
Expand All @@ -225,7 +217,7 @@ test('should use config.timeout on capture', (t) => {

const script = {
config: {
target: `ws://127.0.0.1:${targetServer.address().port}`,
target: `ws://127.0.0.1:${server.address().port}`,
phases: [{ duration: 1, arrivalCount: 1 }],
timeout: 1
},
Expand All @@ -250,7 +242,7 @@ test('should use config.timeout on capture', (t) => {
t.equal(c['vusers.failed'], 1, 'There should be one failure');
t.equal(c['websocket.messages_sent'], 1, 'All messages should be sent');
ee.stop().then(() => {
targetServer.close(t.end);
t.end();
});
});

Expand All @@ -259,19 +251,15 @@ test('should use config.timeout on capture', (t) => {
});

test('should allow an empty string payload to be sent', (t) => {
const server = http.createServer();
const wss = new WebSocket.Server({ server: server });
const targetServer = server.listen(0);

wss.on('connection', function (ws) {
wsServer.on('connection', function (ws) {
ws.on('message', function () {
ws.send(JSON.stringify({ bar: 'baz' }));
});
});

const script = {
config: {
target: `ws://127.0.0.1:${targetServer.address().port}`,
target: `ws://127.0.0.1:${server.address().port}`,
phases: [{ duration: 1, arrivalCount: 1 }]
},
scenarios: [
Expand All @@ -296,7 +284,7 @@ test('should allow an empty string payload to be sent', (t) => {
t.equal(c['websocket.messages_sent'], 1, 'All messages should be sent');

ee.stop().then(() => {
targetServer.close(t.end);
t.end();
});
});

Expand All @@ -305,19 +293,15 @@ test('should allow an empty string payload to be sent', (t) => {
});

test('should allow a simple empty string to be sent', (t) => {
const server = http.createServer();
const wss = new WebSocket.Server({ server: server });
const targetServer = server.listen(0);

wss.on('connection', function (ws) {
wsServer.on('connection', function (ws) {
ws.on('message', function () {
ws.send(JSON.stringify({ bar: 'baz' }));
});
});

const script = {
config: {
target: `ws://127.0.0.1:${targetServer.address().port}`,
target: `ws://127.0.0.1:${server.address().port}`,
phases: [{ duration: 1, arrivalCount: 1 }]
},
scenarios: [
Expand All @@ -339,7 +323,7 @@ test('should allow a simple empty string to be sent', (t) => {
t.equal(c['websocket.messages_sent'], 1, 'All messages should be sent');

ee.stop().then(() => {
targetServer.close(t.end);
t.end();
});
});

Expand All @@ -348,19 +332,15 @@ test('should allow a simple empty string to be sent', (t) => {
});

test('should match allow an undefined variable to be sent', (t) => {
const server = http.createServer();
const wss = new WebSocket.Server({ server: server });
const targetServer = server.listen(0);

wss.on('connection', function (ws) {
wsServer.on('connection', function (ws) {
ws.on('message', function () {
ws.send(JSON.stringify({ baz: 'foo' }));
});
});

const script = {
config: {
target: `ws://127.0.0.1:${targetServer.address().port}`,
target: `ws://127.0.0.1:${server.address().port}`,
phases: [{ duration: 1, arrivalCount: 1 }]
},
scenarios: [
Expand All @@ -382,7 +362,7 @@ test('should match allow an undefined variable to be sent', (t) => {
t.equal(c['websocket.messages_sent'], 1, 'All messages should be sent');

ee.stop().then(() => {
targetServer.close(t.end);
t.end();
});
});

Expand All @@ -391,19 +371,15 @@ test('should match allow an undefined variable to be sent', (t) => {
});

test('should report an error if a step is not valid', (t) => {
const server = http.createServer();
const wss = new WebSocket.Server({ server: server });
const targetServer = server.listen(0);

wss.on('connection', function (ws) {
wsServer.on('connection', function (ws) {
ws.on('message', function () {
ws.send(JSON.stringify({ baz: 'foo' }));
});
});

const script = {
config: {
target: `ws://127.0.0.1:${targetServer.address().port}`,
target: `ws://127.0.0.1:${server.address().port}`,
phases: [{ duration: 1, arrivalCount: 1 }]
},
scenarios: [{ engine: 'ws', flow: [{ sedn: 'test' }] }]
Expand All @@ -416,7 +392,7 @@ test('should report an error if a step is not valid', (t) => {
t.equal(c['errors.invalid_step'], 1, 'There should be one error');

ee.stop().then(() => {
targetServer.close(t.end);
t.end();
});
});

Expand Down

0 comments on commit c3bcf6c

Please sign in to comment.