forked from zeromq/zeromq.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
277 lines (234 loc) · 6.8 KB
/
helpers.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import * as path from "path"
import * as semver from "semver"
import * as fs from "fs"
import * as lockfile from "proper-lockfile"
import {spawn} from "child_process"
import * as zmq from "../../src"
console.log(`ZeroMQ version ${zmq.version}`)
if (semver.satisfies(zmq.version, ">= 4.2")) {
/* Stop pending messages in test suite from preventing process exit. */
zmq.context.blocky = false
}
/**
* Get a unique id to be used as a port number or IPC path.
* This function is thread-safe and will use a lock file to ensure that the id is unique.
*/
let idFallback = 5000
async function getUniqueId() {
const idPath = path.resolve(__dirname, "../../tmp/id")
await fs.promises.mkdir(path.dirname(idPath), {recursive: true})
try {
// Create the file if it doesn't exist
if (!fs.existsSync(idPath)) {
await fs.promises.writeFile(idPath, "5000", "utf8")
/* Windows cannot bind on a ports just above 1014; start higher to be safe. */
return 5000
}
await lockfile.lock(idPath, {retries: 10})
// Read the current number from the file
const idString = await fs.promises.readFile(idPath, "utf8")
let id = parseInt(idString, 10)
// Increment the number
id++
// Ensure the number is within the valid port range
if (id > 65535) {
idFallback++
id = idFallback
}
// Write the new number back to the file
await fs.promises.writeFile(idPath, id.toString(), "utf8")
return id
} catch (err) {
console.error(`Error getting unique id via id file: ${err}`)
return idFallback++
} finally {
// Release the lock
try {
await lockfile.unlock(idPath)
} catch {
// ignore
}
}
}
type Proto = "ipc" | "tcp" | "udp" | "inproc"
export async function uniqAddress(proto: Proto) {
const id = await getUniqueId()
switch (proto) {
case "ipc": {
const sock = path.resolve(__dirname, `../../tmp/${proto}-${id}`)
return `${proto}://${sock}`
}
case "tcp":
case "udp":
return `${proto}://127.0.0.1:${id}`
case "inproc":
default:
return `${proto}://${proto}-${id}`
}
}
export function testProtos(...requested: Proto[]) {
const set = new Set(requested)
/* Do not test with ipc if unsupported. */
if (zmq.capability.ipc !== true) {
set.delete("ipc")
}
/* Only test inproc with version 4.2+, earlier versions are unreliable. */
if (semver.satisfies(zmq.version, "< 4.2")) {
set.delete("inproc")
}
if (!set.size) {
console.error("Warning: test protocol set is empty")
}
return [...set]
}
export async function createWorker<T, D extends {}>(
data: D,
fn: (data: D) => Promise<T>,
): Promise<T> {
const src = `
const {parentPort, workerData} = require("worker_threads")
const zmq = require(${JSON.stringify(path.resolve(__dirname, "../.."))})
async function run() {
const fn = ${fn.toString()}
const msg = await fn(workerData)
parentPort.postMessage(msg)
}
run()
`
const {Worker} = await import("worker_threads")
return new Promise<T>((resolve, reject) => {
const worker = new Worker(src, {
eval: true,
workerData: data,
})
let message: T
worker.on("message", msg => {
message = msg
})
worker.on("exit", code => {
if (code === 0) {
resolve(message)
} else {
reject(new Error(`Worker stopped with exit code ${code}`))
}
})
})
}
interface Result {
code: number
stdout: Buffer
stderr: Buffer
isTimeout: boolean
}
export function createProcess(fn: () => void): Promise<Result> {
const src = `
const zmq = require(${JSON.stringify(path.resolve(__dirname, "../.."))})
const fn = ${fn.toString()}
const result = fn()
if (result instanceof Promise) {
result.catch(err => {
if (error instanceof Error) {
console.error(error.message)
console.error(error.stack)
} else {
console.error(error)
}
process.exit(10)
})
}
`
const child = spawn(process.argv[0], ["--expose_gc"])
child.stdin.write(src)
child.stdin.end()
let stdout: Buffer = Buffer.alloc(0)
let stderr: Buffer = Buffer.alloc(0)
child.stdout.on("data", (data: Buffer) => {
stdout = Buffer.concat([stdout, data])
})
child.stderr.on("data", (data: Buffer) => {
stderr = Buffer.concat([stderr, data])
})
return new Promise((resolve, reject) => {
child.on("close", (code: number, signal: string) => {
if (signal) {
reject(
new Error(
`Child exited with ${signal}:\n${stdout.toString()}\n${stderr.toString()}`,
),
)
} else {
if (code !== 0) {
console.error(
`Child exited with code ${code}:\n${stdout.toString()}\n${stderr.toString()}`,
)
}
resolve({code, stdout, stderr, isTimeout: false})
}
})
setTimeout(() => {
resolve({code: -1, stdout, stderr, isTimeout: true})
console.error(
`Child timed out\n${stdout.toString()}\n${stderr.toString()}`,
)
child.kill()
}, 750)
})
}
export function captureEvent<E extends zmq.EventType>(
socket: zmq.Socket,
type: E,
): Promise<zmq.EventOfType<E>> {
return new Promise(resolve => {
socket.events.on<E>(type, resolve)
})
}
export async function captureEventsUntil(
socket: zmq.Socket,
type: zmq.EventType,
): Promise<zmq.Event[]> {
const events = []
for await (const event of socket.events) {
events.push(event)
if (event.type === type) {
break
}
}
return events
}
// REAL typings for global.gc per
// https://github.com/nodejs/node/blob/v20.0.0/deps/v8/src/extensions/gc-extension.cc
interface GCFunction {
(options: {
execution?: "sync"
flavor?: "regular" | "last-resort"
type?: "major-snapshot" | "major" | "minor"
filename?: string
}): void
(options: {
execution?: "async"
flavor?: "regular" | "last-resort"
type?: "major-snapshot" | "major" | "minor"
filename?: string
}): Promise<void>
(options: {
execution?: "async" | "sync"
flavor?: "regular" | "last-resort"
type?: "major-snapshot" | "major" | "minor"
filename?: string
}): void | Promise<void>
}
export function getGcOrSkipTest(test: Mocha.Context) {
if (process.env.SKIP_GC_TESTS === "true") {
test.skip()
}
const gc = globalThis.gc as undefined | GCFunction
if (typeof gc !== "function") {
throw new Error(
"Garbage collection is not exposed. It may be enabled by the node --expose-gc flag. To skip GC tests, set the environment variable `SKIP_GC_TESTS`",
)
}
// https://github.com/nodejs/node/blob/v20.0.0/deps/v8/src/extensions/gc-extension.h
// per docs, we we're using use case 2 (Test that certain objects indeed are reclaimed)
const asyncMajorGc = () => gc({type: "major", execution: "async"})
return asyncMajorGc
}