From 06fc6ba6f2d72490d4cbc0028ae6b5a4b4bab9bb Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 6 Dec 2023 12:27:49 -0800 Subject: [PATCH] fix a timing race flakiness in the added koa test 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. --- .../test/fixtures/use-koa.mjs | 6 ++++++ .../node/opentelemetry-instrumentation-koa/test/koa.test.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-koa/test/fixtures/use-koa.mjs b/plugins/node/opentelemetry-instrumentation-koa/test/fixtures/use-koa.mjs index e98c7d6ef5..967e76375b 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/test/fixtures/use-koa.mjs +++ b/plugins/node/opentelemetry-instrumentation-koa/test/fixtures/use-koa.mjs @@ -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'; @@ -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(); }); diff --git a/plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts b/plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts index c3916114de..8b38513a70 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts +++ b/plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts @@ -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');