Skip to content

Commit

Permalink
fix a timing race flakiness in the added koa test
Browse files Browse the repository at this point in the history
The ESM test case runs use-koa.mjs and expects some spans. It relies
on the ordering of those spans (based on start time). In some runs
two spans were starting in the same millisecond and the sort order
was ill-defined, which caused tests to fail.
  • Loading branch information
trentm committed Dec 6, 2023
1 parent c90b67e commit 06fc6ba
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// Use koa from an ES module:
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-koa.mjs

import { promisify } from 'util';
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';

import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
Expand All @@ -38,6 +39,11 @@ import * as http from 'http';
const app = new Koa();

app.use(async function simpleMiddleware(ctx, next) {
// Wait a short delay to ensure this "middleware - ..." span clearly starts
// before the "router - ..." span. The test rely on a start-time-based sort
// of the produced spans. If they start in the same millisecond, then tests
// can be flaky.
await promisify(setTimeout)(10);
await next();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ describe('Koa Instrumentation', () => {
// a `simpleMiddleware`, then makes a single 'GET /post/0' request. We
// expect to see spans like this:
// span 'GET /post/:id'
// `- span 'middleware - simpleResponse'
// `- span 'middleware - simpleMiddleware'
// `- span 'router - /post/:id'
const spans = collector.sortedSpans;
assert.strictEqual(spans[0].name, 'GET /post/:id');
Expand Down

0 comments on commit 06fc6ba

Please sign in to comment.