Node.js Mocha测试socketio中间件赢得';t完成套房

Node.js Mocha测试socketio中间件赢得';t完成套房,node.js,socket.io,mocha.js,middleware,Node.js,Socket.io,Mocha.js,Middleware,我正在用mocha在nodejs 8.9上测试一个带有服务器和客户端组件的nodejs应用程序 为了让mocha正常结束,我必须确保在测试运行后关闭所有socketio和http服务器。这在正常测试中运行良好,但只要我向socketio服务器注册一个中间件,mocha进程就不会永远关闭和保持打开状态 Testcode(第二个测试中的注释以查看问题,通过mocha test.spec.js)运行): 知道为什么会发生这种情况吗?所以,问题是,服务器在成功连接事件时关闭了客户端连接。客户机没有得到任

我正在用mocha在nodejs 8.9上测试一个带有服务器和客户端组件的nodejs应用程序

为了让mocha正常结束,我必须确保在测试运行后关闭所有socketio和http服务器。这在正常测试中运行良好,但只要我向socketio服务器注册一个中间件,mocha进程就不会永远关闭和保持打开状态

Testcode(第二个测试中的注释以查看问题,通过
mocha test.spec.js
)运行):


知道为什么会发生这种情况吗?

所以,问题是,服务器在成功连接事件时关闭了客户端连接。客户机没有得到任何关于这方面的信息,而是看到一个失败的连接并试图重新连接。这再次打开了服务器的套接字,因为服务器已经关闭,所以连接错误不断出现

此行为阻止节点正确销毁所有对象,从而解释了挂起的原因。解决方案是仅在客户端声明连接已打开后调用
done()
,而不是在服务器声明连接已打开后调用,如下所示:

'use strict'

const Express = require('express')
const Http = require('http')
const ioserver = require('socket.io')
const ioclient = require('socket.io-client')

const NODE_PORT = process.env.NODE_PORT || 3000

describe('Client', function () {
    beforeEach(() => {
        const express = new Express()
        this._http = Http.Server(express)
        this._ioserver = ioserver(this._http)
        this._http.listen(NODE_PORT)
        this._client = null
    })

    it('should connect to a socketio-server', (done) => {
        this._ioserver.on('connection', () => {
            done()
        })

        this._client = ioclient.connect(`http://localhost:${NODE_PORT}`)
    })

    it('should finish the test suite even with a middleware', (done) => {
        this._ioserver.use((socket, next) => {
            return next()
        })

        this._client = ioclient.connect(`http://localhost:${NODE_PORT}`)

        // if we wait for the server and kill the server socket,
        // the client will try to reconnect and won't be killed
        // by mocha.
        this._client.on('connect', () => {
            done()
        })
    })

    afterEach(() => {
        // this last call forces the client to stop connecting
        // even if tests failed
        this._client.close()

        this._ioserver.close()
        this._http.close()
    })
})

我使用了
client.disconnect()
而不是
close()
也许这就是原因。这两个是同义词,调用相同的方法打开了bugreport:
'use strict'

const Express = require('express')
const Http = require('http')
const ioserver = require('socket.io')
const ioclient = require('socket.io-client')

const NODE_PORT = process.env.NODE_PORT || 3000

describe('Client', function () {
    beforeEach(() => {
        const express = new Express()
        this._http = Http.Server(express)
        this._ioserver = ioserver(this._http)
        this._http.listen(NODE_PORT)
        this._client = null
    })

    it('should connect to a socketio-server', (done) => {
        this._ioserver.on('connection', () => {
            done()
        })

        this._client = ioclient.connect(`http://localhost:${NODE_PORT}`)
    })

    it('should finish the test suite even with a middleware', (done) => {
        this._ioserver.use((socket, next) => {
            return next()
        })

        this._client = ioclient.connect(`http://localhost:${NODE_PORT}`)

        // if we wait for the server and kill the server socket,
        // the client will try to reconnect and won't be killed
        // by mocha.
        this._client.on('connect', () => {
            done()
        })
    })

    afterEach(() => {
        // this last call forces the client to stop connecting
        // even if tests failed
        this._client.close()

        this._ioserver.close()
        this._http.close()
    })
})