Skip to content

Commit 5720ecf

Browse files
author
Oleksandr Dzhychko
committed
test(vue-model-api): test eviction from Cache after garbage collection
1 parent 958ed0f commit 5720ecf

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

vue-model-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
],
1515
"scripts": {
1616
"build": "tsc --build",
17-
"test": "jest",
17+
"test": "node --expose-gc node_modules/.bin/jest",
1818
"prettier": "prettier . --check",
1919
"prettier:fix": "npm run prettier -- --write"
2020
},

vue-model-api/src/internal/Cache.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { INodeJS } from "@modelix/ts-model-api";
22
import { Cache } from "./Cache";
3+
import { runGarbageCollection } from "./runGarbageCollection";
34

45
test("wrapper is added to cache", () => {
56
const cache = new Cache<{ id: string }>();
@@ -17,3 +18,16 @@ test("wrapper is added to cache", () => {
1718
const wrapped2 = cache.memoize(node, wrap2);
1819
expect(wrapped2.id).toBe("aWrapper1");
1920
});
21+
22+
test("wrapper is removed from cache after garbage collection", async () => {
23+
const iNode = {
24+
getReference() {
25+
return "myKey";
26+
},
27+
} as INodeJS;
28+
const cache = new Cache();
29+
cache.memoize(iNode, () => ({}));
30+
expect(cache.get(iNode)).not.toBeUndefined();
31+
await runGarbageCollection();
32+
expect(cache.get(iNode)).toBeUndefined();
33+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { runGarbageCollection } from "./runGarbageCollection";
2+
3+
test("test garbage collection invocation", async () => {
4+
const weakRef = new WeakRef({});
5+
await runGarbageCollection();
6+
expect(weakRef.deref()).toBeUndefined();
7+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Force garbage collection to run.
3+
* See https://github.com/orgs/nodejs/discussions/36467
4+
**/
5+
export async function runGarbageCollection() {
6+
// Enqueue a micro task is needed to run before garbage collection
7+
// See https://github.com/orgs/nodejs/discussions/36467#discussioncomment-3367722
8+
await new Promise((resolve) => setTimeout(resolve, 0));
9+
if (global.gc) {
10+
global.gc();
11+
} else {
12+
fail(
13+
"Garbage collection unavailable. Pass --expose-gc when launching node to enable forced garbage collection.",
14+
);
15+
}
16+
}

vue-model-api/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "@tsconfig/recommended/tsconfig.json",
33
"include": ["src/**/*.ts"],
4-
"exclude": ["src/**/*.test.ts"],
4+
"exclude": ["src/**/*.test.ts", "src/**/runGarbageCollection.ts"],
55
"compilerOptions": {
66
"composite": true,
77
"rootDir": "src",

0 commit comments

Comments
 (0)