Mocha 测试 socketio 中间件不会完成套件

Mocha testing socketio middleware won't finish suite

我正在使用 mocha 在 nodejs 8.9 上测试带有服务器和客户端组件的 nodejs-app。

为了正确结束 mocha,我必须确保在测试 运行 之后关闭所有 socketio 和 http 服务器。这在正常测试中工作正常,但是一旦我向 socketio-server 注册一个中间件,mocha-process 就不会关闭并永远保持打开状态。

测试代码(在第二次测试中评论查看问题,运行 via mocha test.spec.js):

// test.spec.js
'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 test works perfectly, even when I copy it and run it
    // multiple times in this suite
    it('should connect to a socketio-server', (done) => {
        this._ioserver.on('connection', () => {
            client.close()
            done()
        })
        const client = ioclient.connect(`http://localhost:${NODE_PORT}`)
    })

    // this test also finished, but the suite hangs afterwards - as if
    // a socket-client or socket-server was not closed properly.
    it('should finish the test suite even with a middleware', (done) => {
        this._ioserver.use((socket, next) => {
            return next()
        })

        this._ioserver.on('connection', () => {
            client.close()
            done()
        })

        const client = ioclient.connect(`http://localhost:${NODE_PORT}`)
    })

    afterEach(() => {
        this._ioserver.close()
        this._http.close()
    })
})

知道为什么会这样吗?

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

此行为阻止节点正确销毁所有对象,这反过来又解释了挂起。解决方案是仅在 客户端 声明连接打开后才调用 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()
    })
})