2017-11-03 13 views
0

nodejs 8.9의 서버 및 클라이언트 구성 요소로 moza를 사용하여 nodejs-app를 테스트하고 있습니다.모카 테스트 socketio 미들웨어가 패키지를 완성하지 못합니다

모카를 제대로 종료하려면 테스트를 실행 한 후에 모든 소켓과 http 서버를 닫아야합니다. 정상적인 테스트에서는 정상적으로 작동하지만 socketio-server에 미들웨어를 등록하자마자 mocha 프로세스가 닫히지 않고 영원히 열리게됩니다.

Testcode (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() 
    }) 
}) 

어떤 아이디어?

+0

을 내가 ('client.disconnect를 사용하는'대신 :이 솔루션은 서버가과 같이 열 연결을 선언 한 후 클라이언트가 열려 연결을 선언 한 후에 만 ​​done() 전화를 하지입니다 close()'아마도 이것이 원인 일 수 있습니다. –

+0

그 두 가지는 동의어이며 동일한 메소드를 호출합니다. – Lars

+0

다음의 버그 보고서를 열었습니다 : https://github.com/mochajs/mocha/issues/3095 – Lars

답변

0

그래서 문제는 서버가 성공적인 연결 이벤트에서 클라이언트 연결을 닫았습니다. 클라이언트는 그 정보를 얻지 못했지만 연결 실패를보고 다시 연결하려고했습니다. 이것은 서버에 대한 소켓을 다시 열었고 서버가 이미 닫혀 있었기 때문에 연결 오류가 계속 발생했습니다.

이 동작으로 인해 노드가 모든 개체를 적절하게 파괴하지 못하게되어 매달린 것이 설명됩니다. `)

'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() 
    }) 
})