Skip to content

Commit 3bf769f

Browse files
committed
test: improve capability guarding in the tests
1 parent 38a2ed4 commit 3bf769f

9 files changed

+293
-287
lines changed

test/unit/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function testProtos(...requested: Proto[]) {
4242
const set = new Set(requested)
4343

4444
/* Do not test with ipc if unsupported. */
45-
if (!zmq.capability.ipc) {
45+
if (zmq.capability.ipc !== true) {
4646
set.delete("ipc")
4747
}
4848

test/unit/socket-construction-test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,16 @@ describe("socket construction", function () {
159159
)
160160
})
161161

162-
if (!zmq.capability.draft) {
163-
it("should throw with draft type", function () {
164-
assert.throws(
165-
() => new (zmq.Socket as any)(14),
166-
Error,
167-
"Invalid argument",
168-
)
169-
})
170-
}
162+
it("should throw with draft type", function () {
163+
if (zmq.capability.draft === true) {
164+
this.skip()
165+
}
166+
assert.throws(
167+
() => new (zmq.Socket as any)(14),
168+
Error,
169+
"Invalid argument",
170+
)
171+
})
171172

172173
it("should throw error on file descriptor limit", async function () {
173174
const context = new zmq.Context({maxSockets: 10})

test/unit/socket-curve-send-receive-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {testProtos, uniqAddress} from "./helpers"
55

66
for (const proto of testProtos("tcp", "ipc", "inproc")) {
77
describe(`socket with ${proto} curve send/receive`, function () {
8-
if (!zmq.capability.curve) {
8+
if (zmq.capability.curve !== true) {
99
return
1010
}
1111

test/unit/socket-draft-dgram-test.ts

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,61 @@ import {assert} from "chai"
55
import {createSocket} from "dgram"
66
import {testProtos, uniqAddress} from "./helpers"
77

8-
if (zmq.capability.draft) {
9-
for (const proto of testProtos("udp")) {
10-
describe(`draft socket with ${proto} dgram`, function () {
11-
let dgram: draft.Datagram
8+
for (const proto of testProtos("udp")) {
9+
describe(`draft socket with ${proto} dgram`, function () {
10+
if (zmq.capability.draft !== true) {
11+
if (process.env.ZMQ_DRAFT === "true") {
12+
throw new Error("Draft API requested but not available at runtime.")
13+
}
14+
return
15+
}
1216

13-
beforeEach(function () {
14-
dgram = new draft.Datagram()
15-
})
17+
let dgram: draft.Datagram
1618

17-
afterEach(function () {
18-
dgram.close()
19-
global.gc?.()
20-
})
19+
beforeEach(function () {
20+
dgram = new draft.Datagram()
21+
})
2122

22-
describe("send/receive", function () {
23-
it("should deliver messages", async function () {
24-
const messages = ["foo", "bar", "baz", "qux"]
25-
const address = uniqAddress(proto)
26-
const port = parseInt(address.split(":").pop()!, 10)
23+
afterEach(function () {
24+
dgram.close()
25+
global.gc?.()
26+
})
2727

28-
await dgram.bind(address)
28+
describe("send/receive", function () {
29+
it("should deliver messages", async function () {
30+
const messages = ["foo", "bar", "baz", "qux"]
31+
const address = uniqAddress(proto)
32+
const port = parseInt(address.split(":").pop()!, 10)
2933

30-
const echo = async () => {
31-
for await (const [id, msg] of dgram) {
32-
await dgram.send([id, msg])
33-
}
34-
}
34+
await dgram.bind(address)
3535

36-
const received: string[] = []
37-
const send = async () => {
38-
for (const msg of messages) {
39-
const client = createSocket("udp4")
40-
await new Promise(resolve => {
41-
client.on("message", res => {
42-
received.push(res.toString())
43-
client.close()
44-
resolve(undefined)
45-
})
36+
const echo = async () => {
37+
for await (const [id, msg] of dgram) {
38+
await dgram.send([id, msg])
39+
}
40+
}
4641

47-
client.send(msg, port, "localhost")
42+
const received: string[] = []
43+
const send = async () => {
44+
for (const msg of messages) {
45+
const client = createSocket("udp4")
46+
await new Promise(resolve => {
47+
client.on("message", res => {
48+
received.push(res.toString())
49+
client.close()
50+
resolve(undefined)
4851
})
49-
}
5052

51-
dgram.close()
53+
client.send(msg, port, "localhost")
54+
})
5255
}
5356

54-
await Promise.all([echo(), send()])
55-
assert.deepEqual(received, messages)
56-
})
57+
dgram.close()
58+
}
59+
60+
await Promise.all([echo(), send()])
61+
assert.deepEqual(received, messages)
5762
})
5863
})
59-
}
60-
} else {
61-
if (process.env.ZMQ_DRAFT === "true") {
62-
throw new Error("Draft API requested but not available at runtime.")
63-
}
64+
})
6465
}

test/unit/socket-draft-radio-dish-test.ts

Lines changed: 88 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,123 +4,124 @@ import * as draft from "../../src/draft"
44
import {assert} from "chai"
55
import {testProtos, uniqAddress} from "./helpers"
66

7-
if (zmq.capability.draft) {
8-
for (const proto of testProtos("tcp", "ipc", "inproc", "udp")) {
9-
describe(`draft socket with ${proto} radio/dish`, function () {
10-
let radio: draft.Radio
11-
let dish: draft.Dish
12-
13-
beforeEach(function () {
14-
radio = new draft.Radio()
15-
dish = new draft.Dish()
16-
})
7+
for (const proto of testProtos("tcp", "ipc", "inproc", "udp")) {
8+
describe(`draft socket with ${proto} radio/dish`, function () {
9+
if (zmq.capability.draft !== true) {
10+
if (process.env.ZMQ_DRAFT === "true") {
11+
throw new Error("Draft API requested but not available at runtime.")
12+
}
13+
return
14+
}
15+
16+
let radio: draft.Radio
17+
let dish: draft.Dish
18+
19+
beforeEach(function () {
20+
radio = new draft.Radio()
21+
dish = new draft.Dish()
22+
})
1723

18-
afterEach(function () {
19-
global.gc?.()
20-
radio.close()
21-
dish.close()
22-
global.gc?.()
23-
})
24+
afterEach(function () {
25+
global.gc?.()
26+
radio.close()
27+
dish.close()
28+
global.gc?.()
29+
})
2430

25-
describe("send/receive", function () {
26-
it("should deliver messages", async function () {
27-
/* RADIO -> foo -> DISH
31+
describe("send/receive", function () {
32+
it("should deliver messages", async function () {
33+
/* RADIO -> foo -> DISH
2834
-> bar -> joined all
2935
-> baz ->
3036
-> qux ->
3137
*/
3238

33-
const address = uniqAddress(proto)
34-
const messages = ["foo", "bar", "baz", "qux"]
39+
const address = uniqAddress(proto)
40+
const messages = ["foo", "bar", "baz", "qux"]
3541

36-
/* Max 15 non-null bytes. */
37-
const uuid = Buffer.from([
38-
0xf6, 0x46, 0x1f, 0x03, 0xd2, 0x0d, 0xc8, 0x66, 0xe5, 0x5f, 0xf5,
39-
0xa1, 0x65, 0x62, 0xb2,
40-
])
42+
/* Max 15 non-null bytes. */
43+
const uuid = Buffer.from([
44+
0xf6, 0x46, 0x1f, 0x03, 0xd2, 0x0d, 0xc8, 0x66, 0xe5, 0x5f, 0xf5,
45+
0xa1, 0x65, 0x62, 0xb2,
46+
])
4147

42-
const received: string[] = []
48+
const received: string[] = []
4349

44-
dish.join(uuid)
50+
dish.join(uuid)
4551

46-
await dish.bind(address)
47-
await radio.connect(address)
52+
await dish.bind(address)
53+
await radio.connect(address)
4854

49-
const send = async () => {
50-
/* Wait briefly before publishing to avoid slow joiner syndrome. */
51-
await new Promise(resolve => {
52-
setTimeout(resolve, 25)
53-
})
54-
for (const msg of messages) {
55-
await radio.send(msg, {group: uuid})
56-
}
55+
const send = async () => {
56+
/* Wait briefly before publishing to avoid slow joiner syndrome. */
57+
await new Promise(resolve => {
58+
setTimeout(resolve, 25)
59+
})
60+
for (const msg of messages) {
61+
await radio.send(msg, {group: uuid})
5762
}
58-
59-
const receive = async () => {
60-
for await (const [msg, {group}] of dish) {
61-
assert.instanceOf(msg, Buffer)
62-
assert.instanceOf(group, Buffer)
63-
assert.deepEqual(group, uuid)
64-
received.push(msg.toString())
65-
if (received.length === messages.length) {
66-
break
67-
}
63+
}
64+
65+
const receive = async () => {
66+
for await (const [msg, {group}] of dish) {
67+
assert.instanceOf(msg, Buffer)
68+
assert.instanceOf(group, Buffer)
69+
assert.deepEqual(group, uuid)
70+
received.push(msg.toString())
71+
if (received.length === messages.length) {
72+
break
6873
}
6974
}
75+
}
7076

71-
await Promise.all([send(), receive()])
72-
assert.deepEqual(received, messages)
73-
})
77+
await Promise.all([send(), receive()])
78+
assert.deepEqual(received, messages)
7479
})
80+
})
7581

76-
describe("join/leave", function () {
77-
it("should filter messages", async function () {
78-
/* RADIO -> foo -X DISH
82+
describe("join/leave", function () {
83+
it("should filter messages", async function () {
84+
/* RADIO -> foo -X DISH
7985
-> bar -> joined "ba"
8086
-> baz ->
8187
-> qux -X
8288
*/
8389

84-
const address = uniqAddress(proto)
85-
const messages = ["foo", "bar", "baz", "qux"]
86-
const received: string[] = []
90+
const address = uniqAddress(proto)
91+
const messages = ["foo", "bar", "baz", "qux"]
92+
const received: string[] = []
8793

88-
/* Everything after null byte should be ignored. */
89-
dish.join(Buffer.from("fo\x00ba"), Buffer.from("ba\x00fo"))
90-
dish.leave(Buffer.from("fo"))
94+
/* Everything after null byte should be ignored. */
95+
dish.join(Buffer.from("fo\x00ba"), Buffer.from("ba\x00fo"))
96+
dish.leave(Buffer.from("fo"))
9197

92-
await dish.bind(address)
93-
await radio.connect(address)
98+
await dish.bind(address)
99+
await radio.connect(address)
94100

95-
const send = async () => {
96-
/* Wait briefly before publishing to avoid slow joiner syndrome. */
97-
await new Promise(resolve => {
98-
setTimeout(resolve, 25)
99-
})
100-
for (const msg of messages) {
101-
await radio.send(msg, {group: msg.slice(0, 2)})
102-
}
101+
const send = async () => {
102+
/* Wait briefly before publishing to avoid slow joiner syndrome. */
103+
await new Promise(resolve => {
104+
setTimeout(resolve, 25)
105+
})
106+
for (const msg of messages) {
107+
await radio.send(msg, {group: msg.slice(0, 2)})
103108
}
104-
105-
const receive = async () => {
106-
for await (const [msg, {group}] of dish) {
107-
assert.instanceOf(msg, Buffer)
108-
assert.deepEqual(group, msg.slice(0, 2))
109-
received.push(msg.toString())
110-
if (received.length === 2) {
111-
break
112-
}
109+
}
110+
111+
const receive = async () => {
112+
for await (const [msg, {group}] of dish) {
113+
assert.instanceOf(msg, Buffer)
114+
assert.deepEqual(group, msg.slice(0, 2))
115+
received.push(msg.toString())
116+
if (received.length === 2) {
117+
break
113118
}
114119
}
120+
}
115121

116-
await Promise.all([send(), receive()])
117-
assert.deepEqual(received, ["bar", "baz"])
118-
})
122+
await Promise.all([send(), receive()])
123+
assert.deepEqual(received, ["bar", "baz"])
119124
})
120125
})
121-
}
122-
} else {
123-
if (process.env.ZMQ_DRAFT === "true") {
124-
throw new Error("Draft API requested but not available at runtime.")
125-
}
126+
})
126127
}

0 commit comments

Comments
 (0)