Skip to content

Commit

Permalink
tests: add retries for tests dending on span export
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Sep 17, 2023
1 parent 66ca898 commit a85c582
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
73 changes: 43 additions & 30 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand Down
14 changes: 14 additions & 0 deletions test/retry.js
Original file line number Diff line number Diff line change
@@ -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))
}
}
}

0 comments on commit a85c582

Please sign in to comment.