fix(server): unregister error event listener (#9245)

Co-authored-by: Daniel Roe <daniel@roe.dev>
This commit is contained in:
Matthieu Sieben 2021-05-22 13:42:02 +02:00 committed by GitHub
parent c56d4840ad
commit e687842536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View File

@ -75,8 +75,16 @@ export default class Listener {
// Call server.listen // Call server.listen
try { try {
this.server = await new Promise((resolve, reject) => { this.server = await new Promise((resolve, reject) => {
this._server.on('error', error => reject(error)) this._server.once('error', reject)
const s = this._server.listen(listenArgs, error => error ? reject(error) : resolve(s)) this._server.listen(listenArgs, (error) => {
this._server.off('error', reject)
if (error) {
reject(error)
} else {
resolve(this._server)
}
})
}) })
} catch (error) { } catch (error) {
return this.serverErrorHandler(error) return this.serverErrorHandler(error)

View File

@ -17,7 +17,8 @@ describe('server: listener', () => {
const mockServer = () => { const mockServer = () => {
const server = { const server = {
address: jest.fn(), address: jest.fn(),
on: jest.fn(), once: jest.fn(),
off: jest.fn(),
listen: jest.fn((listenArgs, callback) => { listen: jest.fn((listenArgs, callback) => {
Promise.resolve().then(callback) Promise.resolve().then(callback)
return server return server
@ -73,8 +74,8 @@ describe('server: listener', () => {
expect(http.createServer).toBeCalledTimes(1) expect(http.createServer).toBeCalledTimes(1)
expect(http.createServer).toBeCalledWith(options.app) expect(http.createServer).toBeCalledWith(options.app)
expect(server.on).toBeCalledTimes(1) expect(server.once).toBeCalledTimes(1)
expect(server.on).toBeCalledWith('error', expect.any(Function)) expect(server.once).toBeCalledWith('error', expect.any(Function))
expect(server.listen).toBeCalledTimes(1) expect(server.listen).toBeCalledTimes(1)
expect(server.listen).toBeCalledWith( expect(server.listen).toBeCalledWith(
{ {
@ -111,8 +112,8 @@ describe('server: listener', () => {
expect(https.createServer).toBeCalledTimes(1) expect(https.createServer).toBeCalledTimes(1)
expect(https.createServer).toBeCalledWith(options.https, options.app) expect(https.createServer).toBeCalledWith(options.https, options.app)
expect(server.on).toBeCalledTimes(1) expect(server.once).toBeCalledTimes(1)
expect(server.on).toBeCalledWith('error', expect.any(Function)) expect(server.once).toBeCalledWith('error', expect.any(Function))
expect(server.listen).toBeCalledTimes(1) expect(server.listen).toBeCalledTimes(1)
expect(server.listen).toBeCalledWith( expect(server.listen).toBeCalledWith(
{ {
@ -150,8 +151,8 @@ describe('server: listener', () => {
expect(http.createServer).toBeCalledTimes(1) expect(http.createServer).toBeCalledTimes(1)
expect(http.createServer).toBeCalledWith(options.app) expect(http.createServer).toBeCalledWith(options.app)
expect(server.on).toBeCalledTimes(1) expect(server.once).toBeCalledTimes(1)
expect(server.on).toBeCalledWith('error', expect.any(Function)) expect(server.once).toBeCalledWith('error', expect.any(Function))
expect(server.listen).toBeCalledTimes(1) expect(server.listen).toBeCalledTimes(1)
expect(server.listen).toBeCalledWith( expect(server.listen).toBeCalledWith(
{ {
@ -204,7 +205,7 @@ describe('server: listener', () => {
const serverError = new Error('error occurred') const serverError = new Error('error occurred')
server.listen.mockImplementationOnce((listenArgs, callback) => { server.listen.mockImplementationOnce((listenArgs, callback) => {
Promise.resolve().then(callback) Promise.resolve().then(callback)
const errorListener = server.on.mock.calls[0][1] const errorListener = server.once.mock.calls[0][1]
errorListener(serverError) errorListener(serverError)
return server return server
}) })