mirror of
https://github.com/Xahau/xahau.js.git
synced 2026-06-05 01:36:47 +00:00
fix: websocket wouldn't close properly on erroring test (#1554)
* fix: websocket wouldn't close properly on erroring test * test: add test
This commit is contained in:
@@ -401,10 +401,10 @@ export class Connection extends EventEmitter {
|
||||
this.emit('error', 'websocket', error.message, error)
|
||||
)
|
||||
// Handle a closed connection: reconnect if it was unexpected
|
||||
this._ws.once('close', (code) => {
|
||||
this._ws.once('close', (code, reason) => {
|
||||
this._clearHeartbeatInterval()
|
||||
this._requestManager.rejectAll(
|
||||
new DisconnectedError('websocket was closed')
|
||||
new DisconnectedError(`websocket was closed, ${reason}`)
|
||||
)
|
||||
this._ws.removeAllListeners()
|
||||
this._ws = null
|
||||
|
||||
@@ -232,7 +232,7 @@ class Client extends EventEmitter {
|
||||
// 4000: Connection uses a 4000 code internally to indicate a manual disconnect/close
|
||||
// Since 4000 is a normal disconnect reason, we convert this to the standard exit code 1000
|
||||
if (finalCode === 4000) {
|
||||
finalCode = 1000
|
||||
finalCode = 1000
|
||||
}
|
||||
this.emit('disconnected', finalCode)
|
||||
})
|
||||
|
||||
@@ -572,4 +572,13 @@ describe('Connection', function () {
|
||||
data: {disconnectIn: 5}
|
||||
})
|
||||
})
|
||||
|
||||
it('should not crash on error', async function (done) {
|
||||
this.mockRippled.suppressOutput = true
|
||||
this.client.connection.request({
|
||||
command: 'test_garbage'
|
||||
})
|
||||
.then(() => new Error('Should not have succeeded'))
|
||||
.catch(done())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -88,6 +88,8 @@ export function createMockRippled(port) {
|
||||
mock.expectedRequests = expectedRequests
|
||||
}
|
||||
|
||||
mock.suppressOutput = false
|
||||
|
||||
mock.on('connection', function (this: MockedWebSocketServer, conn: any) {
|
||||
if (mock.config.breakNextConnection) {
|
||||
mock.config.breakNextConnection = false
|
||||
@@ -106,8 +108,9 @@ export function createMockRippled(port) {
|
||||
mock.emit('request_' + request.command, request, conn)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error: ' + err.message)
|
||||
assert(false, err.message)
|
||||
if (!mock.suppressOutput)
|
||||
console.error('Error: ' + err.message)
|
||||
conn.close(4000, err.message)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user