/
socket-draft-server-client-test.ts
88 lines (74 loc) · 2.52 KB
/
socket-draft-server-client-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
import * as zmq from "../../src"
import * as draft from "../../src/draft"
import {assert} from "chai"
import {testProtos, uniqAddress} from "./helpers"
if (zmq.capability.draft) {
for (const proto of testProtos("tcp", "ipc", "inproc")) {
describe(`draft socket with ${proto} server/client`, function() {
let server: draft.Server
let clientA: draft.Client
let clientB: draft.Client
beforeEach(function() {
server = new draft.Server()
clientA = new draft.Client()
clientB = new draft.Client()
})
afterEach(function() {
server.close()
clientA.close()
clientB.close()
global.gc?.()
})
describe("send/receive", function() {
it("should deliver messages", async function() {
const address = uniqAddress(proto)
const messages = ["foo", "bar", "baz", "qux"]
const receivedA: string[] = []
const receivedB: string[] = []
await server.bind(address)
clientA.connect(address)
clientB.connect(address)
const echo = async () => {
for await (const [msg, {routingId}] of server) {
assert.typeOf(routingId, "number")
await server.send(msg, {routingId})
}
}
const send = async () => {
for (const msg of messages) {
await clientA.send(msg)
await clientB.send(msg)
}
for await (const msg of clientA) {
receivedA.push(msg.toString())
if (receivedA.length === messages.length) break
}
for await (const msg of clientB) {
receivedB.push(msg.toString())
if (receivedB.length === messages.length) break
}
server.close()
}
await Promise.all([echo(), send()])
assert.deepEqual(receivedA, messages)
assert.deepEqual(receivedB, messages)
})
it("should fail with unroutable message", async function() {
try {
await server.send("foo", {routingId: 12345})
assert.ok(false)
} catch (err) {
assert.instanceOf(err, Error)
assert.equal(err.message, "Host unreachable")
assert.equal(err.code, "EHOSTUNREACH")
assert.typeOf(err.errno, "number")
}
})
})
})
}
} else {
if (process.env.ZMQ_DRAFT === "true") {
throw new Error("Draft API requested but not available at runtime.")
}
}