Skip to content

Commit bd16bcd

Browse files
committed
Add AsyncLocalStorage test
1 parent 0b05d95 commit bd16bcd

File tree

6 files changed

+66
-13
lines changed

6 files changed

+66
-13
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
"fix": "yarn fix:eslint && yarn fix:clang",
2424
"fix:eslint": "eslint . --format stylish --fix",
2525
"fix:clang": "node scripts/clang-format.mjs --fix",
26-
"build": "yarn build:lib && yarn build:bindings:configure && yarn build:bindings",
26+
"build": "yarn clean && yarn build:lib && yarn build:bindings:configure && yarn build:bindings",
2727
"build:lib": "tsc",
2828
"build:bindings:configure": "node-gyp configure",
2929
"build:bindings:configure:arm64": "node-gyp configure --arch=arm64 --target_arch=arm64",
3030
"build:bindings": "node-gyp build && node scripts/copy-target.mjs",
3131
"build:bindings:arm64": "node-gyp build --arch=arm64 && node scripts/copy-target.mjs",
3232
"build:dev": "yarn clean && yarn build:bindings:configure && yarn build",
3333
"build:tarball": "npm pack",
34-
"clean": "node-gyp clean && rm -rf lib && rm -rf build",
34+
"clean": "node-gyp clean && rm -rf lib && rm -rf build && rm -f *.tgz",
3535
"test": "yarn test:install && node ./test/prepare.mjs && vitest run --silent=false --disable-console-intercept",
3636
"test:install": "cross-env ALWAYS_THROW=true yarn install"
3737
},

src/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,7 @@ export function registerThread(threadName: string = String(threadId)): void {
189189
* @param disableLastSeen If true, disables the last seen tracking for this thread.
190190
*/
191191
export function threadPoll(disableLastSeen?: boolean): void {
192-
if (disableLastSeen) {
193-
native.threadPoll(disableLastSeen);
194-
} else {
195-
native.threadPoll();
196-
}
192+
native.threadPoll(!!disableLastSeen);
197193
}
198194

199195
/**

test/async-storage.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { AsyncLocalStorage } from 'node:async_hooks';
2+
import { Worker } from 'node:worker_threads';
3+
import { registerThread, registerThreadStateCallback } from '@sentry-internal/node-native-stacktrace';
4+
import { longWork } from './long-work.js';
5+
6+
registerThread();
7+
8+
const asyncLocalStorage = new AsyncLocalStorage();
9+
10+
function withTraceId(traceId, fn) {
11+
return asyncLocalStorage.run(traceId, fn);
12+
}
13+
14+
registerThreadStateCallback(() => {
15+
const traceId = asyncLocalStorage.getStore();
16+
return { traceId };
17+
});
18+
19+
const watchdog = new Worker('./test/watchdog.js');
20+
21+
for (let i = 0; i < 10; i++) {
22+
withTraceId(`trace-${i}`, () => {
23+
if(i === 5) {
24+
longWork();
25+
}
26+
});
27+
}

test/e2e.test.mjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,38 @@ describe('e2e Tests', { timeout: 20000 }, () => {
9090
expect(stacks['2'].frames.length).toEqual(1);
9191
});
9292

93+
test('async storage state', { timeout: 20000 }, () => {
94+
const testFile = join(__dirname, 'async-storage.mjs');
95+
const result = spawnSync('node', [testFile]);
96+
97+
expect(result.status).toEqual(0);
98+
99+
const stacks = JSON.parse(result.stdout.toString());
100+
101+
expect(stacks['0'].frames).toEqual(expect.arrayContaining([
102+
{
103+
function: 'pbkdf2Sync',
104+
filename: expect.any(String),
105+
lineno: expect.any(Number),
106+
colno: expect.any(Number),
107+
},
108+
{
109+
function: 'longWork',
110+
filename: expect.stringMatching(/long-work.js$/),
111+
lineno: expect.any(Number),
112+
colno: expect.any(Number),
113+
},
114+
{
115+
function: '?',
116+
filename: expect.stringMatching(/async-storage.mjs$/),
117+
lineno: expect.any(Number),
118+
colno: expect.any(Number),
119+
},
120+
]));
121+
122+
expect(stacks['0'].state).toEqual({ traceId: 'trace-5' });
123+
});
124+
93125
test('can be disabled', { timeout: 20000 }, () => {
94126
const testFile = join(__dirname, 'stalled-disabled.js');
95127
const result = spawnSync('node', [testFile]);

test/package.json.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "node-cpu-profiler-test",
2+
"name": "node-native-stacktrace-test",
33
"license": "MIT",
44
"dependencies": {
55
"@sentry-internal/node-native-stacktrace": "{{path}}"

test/prepare.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
21
import { execSync, spawnSync } from 'node:child_process';
3-
import {existsSync,readFileSync, rmSync, writeFileSync } from 'node:fs';
2+
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
43
import { createRequire } from 'node:module';
54
import { dirname, join, relative } from 'node:path';
65
import { fileURLToPath } from 'node:url';
@@ -16,9 +15,8 @@ function installTarballAsDependency(root) {
1615
const tarball = join(__dirname, '..', `${normalizedName}-${pkgJson.version}.tgz`);
1716

1817
if (!existsSync(tarball)) {
19-
console.error(`Tarball not found: '${tarball}'`);
20-
console.error('Run \'yarn build && yarn build:tarball\' first');
21-
process.exit(1);
18+
console.log('Creating tarball...');
19+
execSync('yarn build:tarball', { shell: true, stdio: 'inherit' });
2220
}
2321

2422
const tarballRelative = relative(root, tarball);

0 commit comments

Comments
 (0)