-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathintegration.worker.test.ts
More file actions
97 lines (78 loc) · 3.21 KB
/
integration.worker.test.ts
File metadata and controls
97 lines (78 loc) · 3.21 KB
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
import type { ProfilingIntegration, Transport } from '@sentry/core';
import * as Sentry from '@sentry/node';
import { CpuProfilerBindings } from '@sentry-internal/node-cpu-profiler';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { _nodeProfilingIntegration } from '../src/integration';
// Mock the modules before the import, so that the value is initialized before the module is loaded
vi.mock('worker_threads', () => {
return {
isMainThread: false,
threadId: 9999,
};
});
vi.setConfig({ testTimeout: 10_000 });
function makeClient(options: Partial<Sentry.NodeOptions> = {}): [Sentry.NodeClient, Transport] {
const integration = _nodeProfilingIntegration();
const client = new Sentry.NodeClient({
stackParser: Sentry.defaultStackParser,
tracesSampleRate: 1,
debug: true,
environment: 'test-environment',
dsn: 'https://7fa19397baaf433f919fbe02228d5470@o1137848.ingest.sentry.io/6625302',
integrations: [integration],
transport: _opts =>
Sentry.makeNodeTransport({
url: 'https://7fa19397baaf433f919fbe02228d5470@o1137848.ingest.sentry.io/6625302',
recordDroppedEvent: () => {
return undefined;
},
}),
...options,
});
return [client, client.getTransport() as Transport];
}
describe('worker threads', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('does not start continuous profiling in worker threads', () => {
const startProfilingSpy = vi.spyOn(CpuProfilerBindings, 'startProfiling');
const [client] = makeClient();
Sentry.setCurrentClient(client);
client.init();
const integration = client.getIntegrationByName<ProfilingIntegration<any>>('ProfilingIntegration');
if (!integration) {
throw new Error('Profiling integration not found');
}
// Calling start should be a no-op in a worker thread
integration._profiler.start();
const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
transaction.end();
// The native profiler should never have been called
expect(startProfilingSpy).not.toHaveBeenCalled();
integration._profiler.stop();
});
it('does not start span profiling in worker threads', () => {
const startProfilingSpy = vi.spyOn(CpuProfilerBindings, 'startProfiling');
const [client] = makeClient({ profilesSampleRate: 1 });
Sentry.setCurrentClient(client);
client.init();
const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
transaction.end();
// The native profiler should never have been called even with profilesSampleRate set
expect(startProfilingSpy).not.toHaveBeenCalled();
});
it('does not start trace lifecycle profiling in worker threads', () => {
const startProfilingSpy = vi.spyOn(CpuProfilerBindings, 'startProfiling');
const [client] = makeClient({
profileSessionSampleRate: 1.0,
profileLifecycle: 'trace',
});
Sentry.setCurrentClient(client);
client.init();
const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
transaction.end();
// The native profiler should never have been called
expect(startProfilingSpy).not.toHaveBeenCalled();
});
});