Skip to content

Commit

Permalink
bind method rejects on error. Fixes #42
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Oct 13, 2020
1 parent 919c134 commit 05bea35
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## v4.0.0 2020-10-13

- `bind` method rejects on error. Fixes #42.
- Requires Node >= 10.
- Uses eslint instead of tslint.

Expand Down
38 changes: 27 additions & 11 deletions src/socket-as-promised.ts
Expand Up @@ -14,17 +14,33 @@ export class SocketAsPromised {
bind(options: BindOptions): Promise<AddressInfo>

bind(arg1?: any, arg2?: any): Promise<AddressInfo> {
return new Promise(resolve => {
if (arg2 !== undefined) {
this.socket.bind(arg1, arg2, () => {
const address = this.socket.address() as AddressInfo
resolve(address)
})
} else {
this.socket.bind(arg1, () => {
const address = this.socket.address() as AddressInfo
resolve(address)
})
const socket = this.socket

return new Promise((resolve, reject) => {
const errorHandler = (err: Error) => {
removeListeners()
reject(err)
}

const listeningHandler = () => {
const address = socket.address() as AddressInfo
removeListeners()
resolve(address)
}

const removeListeners = () => {
socket.removeListener("error", errorHandler)
socket.removeListener("listening", listeningHandler)
}

socket.on("error", errorHandler)
socket.on("listening", listeningHandler)

try {
socket.bind(arg1, arg2)
} catch (e) {
removeListeners()
reject(e)
}
})
}
Expand Down
54 changes: 54 additions & 0 deletions test/dgram-as-promised.ts
Expand Up @@ -51,6 +51,60 @@ Feature("Test dgram-as-promised module", () => {
})
})

Scenario("Can't bind with rejection", () => {
let error: Error
let socket: SocketAsPromised

Given("socket", () => {
socket = dgramAsPromised.createSocket({type: "udp4", dgram: mockDgram as any})
})

When("bind operation is rejected", async () => {
await socket.bind({address: "exception"}).catch(err => {
error = err
})
})

Then("error is occured", () => {
expect(error).to.be.an("Error")
})

After(async () => {
try {
await socket.close()
} catch (e) {
// ignore
}
})
})

Scenario("Can't bind with exception", () => {
let error: Error
let socket: SocketAsPromised

Given("socket", () => {
socket = dgramAsPromised.createSocket({type: "udp4", dgram: mockDgram as any})
})

When("bind operation throws exception", async () => {
await socket.bind({address: "exception"}).catch(err => {
error = err
})
})

Then("error is occured", () => {
expect(error).to.be.an("Error")
})

After(async () => {
try {
await socket.close()
} catch (e) {
// ignore
}
})
})

Scenario("Can't send datagram", () => {
let address: string
let error: Error
Expand Down
10 changes: 9 additions & 1 deletion test/lib/mock-dgram.ts
Expand Up @@ -26,12 +26,20 @@ class Socket extends EventEmitter {
bind(options: BindOptions, callback?: () => void): void

bind(options?: any, callback?: () => void): void {
if (typeof options === "object" && options.address === "rejection") {
this.emit("error", new Error(options))
return
}
if (typeof options === "object" && options.address === "exception") {
throw new Error(options)
}
if (typeof options === "function") {
callback = options
}
if (callback) {
callback()
this.on("listening", callback)
}
this.emit("listening")
}

close(callback?: () => void): void {
Expand Down

0 comments on commit 05bea35

Please sign in to comment.