Skip to content

Commit 35f9525

Browse files
CopilotLms24
andauthored
fix(node-integration-tests): fix flaky kafkajs test by making transaction order independent
The test was flaky because the producer and consumer transactions could arrive in either order, but the test asserted them in a fixed sequence. Now both transactions are collected and assertions are performed after both have been received, regardless of arrival order. Fixes #20121 Co-Authored-By: Claude <noreply@anthropic.com> Agent-Logs-Url: https://github.com/getsentry/sentry-javascript/sessions/0bc166af-c93a-42a6-a762-dae244893e2b Co-authored-by: Lms24 <8420481+Lms24@users.noreply.github.com>
1 parent 86dc30a commit 35f9525

File tree

1 file changed

+26
-13
lines changed
  • dev-packages/node-integration-tests/suites/tracing/kafkajs

1 file changed

+26
-13
lines changed

dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { TransactionEvent } from '@sentry/core';
12
import { afterAll, describe, expect } from 'vitest';
23
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner';
34

@@ -8,16 +9,32 @@ describe('kafkajs', () => {
89

910
createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
1011
test('traces producers and consumers', { timeout: 60_000 }, async () => {
12+
// The producer and consumer transactions can arrive in any order,
13+
// so we collect them and assert after both have been received.
14+
const receivedTransactions: TransactionEvent[] = [];
15+
1116
await createRunner()
1217
.withDockerCompose({
1318
workingDirectory: [__dirname],
1419
readyMatches: ['9092'],
1520
})
1621
.expect({
17-
transaction: {
18-
transaction: 'send test-topic',
19-
contexts: {
20-
trace: expect.objectContaining({
22+
transaction: (transaction: TransactionEvent) => {
23+
receivedTransactions.push(transaction);
24+
},
25+
})
26+
.expect({
27+
transaction: (transaction: TransactionEvent) => {
28+
receivedTransactions.push(transaction);
29+
30+
const producer = receivedTransactions.find(t => t.transaction === 'send test-topic');
31+
const consumer = receivedTransactions.find(t => t.transaction === 'process test-topic');
32+
33+
expect(producer).toBeDefined();
34+
expect(consumer).toBeDefined();
35+
36+
expect(producer!.contexts?.trace).toMatchObject(
37+
expect.objectContaining({
2138
op: 'message',
2239
status: 'ok',
2340
data: expect.objectContaining({
@@ -28,14 +45,10 @@ describe('kafkajs', () => {
2845
'sentry.origin': 'auto.kafkajs.otel.producer',
2946
}),
3047
}),
31-
},
32-
},
33-
})
34-
.expect({
35-
transaction: {
36-
transaction: 'process test-topic',
37-
contexts: {
38-
trace: expect.objectContaining({
48+
);
49+
50+
expect(consumer!.contexts?.trace).toMatchObject(
51+
expect.objectContaining({
3952
op: 'message',
4053
status: 'ok',
4154
data: expect.objectContaining({
@@ -46,7 +59,7 @@ describe('kafkajs', () => {
4659
'sentry.origin': 'auto.kafkajs.otel.consumer',
4760
}),
4861
}),
49-
},
62+
);
5063
},
5164
})
5265
.start()

0 commit comments

Comments
 (0)