From a85c58289525b64dcfa6942a68941d004c842337 Mon Sep 17 00:00:00 2001 From: Stefan Pfaffel Date: Sun, 17 Sep 2023 21:25:36 +0200 Subject: [PATCH] tests: add retries for tests dending on span export --- package.json | 2 +- test/index.spec.js | 73 +++++++++++++++++++++++++++------------------- test/retry.js | 14 +++++++++ 3 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 test/retry.js diff --git a/package.json b/package.json index b38ae98..2eb94b9 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ }, "scripts": { "start": "functions-framework --target=api --signature-type=http", - "test": "mocha --timeout 5000 test/**/*spec.js --file test/global-mocha-setup.js --file ./lib/instrumentation.cjs", + "test": "mocha --timeout 10000 test/**/*spec.js --file test/global-mocha-setup.js --file ./lib/instrumentation.cjs", "test:ci": "npm run test -- --forbid-only", "coverage": "nyc c8 --100 npm run test:ci", "lint": "npx eslint -c .eslintrc.cjs lib --ext .js,.jsx,.ts,.tsx", diff --git a/test/index.spec.js b/test/index.spec.js index a4e21c5..aabdec7 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -3,6 +3,7 @@ const { createTracer } = require('../lib/index.js') const { fetchSpans } = require('./simple-fetch.js') const simpleFetch = require('./simple-fetch.js') const testServer = require('./test-server.js') +const retry = require('./retry.js') describe('Tracing', () => { after(() => { @@ -16,21 +17,25 @@ describe('Tracing', () => { return simpleFetch('http://127.0.0.1:4444/active-span') }) it('creates a span', async () => { - const spans = await fetchSpans({ spanName }) + await retry(async () => { + const spans = await fetchSpans({ spanName }) - const span = spans.find((s) => s.operationName === spanName) - expect(span.operationName).to.equal(spanName) - const tag = span.tags.find((s) => s.key === 'otel.library.name') - expect(tag.value).to.equal('api-kit') + const span = spans.find((s) => s.operationName === spanName) + expect(span.operationName).to.equal(spanName) + const tag = span.tags.find((s) => s.key === 'otel.library.name') + expect(tag.value).to.equal('api-kit') + }) }) it('adds attributes to the span', async () => { - const spans = await fetchSpans({ spanName }) + await retry(async () => { + const spans = await fetchSpans({ spanName }) - const span = spans.find((s) => s.operationName === spanName) - expect(span.operationName).to.equal(spanName) - const tag = span.tags.find((s) => s.key === 'my') - expect(tag.value).to.equal('attr') + const span = spans.find((s) => s.operationName === spanName) + expect(span.operationName).to.equal(spanName) + const tag = span.tags.find((s) => s.key === 'my') + expect(tag.value).to.equal('attr') + }) }) it('calls the callback with the span as first attribute', (done) => { @@ -52,21 +57,25 @@ describe('Tracing', () => { return simpleFetch('http://127.0.0.1:4444/active-span-sync') }) it('creates a span', async () => { - const spans = await fetchSpans({ spanName }) + await retry(async () => { + const spans = await fetchSpans({ spanName }) - const span = spans.find((s) => s.operationName === spanName) - expect(span.operationName).to.equal(spanName) - const tag = span.tags.find((s) => s.key === 'otel.library.name') - expect(tag.value).to.equal('api-kit') + const span = spans.find((s) => s.operationName === spanName) + expect(span.operationName).to.equal(spanName) + const tag = span.tags.find((s) => s.key === 'otel.library.name') + expect(tag.value).to.equal('api-kit') + }) }) it('adds attributes to the span', async () => { - const spans = await fetchSpans({ spanName }) + await retry(async () => { + const spans = await fetchSpans({ spanName }) - const span = spans.find((s) => s.operationName === spanName) - expect(span.operationName).to.equal(spanName) - const tag = span.tags.find((s) => s.key === 'my') - expect(tag.value).to.equal('attr') + const span = spans.find((s) => s.operationName === spanName) + expect(span.operationName).to.equal(spanName) + const tag = span.tags.find((s) => s.key === 'my') + expect(tag.value).to.equal('attr') + }) }) it('calls the callback with the span as first attribute', (done) => { @@ -88,21 +97,25 @@ describe('Tracing', () => { return simpleFetch('http://127.0.0.1:4444/orphaned-span') }) it('creates a span', async () => { - const spans = await fetchSpans({ spanName }) + await retry(async () => { + const spans = await fetchSpans({ spanName }) - const span = spans.find((s) => s.operationName === spanName) - expect(span.operationName).to.equal(spanName) - const tag = span.tags.find((s) => s.key === 'otel.library.name') - expect(tag.value).to.equal('api-kit') + const span = spans.find((s) => s.operationName === spanName) + expect(span.operationName).to.equal(spanName) + const tag = span.tags.find((s) => s.key === 'otel.library.name') + expect(tag.value).to.equal('api-kit') + }) }) it('adds attributes to the span', async () => { - const spans = await fetchSpans({ spanName }) + await retry(async () => { + const spans = await fetchSpans({ spanName }) - const span = spans.find((s) => s.operationName === spanName) - expect(span.operationName).to.equal(spanName) - const tag = span.tags.find((s) => s.key === 'my') - expect(tag.value).to.equal('attr') + const span = spans.find((s) => s.operationName === spanName) + expect(span.operationName).to.equal(spanName) + const tag = span.tags.find((s) => s.key === 'my') + expect(tag.value).to.equal('attr') + }) }) it('calls the callback with the span as first attribute', (done) => { diff --git a/test/retry.js b/test/retry.js new file mode 100644 index 0000000..6b3c1f2 --- /dev/null +++ b/test/retry.js @@ -0,0 +1,14 @@ +module.exports = async (callback, { maxRetries = 10, delay = 250, backOff = 2 } = {}) => { + for (let i = 1; i <= maxRetries; i++) { + try { + await callback() + } catch (e) { + if (i === maxRetries) { + throw e + } + + console.log(`Caught error ${e}. Will retry ${maxRetries - i} more times.`) + await new Promise((resolve) => setTimeout(resolve, delay * i * backOff)) + } + } +} \ No newline at end of file