gpt4 book ai didi

testing - 我应该如何在 mocha 中测试客户端/服务器 socket.io?

转载 作者:行者123 更新时间:2023-11-28 20:04:05 26 4
gpt4 key购买 nike

How does one properly shutdown socket.io / websocket-client? -- 相关,但已过时/无效

我正在编写一组测试来测试服务器端套接字。从 jamescarr on github 获得代码. Carr 的代码基于 Liam Kaufman 的 here . 传入的文本墙

这是测试端。

ioServer = require("socket.io")
ioClient = require("socket.io-client")

chai.should()
chai.use(sinonChai)
expect = chai.expect

server = require('./testUtils/chat-server.coffee')

socketURL = 'http://localhost:5000'

options =
transports: ['websockets']
'force new connection':true

chatUser1 = name:'Tom'
chatUser2 = name:'Sally'
chatUser3 = name:'Dana'

describe "Chat Server", ->
before (done) ->
server.start 5000, done
after (done) ->
server.stop done

it "Should broadcast new user to all users", (done) ->
console.log "new user to all users"
client1 = ioClient.connect(socketURL)
client1.on "connect", (data) ->
client1.emit "connection name", chatUser1
client2 = ioClient.connect(socketURL, options)
client2.on "connect", (data) ->
client2.emit "connection name", chatUser2

client2.on "new user", (usersName) ->
usersName.should.equal chatUser2.name + " has joined."
client2.disconnect()

numUsers = 0
client1.on "new user", (usersName) ->
numUsers += 1
if numUsers is 2
usersName.should.equal chatUser2.name + " has joined."
client1.disconnect()
done()

这是服务器端。

io = null
clients = {}

module.exports =
start: (port, cb) ->
io = require("socket.io").listen port, cb
io.sockets.on "connection", (socket) ->
console.log "server on connection"
userName = ''
socket.on "connection name", (user) ->
console.log "server on connection name"
clients[user.name] = socket
userName = user.name
io.sockets.emit "new user", user.name + " has joined."

socket.on 'message', (msg) ->
console.log "server on message"
io.sockets.emit 'message', msg

socket.on 'private message', (msg) ->
console.log "server on private message"
fromMsg =
from: userName
txt: msg.txt
clients[msg.to].emit 'private message', fromMsg
stop: (cb) ->
io.server.close()
cb()

出于某种原因,无论我做什么,连接都会超时:

......   info  - socket.io started                                                                                           
new user to all users
.15:55:06
.......................

× 1 of 45 tests failed:

1) Chat Server Should broadcast new user to all users:
Error: timeout of 2000ms exceeded
at Object.<anonymous> (c:\Users\jcollum\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:167:14)
at Timer.list.ontimeout (timers.js:101:19)


- watching debug - emitting heartbeat for client 9VqXYQIv39VLoLVZB-Gw
debug - websocket writing 2::
debug - set heartbeat timeout for client 9VqXYQIv39VLoLVZB-Gw
\ watching

最佳答案

五天了,没有答案所以:

这是我学到的东西。

  1. 任何在签名中带有 (done) 的测试都必须调用 done 否则它将失败 (duh)。
  2. 设置您的回调,将done 调用放入最后一个。
  3. 如果有两个连接并且它们交互,将第二个连接的行为放在第一个连接回调中。
  4. 最后,在测试结束时启动“种子”连接。

关于testing - 我应该如何在 mocha 中测试客户端/服务器 socket.io?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14992257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com