集成测试 node.js socket.io 服务器
Posted
技术标签:
【中文标题】集成测试 node.js socket.io 服务器【英文标题】:Integration testing a node.js socket.io server 【发布时间】:2019-06-20 19:19:20 【问题描述】:我正在尝试设置服务器以使用 nodejs、express 和 socket.io 响应 socket.io 客户端。我想编写测试来探测服务器并确保它正确处理事件并向客户端发送适当的响应。
我尝试使用 jest 编写一些自动化测试,但我不知道如何实际向服务器发出事件并让它响应。
Unit testing Node.js and WebSockets (Socket.io)
我查看了上面的帖子,但它对我不起作用......
【问题讨论】:
Unit testing Node.js and WebSockets (Socket.io)的可能重复 不重复,上面的注释是UNIT测试,这个Q是INTEGRATION测试。 【参考方案1】:查看这个基于承诺和良好实践的样板解决方案。 您可以用它测试您的服务器的整个 io 事件,毫不费力。 您只需要复制样板测试并根据需要添加自己的代码。
查看 GitHub 上的存储库以获取完整源代码。
https://github.com/PatMan10/testing_socketIO_server
const io = require("socket.io-client");
const ev = require("../utils/events");
const logger = require("../utils/logger");
// initSocket returns a promise
// success: resolve a new socket object
// fail: reject a error
const initSocket = () =>
return new Promise((resolve, reject) =>
// create socket for communication
const socket = io("localhost:5000",
"reconnection delay": 0,
"reopen delay": 0,
"force new connection": true
);
// define event handler for sucessfull connection
socket.on(ev.CONNECT, () =>
logger.info("connected");
resolve(socket);
);
// if connection takes longer than 5 seconds throw error
setTimeout(() =>
reject(new Error("Failed to connect wihtin 5 seconds."));
, 5000);
);
;
// destroySocket returns a promise
// success: resolve true
// fail: reject false
const destroySocket = socket =>
return new Promise((resolve, reject) =>
// check if socket connected
if (socket.connected)
// disconnect socket
logger.info("disconnecting...");
socket.disconnect();
resolve(true);
else
// not connected
logger.info("no connection to break...");
resolve(false);
);
;
describe("test suit: Echo & Bello", () =>
test("test: ECHO", async () =>
try
// create socket for communication
const socketClient = await initSocket();
// create new promise for server response
const serverResponse = new Promise((resolve, reject) =>
// define a handler for the test event
socketClient.on(ev.res_ECHO, data4Client =>
//process data received from server
const message = data4Client;
logger.info("Server says: " + message);
// destroy socket after server responds
destroySocket(socketClient);
// return data for testing
resolve(data4Client);
);
// if response takes longer than 5 seconds throw error
setTimeout(() =>
reject(new Error("Failed to get reponse, connection timed out..."));
, 5000);
);
// define data 4 server
const data4Server = message: "CLIENT ECHO" ;
// emit event with data to server
logger.info("Emitting ECHO event");
socketClient.emit(ev.com_ECHO, data4Server);
// wait for server to respond
const status, message = await serverResponse;
expect(status).toBe(200);
expect(message).toBe("SERVER ECHO");
catch (error)
logger.error(error);
);
test("test BELLO", async () =>
try
const socketClient = await initSocket();
const serverResponse = new Promise((resolve, reject) =>
socketClient.on(ev.res_BELLO, data4Client =>
const message = data4Client;
logger.info("Server says: " + message);
destroySocket(socketClient);
resolve(data4Client);
);
setTimeout(() =>
reject(new Error("Failed to get reponse, connection timed out..."));
, 5000);
);
const data4Server = message: "CLIENT BELLO" ;
logger.info("Emitting BELLO event");
socketClient.emit(ev.com_BELLO, data4Server);
const status, message = await serverResponse;
expect(status).toBe(200);
expect(message).toBe("SERVER BELLO");
catch (error)
logger.error(error);
);
);
【讨论】:
以上是关于集成测试 node.js socket.io 服务器的主要内容,如果未能解决你的问题,请参考以下文章
尝试在专用服务器上实现 Node.js 和 Socket.io
node.js 和 socket.io。 websocket的传输类型配置?
os x 上的 node.js socket.io 服务器无法连接超过 120 个客户端