Skip to content

Commit

Permalink
Fixed retry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGinnivan committed Dec 11, 2023
1 parent 3dc8ff9 commit d77f7fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 8 additions & 6 deletions libs/js-sdk/src/utils/retry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('retry function with OpenTelemetry', () => {
it('should retry the function and succeed', async () => {
let attempt = 0
const mockFn = async () => {
if (attempt < 2) {
if (attempt < 1) {
attempt++
throw new Error('Temporary failure')
}
Expand All @@ -37,12 +37,14 @@ describe('retry function with OpenTelemetry', () => {

expect(result).toBe('Success')
const spans = exporter.getFinishedSpans()
expect(spans.length).toEqual(5)
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('delay')
expect(spans[2].attributes?.retryAttempt).toBe(1)
expect(spans[3].name).toEqual('delay')
expect(spans[4].attributes?.retryAttempt).toBe(2)
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)
})

it('should retry the function and fail', async () => {
Expand Down
8 changes: 5 additions & 3 deletions libs/js-sdk/src/utils/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ export async function retry<T>(
// eslint-disable-next-line no-constant-condition
while (true) {
try {
return await retryAttemptFn<T>(tracer, retryAttempt, fn).then(
() => span.end(),
)
return await retryAttemptFn<T>(
tracer,
retryAttempt,
fn,
).finally(() => span.end())
} catch (error) {
const err = resolveError(error)
if (cancellationToken?.cancel) {
Expand Down

0 comments on commit d77f7fd

Please sign in to comment.