-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
141d3cf
commit be98aae
Showing
2 changed files
with
20 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters