Skip to content

Commit 9b33e48

Browse files
committed
fix tests
1 parent ca6b068 commit 9b33e48

2 files changed

Lines changed: 67 additions & 6 deletions

File tree

packages/server-utils/test/orchestrion/postgres-ignore-connect.test.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { tracingChannel } from 'node:diagnostics_channel';
33
import type { Scope } from '@sentry/core';
44
import {
55
_INTERNAL_setSpanForScope,
6+
Client,
7+
createTransport,
68
getDefaultCurrentScope,
79
getDefaultIsolationScope,
10+
resolvedSyncPromise,
811
setAsyncContextStrategy,
912
} from '@sentry/core';
1013
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
@@ -16,7 +19,28 @@ interface TestStore {
1619
isolationScope: Scope;
1720
}
1821

19-
// `setupOnce` only subscribes once `waitForTracingChannelBinding` sees an
22+
class TestClient extends Client<any> {
23+
public constructor(options: any) {
24+
super(options);
25+
}
26+
public eventFromException(): PromiseLike<any> {
27+
return resolvedSyncPromise({});
28+
}
29+
public eventFromMessage(): PromiseLike<any> {
30+
return resolvedSyncPromise({});
31+
}
32+
}
33+
34+
function createTestClient(): Client {
35+
return new TestClient({
36+
dsn: 'https://username@domain/123',
37+
integrations: [],
38+
transport: () => createTransport({ recordDroppedEvent: () => undefined }, () => resolvedSyncPromise({})),
39+
stackParser: () => [],
40+
});
41+
}
42+
43+
// `setup` only subscribes once `waitForTracingChannelBinding` sees an
2044
// async-context strategy exposing `getTracingChannelBinding`. Install a
2145
// minimal one so the subscriptions actually register here.
2246
function installTestAsyncContextStrategy(): void {
@@ -59,7 +83,7 @@ function installTestAsyncContextStrategy(): void {
5983
});
6084
}
6185

62-
// `setupOnce` subscribes to process-global `tracingChannel`s, so asserting the
86+
// `setup` subscribes to process-global `tracingChannel`s, so asserting the
6387
// ABSENCE of connect subscribers only holds when no other (default-options)
6488
// integration in the same module context has subscribed. vitest isolates
6589
// module state per file, so this file keeps that assertion clean (the default
@@ -74,7 +98,11 @@ describe('postgresChannelIntegration({ ignoreConnectSpans: true })', () => {
7498
});
7599

76100
it('subscribes to the query channel but NOT the connect / pool-connect channels', () => {
77-
postgresChannelIntegration({ ignoreConnectSpans: true }).setupOnce?.();
101+
const client = createTestClient();
102+
postgresChannelIntegration({ ignoreConnectSpans: true }).setup?.(client);
103+
// Channel subscribers only register once orchestrion reports a matching
104+
// module as injected.
105+
client.emit('orchestrion.module-runtime-injected', 'pg');
78106

79107
expect(tracingChannel(CHANNELS.PG_QUERY).start.hasSubscribers).toBe(true);
80108
expect(tracingChannel(CHANNELS.PG_CONNECT).start.hasSubscribers).toBe(false);

packages/server-utils/test/orchestrion/postgres.test.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import type { Scope, Span } from '@sentry/core';
44
import * as SentryCore from '@sentry/core';
55
import {
66
_INTERNAL_setSpanForScope,
7+
Client,
8+
createTransport,
79
getDefaultCurrentScope,
810
getDefaultIsolationScope,
11+
resolvedSyncPromise,
912
setAsyncContextStrategy,
1013
} from '@sentry/core';
1114
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, type MockInstance, vi } from 'vitest';
@@ -17,7 +20,28 @@ interface TestStore {
1720
isolationScope: Scope;
1821
}
1922

20-
// `bindTracingChannelToSpan` only binds (and `setupOnce` only subscribes via
23+
class TestClient extends Client<any> {
24+
public constructor(options: any) {
25+
super(options);
26+
}
27+
public eventFromException(): PromiseLike<any> {
28+
return resolvedSyncPromise({});
29+
}
30+
public eventFromMessage(): PromiseLike<any> {
31+
return resolvedSyncPromise({});
32+
}
33+
}
34+
35+
function createTestClient(): Client {
36+
return new TestClient({
37+
dsn: 'https://username@domain/123',
38+
integrations: [],
39+
transport: () => createTransport({ recordDroppedEvent: () => undefined }, () => resolvedSyncPromise({})),
40+
stackParser: () => [],
41+
});
42+
}
43+
44+
// `bindTracingChannelToSpan` only binds (and `setup` only subscribes via
2145
// `waitForTracingChannelBinding`) when an async-context strategy exposes a
2246
// `getTracingChannelBinding`. Install a minimal one so the channel
2347
// subscriptions actually register in this unit-test context (no SDK `init`).
@@ -61,6 +85,15 @@ function installTestAsyncContextStrategy(): void {
6185
});
6286
}
6387

88+
// Channel subscribers only register once orchestrion reports a matching module
89+
// as injected (`invokeOrchestrionInstrumentation`). Emit that event after
90+
// `setup` so subscriptions land synchronously under the test strategy above.
91+
function setupPostgresChannelIntegration(options?: { ignoreConnectSpans?: boolean }): void {
92+
const client = createTestClient();
93+
postgresChannelIntegration(options).setup?.(client);
94+
client.emit('orchestrion.module-runtime-injected', 'pg');
95+
}
96+
6497
// The subscriber builds spans via `startInactiveSpan` and gates on
6598
// `getActiveSpan`. We spy both: `getActiveSpan` to satisfy the
6699
// requireParentSpan gate, and `startInactiveSpan` to capture the span
@@ -84,10 +117,10 @@ describe('postgresChannelIntegration', () => {
84117

85118
// Subscribe once for the whole file so a single subscriber handles each
86119
// publish (avoids accumulating duplicate subscriptions across tests). The
87-
// strategy must be installed first so `setupOnce`'s `waitForTracingChannelBinding` fires synchronously.
120+
// strategy must be installed first so `setup`'s `waitForTracingChannelBinding` fires synchronously.
88121
beforeAll(() => {
89122
installTestAsyncContextStrategy();
90-
postgresChannelIntegration().setupOnce?.();
123+
setupPostgresChannelIntegration();
91124
});
92125

93126
afterAll(() => {

0 commit comments

Comments
 (0)