Skip to content

Commit c9bfe82

Browse files
committed
test: convert socket pair. push, req, router compat tests to TypeScript
1 parent 27601b2 commit c9bfe82

File tree

6 files changed

+55
-52
lines changed

6 files changed

+55
-52
lines changed

test/unit/compat/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The tests in this directory have been copied from the original ZeroMQ.js version
1+
The tests in this directory have been modified from the original ZeroMQ.js version
22
(up to 5.x) for which the license and copyright notice is reproduced below.
33

44
Copyright (c) 2011 TJ Holowaychuk Copyright (c) 2010, 2011 Justin Tulloss

test/unit/compat/socket-pair-test.js renamed to test/unit/compat/socket-pair-test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
if (process.env.INCLUDE_COMPAT_TESTS) {
2-
const zmq = require("./load")
3-
const {assert} = require("chai")
4-
const {testProtos, uniqAddress} = require("../helpers")
1+
import * as zmq from "../../../v5-compat"
2+
import {assert} from "chai"
3+
import {testProtos, uniqAddress} from "../helpers"
54

5+
if (process.env.INCLUDE_COMPAT_TESTS) {
66
for (const proto of testProtos("tcp")) {
77
describe(`compat socket with ${proto} pair`, function () {
8-
it("should support pair-pair", function (done) {
8+
it("should support pair-pair", async function (done) {
99
const pairA = zmq.socket("pair")
1010
const pairB = zmq.socket("pair")
1111

12-
const address = uniqAddress(proto)
12+
const address = await uniqAddress(proto)
1313

1414
let n = 0
1515
pairA.monitor()

test/unit/compat/socket-pub-sub-test.js renamed to test/unit/compat/socket-pub-sub-test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
if (process.env.INCLUDE_COMPAT_TESTS) {
2-
const zmq = require("./load")
3-
const {assert} = require("chai")
4-
const {testProtos, uniqAddress} = require("../helpers")
1+
import * as zmq from "../../../v5-compat"
2+
import {assert} from "chai"
3+
import {testProtos, uniqAddress} from "../helpers"
54

5+
if (process.env.INCLUDE_COMPAT_TESTS) {
66
for (const proto of testProtos("tcp", "inproc")) {
77
describe(`compat socket with ${proto} pub-sub`, function () {
8-
let pub
9-
let sub
8+
let pub: zmq.Socket
9+
let sub: zmq.Socket
1010

1111
beforeEach(function () {
1212
pub = zmq.socket("pub")
1313
sub = zmq.socket("sub")
1414
})
1515

16-
it("should support pub-sub", function (done) {
17-
const address = uniqAddress(proto)
16+
it("should support pub-sub", async function (done) {
17+
const address = await uniqAddress(proto)
1818
let n = 0
1919

2020
sub.subscribe("")
@@ -59,8 +59,8 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
5959
})
6060
})
6161

62-
it("should support pub-sub filter", function (done) {
63-
const address = uniqAddress(proto)
62+
it("should support pub-sub filter", async function (done) {
63+
const address = await uniqAddress(proto)
6464
let n = 0
6565

6666
sub.subscribe("js")
@@ -112,13 +112,13 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
112112
}
113113
})
114114

115-
it("should continue to deliver messages in message handler", function (done) {
116-
let error
115+
it("should continue to deliver messages in message handler", async function (done) {
116+
let error: Error
117117
process.once("uncaughtException", err => {
118118
error = err
119119
})
120120

121-
const address = uniqAddress(proto)
121+
const address = await uniqAddress(proto)
122122
let n = 0
123123

124124
sub.subscribe("")

test/unit/compat/socket-push-pull-test.js renamed to test/unit/compat/socket-push-pull-test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
if (process.env.INCLUDE_COMPAT_TESTS) {
2-
const zmq = require("./load")
3-
const {assert} = require("chai")
4-
const {testProtos, uniqAddress} = require("../helpers")
1+
import * as zmq from "../../../v5-compat"
2+
import {assert} from "chai"
3+
import {testProtos, uniqAddress} from "../helpers"
54

5+
if (process.env.INCLUDE_COMPAT_TESTS) {
66
for (const proto of testProtos("tcp", "inproc")) {
77
describe(`compat socket with ${proto} push-pull`, function () {
8-
it("should support push-pull", function (done) {
8+
it("should support push-pull", async function (done) {
99
const push = zmq.socket("push")
1010
const pull = zmq.socket("pull")
1111

12-
const address = uniqAddress(proto)
12+
const address = await uniqAddress(proto)
1313

1414
let n = 0
1515
pull.on("message", function (msg) {
@@ -42,11 +42,11 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
4242
})
4343
})
4444

45-
it("should not emit messages after pause", function (done) {
45+
it("should not emit messages after pause", async function (done) {
4646
const push = zmq.socket("push")
4747
const pull = zmq.socket("pull")
4848

49-
const address = uniqAddress(proto)
49+
const address = await uniqAddress(proto)
5050

5151
let n = 0
5252

@@ -77,11 +77,11 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
7777
}, 15)
7878
})
7979

80-
it("should be able to read messages after pause", function (done) {
80+
it("should be able to read messages after pause", async function (done) {
8181
const push = zmq.socket("push")
8282
const pull = zmq.socket("pull")
8383

84-
const address = uniqAddress(proto)
84+
const address = await uniqAddress(proto)
8585

8686
const messages = ["bar", "foo"]
8787
pull.bind(address, err => {
@@ -108,19 +108,19 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
108108
}, 15)
109109
})
110110

111-
it("should emit messages after resume", function (done) {
111+
it("should emit messages after resume", async function (done) {
112112
const push = zmq.socket("push")
113113
const pull = zmq.socket("pull")
114114

115-
const address = uniqAddress(proto)
115+
const address = await uniqAddress(proto)
116116

117117
let n = 0
118118

119-
function checkNoMessages(msg) {
119+
function checkNoMessages(msg: string) {
120120
assert.equal(msg, undefined)
121121
}
122122

123-
function checkMessages(msg) {
123+
function checkMessages(msg: string) {
124124
assert.instanceOf(msg, Buffer)
125125
switch (n++) {
126126
case 0:

test/unit/compat/socket-req-rep-test.js renamed to test/unit/compat/socket-req-rep-test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
if (process.env.INCLUDE_COMPAT_TESTS) {
2-
const zmq = require("./load")
3-
const {assert} = require("chai")
4-
const {testProtos, uniqAddress} = require("../helpers")
1+
import * as zmq from "../../../v5-compat"
2+
import {assert} from "chai"
3+
import {testProtos, uniqAddress} from "../helpers"
54

5+
if (process.env.INCLUDE_COMPAT_TESTS) {
66
for (const proto of testProtos("tcp", "inproc")) {
77
describe(`compat socket with ${proto} req-rep`, function () {
8-
it("should support req-rep", function (done) {
8+
it("should support req-rep", async function (done) {
99
const rep = zmq.socket("rep")
1010
const req = zmq.socket("req")
1111

12-
const address = uniqAddress(proto)
12+
const address = await uniqAddress(proto)
1313

14-
rep.on("message", function (msg) {
14+
rep.on("message", function (msg: string) {
1515
assert.instanceOf(msg, Buffer)
1616
assert.equal(msg.toString(), "hello")
1717
rep.send("world")
@@ -37,11 +37,11 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
3737
const n = 5
3838

3939
for (let i = 0; i < n; i++) {
40-
;(function (n) {
40+
;(async function (n) {
4141
const rep = zmq.socket("rep")
4242
const req = zmq.socket("req")
4343

44-
const address = uniqAddress(proto)
44+
const address = await uniqAddress(proto)
4545

4646
rep.on("message", function (msg) {
4747
assert.instanceOf(msg, Buffer)
@@ -69,11 +69,11 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
6969
}
7070
})
7171

72-
it("should support a burst", function (done) {
72+
it("should support a burst", async function (done) {
7373
const rep = zmq.socket("rep")
7474
const req = zmq.socket("req")
7575

76-
const address = uniqAddress(proto)
76+
const address = await uniqAddress(proto)
7777

7878
const n = 10
7979

test/unit/compat/socket-router-test.js renamed to test/unit/compat/socket-router-test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
if (process.env.INCLUDE_COMPAT_TESTS) {
2-
const zmq = require("./load")
3-
const {assert} = require("chai")
4-
const {testProtos, uniqAddress} = require("../helpers")
1+
import * as zmq from "../../../v5-compat"
2+
import {assert} from "chai"
3+
import {testProtos, uniqAddress} from "../helpers"
54

5+
if (process.env.INCLUDE_COMPAT_TESTS) {
66
for (const proto of testProtos("tcp", "inproc")) {
77
describe(`compat socket with ${proto} router`, function () {
88
it("should handle unroutable messages", function (done) {
@@ -16,7 +16,10 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
1616
errMsgs.push("Resource temporarily unavailable")
1717
errMsgs.push("Host unreachable")
1818

19-
function assertRouteError(err) {
19+
function assertRouteError(err: Error | undefined) {
20+
if (err === undefined) {
21+
throw new Error("No error was emitted")
22+
}
2023
if (errMsgs.indexOf(err.message) === -1) {
2124
throw new Error(err.message)
2225
}
@@ -71,14 +74,14 @@ if (process.env.INCLUDE_COMPAT_TESTS) {
7174
}
7275
})
7376

74-
it("should handle router-dealer message bursts", function (done) {
77+
it("should handle router-dealer message bursts", async function (done) {
7578
// tests https://github.com/JustinTulloss/zeromq.node/issues/523
7679
// based on https://gist.github.com/messa/862638ab44ca65f712fe4d6ef79aeb67
7780

7881
const router = zmq.socket("router")
7982
const dealer = zmq.socket("dealer")
8083

81-
const address = uniqAddress(proto)
84+
const address = await uniqAddress(proto)
8285

8386
const expected = 1000
8487
let counted = 0

0 commit comments

Comments
 (0)