Skip to content

Commit b070ed6

Browse files
committed
test: better handling of process error and timeout for context exits
1 parent 3ac7a11 commit b070ed6

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

test/unit/context-process-exit-test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ describe("context process exit", function () {
3030

3131
it("should not occur when sockets are open and polling", async function () {
3232
this.slow(1000)
33-
const {code, is_timeout} = await createProcess(() => {
33+
const {code, isTimeout} = await createProcess(() => {
3434
const socket1 = new zmq.Dealer()
3535
socket1.connect("inproc://foo")
3636
socket1.receive().catch(err => {
3737
throw err
3838
})
3939
})
4040

41+
assert.equal(isTimeout, true)
4142
assert.equal(code, -1)
42-
assert.equal(is_timeout, true)
4343
})
4444

4545
it("should produce warning when messages are queued with blocky", async function () {
@@ -143,15 +143,15 @@ describe("context process exit", function () {
143143

144144
it("should not occur when sockets are open and polling", async function () {
145145
this.slow(1000)
146-
const {code, is_timeout} = await createProcess(() => {
146+
const {code, isTimeout} = await createProcess(() => {
147147
const context = new zmq.Context()
148148
const socket1 = new zmq.Dealer({context})
149149
socket1.connect("inproc://foo")
150150
socket1.receive()
151151
})
152152

153+
assert.equal(isTimeout, true)
153154
assert.equal(code, -1)
154-
assert.equal(is_timeout, true)
155155
})
156156
})
157157
})

test/unit/helpers.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ interface Result {
148148
code: number
149149
stdout: Buffer
150150
stderr: Buffer
151-
is_timeout: boolean
151+
isTimeout: boolean
152152
}
153153

154154
export function createProcess(fn: () => void): Promise<Result> {
@@ -158,7 +158,13 @@ export function createProcess(fn: () => void): Promise<Result> {
158158
const result = fn()
159159
if (result instanceof Promise) {
160160
result.catch(err => {
161-
throw err
161+
if (error instanceof Error) {
162+
console.error(error.message)
163+
console.error(error.stack)
164+
} else {
165+
console.error(error)
166+
}
167+
process.exit(10)
162168
})
163169
}
164170
`
@@ -191,12 +197,12 @@ export function createProcess(fn: () => void): Promise<Result> {
191197
)
192198
}
193199

194-
resolve({code, stdout, stderr, is_timeout: false})
200+
resolve({code, stdout, stderr, isTimeout: false})
195201
}
196202
})
197203

198204
setTimeout(() => {
199-
resolve({code: -1, stdout, stderr, is_timeout: true})
205+
resolve({code: -1, stdout, stderr, isTimeout: true})
200206
console.error(
201207
`Child timed out\n${stdout.toString()}\n${stderr.toString()}`,
202208
)

0 commit comments

Comments
 (0)