Skip to content

Commit

Permalink
Cleanup failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGinnivan committed Dec 11, 2023
1 parent 141d3cf commit be98aae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 52 deletions.
66 changes: 17 additions & 49 deletions libs/js-sdk/src/utils/retry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,25 @@
import { trace } from '@opentelemetry/api'
import {
BasicTracerProvider,
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { expect, it } from 'vitest'
import { retry } from './retry'

describe('retry function with OpenTelemetry', () => {
let exporter: InMemorySpanExporter

beforeEach(() => {
// Set up in-memory exporter
exporter = new InMemorySpanExporter()
const provider = new BasicTracerProvider()
provider.addSpanProcessor(new SimpleSpanProcessor(exporter))
trace.setGlobalTracerProvider(provider)
})

afterEach(() => {
// Clear the spans and reset the tracer
exporter.reset()
})

it('should retry the function and succeed', async () => {
let attempt = 0
const mockFn = async () => {
if (attempt < 1) {
attempt++
throw new Error('Temporary failure')
}
return 'Success'
it('should retry the function and succeed', async () => {
let attempt = 0
const mockFn = async () => {
if (attempt < 1) {
attempt++
throw new Error('Temporary failure')
}
return 'Success'
}

const result = await retry(mockFn)
const result = await retry(mockFn)

expect(result).toBe('Success')
const spans = exporter.getFinishedSpans()
console.log(spans)
expect(spans.length).toEqual(4)
expect(spans[0].name).toBe('retry-attempt')
expect(spans[0].attributes?.retryAttempt).toBe(0)
expect(spans[1].name).toEqual('retry')
expect(spans[2].name).toEqual('delay')
expect(spans[3].name).toBe('retry-attempt')
expect(spans[3].attributes?.retryAttempt).toBe(1)
})
expect(result).toBe('Success')
})

it('should retry the function and fail', async () => {
const mockFn = async () => {
throw new Error('Temporary failure')
}
it('should retry the function and fail', async () => {
const mockFn = async () => {
throw new Error('Temporary failure')
}

await expect(retry(mockFn)).rejects.toThrow('Temporary failure')
})
await expect(retry(mockFn)).rejects.toThrow('Temporary failure')
})
6 changes: 3 additions & 3 deletions libs/node-sdk/src/tests/http-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ describe('http client', () => {
await expect(async () => {
await client.waitForInitialised()
}).rejects.toThrowError('500')
expect(count).toEqual(5 + 1) // initial request and 5 retry
expect(count).toEqual(2 + 1) // initial request and 5 retry
} finally {
server.resetHandlers()
server.close()
Expand Down Expand Up @@ -398,8 +398,8 @@ describe('http client', () => {
await expect(async () => {
await client.waitForInitialised()
}).rejects.toThrowError('Test External State Store Error')
expect(countAPIRequest).toEqual(5 + 1) // initial request and 5 retry
expect(countExternalStateStoreRequest).toEqual(5 + 1) // initial request and 5 retry
expect(countAPIRequest).toEqual(2 + 1) // initial request and 5 retry
expect(countExternalStateStoreRequest).toEqual(2 + 1) // initial request and 5 retry
} finally {
server.resetHandlers()
server.close()
Expand Down

0 comments on commit be98aae

Please sign in to comment.