Skip to content

Commit cfa3117

Browse files
committed
bump workflow versions
Replace weak-napi with built in WeakRef
1 parent 46d129d commit cfa3117

File tree

8 files changed

+112
-201
lines changed

8 files changed

+112
-201
lines changed

CONTRIBUTING.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,3 @@ tool similar to Cmake. GYP was originally created to generate native IDE project
3838
files (Visual Studio, Xcode) for building Chromium.
3939

4040
The `.gyp` file is structured as a Python dictionary.
41-
42-
## Weak-napi
43-
44-
https://www.npmjs.com/package/weak-napi On certain rarer occasions, you run into
45-
the need to be notified when a JavaScript object is going to be garbage
46-
collected. This feature is exposed to V8's C++ API, but not to JavaScript.
47-
48-
That's where weak-napi comes in! This module exports the JS engine's GC tracking
49-
functionality to JavaScript. This allows you to create weak references, and
50-
optionally attach a callback function to any arbitrary JS object.

test/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/pnpm-lock.yaml

Lines changed: 0 additions & 81 deletions
This file was deleted.

test/tsconfig.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
{
22
"extends": "../tsconfig.json",
3-
"include": ["**/*.ts"]
3+
"include": [
4+
"**/*.ts"
5+
],
6+
"ts-node": {
7+
"files": true
8+
},
9+
"compilerOptions": {
10+
"types": [
11+
"mocha"
12+
],
13+
"lib": [
14+
"ESNext"
15+
]
16+
}
417
}

test/unit/helpers.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,43 @@ export async function captureEventsUntil(
235235

236236
return events
237237
}
238+
239+
// REAL typings for global.gc per
240+
// https://github.com/nodejs/node/blob/v20.0.0/deps/v8/src/extensions/gc-extension.cc
241+
interface GCFunction {
242+
(options: {
243+
execution?: "sync"
244+
flavor?: "regular" | "last-resort"
245+
type?: "major-snapshot" | "major" | "minor"
246+
filename?: string
247+
}): void
248+
(options: {
249+
execution?: "async"
250+
flavor?: "regular" | "last-resort"
251+
type?: "major-snapshot" | "major" | "minor"
252+
filename?: string
253+
}): Promise<void>
254+
(options: {
255+
execution?: "async" | "sync"
256+
flavor?: "regular" | "last-resort"
257+
type?: "major-snapshot" | "major" | "minor"
258+
filename?: string
259+
}): void | Promise<void>
260+
}
261+
262+
export function getGcOrSkipTest(test: Mocha.Context) {
263+
if (process.env.SKIP_GC_TESTS) {
264+
test.skip()
265+
}
266+
267+
const gc = globalThis.gc as undefined | GCFunction
268+
if (typeof gc !== "function") {
269+
throw new Error(
270+
"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`",
271+
)
272+
}
273+
// https://github.com/nodejs/node/blob/v20.0.0/deps/v8/src/extensions/gc-extension.h
274+
// per docs, we we're using use case 2 (Test that certain objects indeed are reclaimed)
275+
const asyncMajorGc = () => gc({type: "major", execution: "async"})
276+
return asyncMajorGc
277+
}

test/unit/socket-close-test.ts

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as zmq from "../../src"
33

44
import {assert} from "chai"
5-
import {testProtos, uniqAddress} from "./helpers"
5+
import {testProtos, uniqAddress, getGcOrSkipTest} from "./helpers"
66
import {isFullError} from "../../src/errors"
77

88
for (const proto of testProtos("tcp", "ipc", "inproc")) {
@@ -107,75 +107,50 @@ for (const proto of testProtos("tcp", "ipc", "inproc")) {
107107
})
108108

109109
it("should release reference to context", async function () {
110-
if (process.env.SKIP_GC_TESTS === "true") {
111-
this.skip()
112-
}
113-
if (global.gc === undefined) {
114-
console.warn("gc is not exposed by the runtime")
115-
this.skip()
116-
}
117-
110+
const gc = getGcOrSkipTest(this)
118111
this.slow(200)
119112

120-
const weak = require("weak-napi") as typeof import("weak-napi")
113+
let weakRef: undefined | WeakRef<any>
121114

122-
let released = false
123115
const task = async () => {
124116
let context: zmq.Context | undefined = new zmq.Context()
125117
const socket = new zmq.Dealer({context, linger: 0})
118+
weakRef = new WeakRef(context)
126119

127-
weak(context, () => {
128-
released = true
129-
})
130-
context = undefined
131-
132-
global.gc!()
133120
socket.connect(await uniqAddress(proto))
134121
await socket.send(Buffer.from("foo"))
135122
socket.close()
136123
}
137124

138125
await task()
139-
global.gc()
140-
await new Promise(resolve => {
141-
setTimeout(resolve, 5)
142-
})
143-
assert.equal(released, true)
126+
await gc()
127+
128+
assert.isDefined(weakRef)
129+
assert.isUndefined(weakRef!.deref())
144130
})
145131
})
146132

147133
describe("in gc finalizer", function () {
148134
it("should release reference to context", async function () {
149-
if (process.env.SKIP_GC_TESTS === "true") {
150-
this.skip()
151-
}
152-
if (global.gc === undefined) {
153-
console.warn("gc is not exposed by the runtime")
135+
const gc = getGcOrSkipTest(this)
136+
if (process.env.SKIP_GC_FINALIZER_TESTS) {
154137
this.skip()
155138
}
156139
this.slow(200)
157140

158-
const weak = require("weak-napi") as typeof import("weak-napi")
159-
160-
let released = false
141+
let weakRef: undefined | WeakRef<any>
161142
const task = async () => {
162143
let context: zmq.Context | undefined = new zmq.Context()
163144

164145
const _dealer = new zmq.Dealer({context, linger: 0})
165-
166-
weak(context, () => {
167-
released = true
168-
})
169-
context = undefined
170-
global.gc!()
146+
weakRef = new WeakRef(context)
171147
}
172148

173149
await task()
174-
global.gc()
175-
await new Promise(resolve => {
176-
setTimeout(resolve, 5)
177-
})
178-
assert.equal(released, true)
150+
await gc()
151+
152+
assert.isDefined(weakRef)
153+
assert.isUndefined(weakRef!.deref())
179154
})
180155
})
181156
})

0 commit comments

Comments
 (0)