Skip to content

Commit

Permalink
close always emit close now
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Oct 5, 2024
1 parent c68d96e commit 31d105d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,21 @@ module.exports = class UDXSocket extends events.EventEmitter {

async close () {
if (this._closing) return this._closing
this._closing = (this._inited && this.idle) ? new Promise(resolve => this.once('close', resolve)) : Promise.resolve()
this._closing = new Promise(resolve => this.once('close', resolve))
this._closeMaybe()
return this._closing
}

_closeMaybe () {
if (!this._closed && this._inited && this.closing && this.idle) {
if (this._closed || this._closing === null) return this._closed

if (!this._inited) {
this._closed = true
this.emit('close')
return true
}

if (this.idle) {
binding.udx_napi_socket_close(this._handle)
this._closed = true
}
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function uncaught (fn) {
function createSocket (t, udx, opts) {
const socket = udx.createSocket(opts)
const closed = new Promise(resolve => socket.once('close', resolve))
t.teardown(() => socket.bound && closed, { order: Infinity })
t.teardown(() => closed, { order: Infinity })
return socket
}

Expand Down
6 changes: 5 additions & 1 deletion test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,11 @@ test('connect to invalid host ip', async function (t) {
}

s.destroy()

await a.close()
})

test('bind to invalid host ip', function (t) {
test('bind to invalid host ip', async function (t) {
t.plan(1)

const u = new UDX()
Expand All @@ -448,6 +450,8 @@ test('bind to invalid host ip', function (t) {
} catch (error) {
t.is(error.message, `${invalidHost} is not a valid IP address`)
}

await a.close()
})

test('send to invalid host ip', async function (t) {
Expand Down

0 comments on commit 31d105d

Please sign in to comment.