forked from zeromq/zeromq.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-close-test.ts
156 lines (135 loc) · 4.43 KB
/
socket-close-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* eslint-disable @typescript-eslint/no-var-requires */
import * as zmq from "../../src"
import {assert} from "chai"
import {testProtos, uniqAddress, getGcOrSkipTest} from "./helpers"
import {isFullError} from "../../src/errors"
for (const proto of testProtos("tcp", "ipc", "inproc")) {
describe(`socket with ${proto} close`, function () {
let sock: zmq.Dealer
beforeEach(function () {
sock = new zmq.Dealer()
})
afterEach(function () {
sock.close()
global.gc?.()
})
describe("with explicit call", function () {
it("should close socket", function () {
assert.equal(sock.closed, false)
sock.close()
assert.equal(sock.closed, true)
})
it("should close socket and cancel send", async function () {
assert.equal(sock.closed, false)
const promise = sock.send(Buffer.from("foo"))
sock.close()
assert.equal(sock.closed, true)
try {
await promise
} catch (err) {
if (!isFullError(err)) {
throw err
}
assert.equal(err.message, "Operation was not possible or timed out")
assert.equal(err.code, "EAGAIN")
assert.typeOf(err.errno, "number")
}
})
it("should close socket and cancel receive", async function () {
assert.equal(sock.closed, false)
const promise = sock.receive()
sock.close()
assert.equal(sock.closed, true)
try {
await promise
} catch (err) {
if (!isFullError(err)) {
throw err
}
assert.equal(err.message, "Operation was not possible or timed out")
assert.equal(err.code, "EAGAIN")
assert.typeOf(err.errno, "number")
}
})
it("should close after successful bind", async function () {
const promise = sock.bind(await uniqAddress(proto))
sock.close()
assert.equal(sock.closed, false)
await promise
assert.equal(sock.closed, true)
})
it("should close after unsuccessful bind", async function () {
const address = await uniqAddress(proto)
await sock.bind(address)
const promise = sock.bind(address)
sock.close()
assert.equal(sock.closed, false)
try {
await promise
assert.ok(false)
} catch (err) {
/* Ignore */
}
assert.equal(sock.closed, true)
})
it("should close after successful unbind", async function () {
const address = await uniqAddress(proto)
await sock.bind(address)
const promise = sock.unbind(address)
sock.close()
assert.equal(sock.closed, false)
await promise
assert.equal(sock.closed, true)
})
it("should close after unsuccessful unbind", async function () {
const address = await uniqAddress(proto)
const promise = sock.unbind(address)
sock.close()
assert.equal(sock.closed, false)
try {
await promise
assert.ok(false)
} catch (err) {
/* Ignore */
}
assert.equal(sock.closed, true)
})
it("should release reference to context", async function () {
const gc = getGcOrSkipTest(this)
this.slow(200)
let weakRef: undefined | WeakRef<any>
const task = async () => {
let context: zmq.Context | undefined = new zmq.Context()
const socket = new zmq.Dealer({context, linger: 0})
weakRef = new WeakRef(context)
socket.connect(await uniqAddress(proto))
await socket.send(Buffer.from("foo"))
socket.close()
}
await task()
await gc()
assert.isDefined(weakRef)
assert.isUndefined(weakRef!.deref())
})
})
describe("in gc finalizer", function () {
it("should release reference to context", async function () {
const gc = getGcOrSkipTest(this)
if (process.env.SKIP_GC_FINALIZER_TESTS) {
this.skip()
}
this.slow(200)
let weakRef: undefined | WeakRef<any>
const task = async () => {
const context: zmq.Context | undefined = new zmq.Context()
const _dealer = new zmq.Dealer({context, linger: 0})
weakRef = new WeakRef(context)
}
await task()
await gc()
assert.isDefined(weakRef)
assert.isUndefined(weakRef!.deref())
})
})
})
}