Skip to content

🎥 Make videos programmatically with React

remove timeout test and make tests run supa fast

Igor Samokhovets437c618171df
diff --git a/packages/licensing/src/register-usage-event.ts b/packages/licensing/src/register-usage-event.ts
index 09ba3e9826..2f3d83dbce 100644
--- a/packages/licensing/src/register-usage-event.ts
+++ b/packages/licensing/src/register-usage-event.ts
@@ -4,11 +4,11 @@ import {isNetworkError} from './is-network-error';
const DEFAULT_MAX_RETRIES = 3;
-const exponentialBackoffMs = (attempt: number): number => {
+export const exponentialBackoffMs = (attempt: number): number => {
return 1000 * 2 ** (attempt - 1);
};
-const sleep = (ms: number): Promise<void> => {
+export const sleep = (ms: number): Promise<void> => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
diff --git a/packages/licensing/src/test/register-usage-event-retry.test.ts b/packages/licensing/src/test/register-usage-event-retry.test.ts
index 9c272a270a..93082dc77e 100644
--- a/packages/licensing/src/test/register-usage-event-retry.test.ts
+++ b/packages/licensing/src/test/register-usage-event-retry.test.ts
@@ -1,7 +1,20 @@
-import {afterAll, afterEach, beforeAll, expect, test} from 'bun:test';
-import {delay, http, HttpResponse} from 'msw';
+import {
+ afterAll,
+ afterEach,
+ beforeAll,
+ beforeEach,
+ expect,
+ spyOn,
+ test,
+} from 'bun:test';
+import {http, HttpResponse} from 'msw';
import {setupServer} from 'msw/node';
-import {HOST, registerUsageEvent} from '../register-usage-event';
+import * as usageEventModule from '../register-usage-event';
+import {
+ exponentialBackoffMs,
+ HOST,
+ registerUsageEvent,
+} from '../register-usage-event';
const server = setupServer();
@@ -9,6 +22,10 @@ beforeAll(() => {
server.listen({onUnhandledRequest: 'error'});
});
+beforeEach(() => {
+ spyOn(usageEventModule, 'sleep').mockImplementation(() => Promise.resolve());
+});
+
afterEach(() => {
server.resetHandlers();
});
@@ -17,6 +34,13 @@ afterAll(() => {
server.close();
});
+test('exponentialBackoffMs returns correct backoff values', () => {
+ expect(exponentialBackoffMs(1)).toBe(1000);
+ expect(exponentialBackoffMs(2)).toBe(2000);
+ expect(exponentialBackoffMs(3)).toBe(4000);
+ expect(exponentialBackoffMs(4)).toBe(8000);
+});
+
test('should succeed on first attempt without retry', async () => {
server.use(
http.post(`${HOST}/api/track/register-usage-point`, () => {
@@ -48,7 +72,6 @@ test('should retry and succeed after transient network error', async () => {
http.post(`${HOST}/api/track/register-usage-point`, () => {
attemptCount++;
if (attemptCount === 1) {
- // Simulate network error on first attempt
return HttpResponse.error();
}
@@ -74,69 +97,17 @@ test('should retry and succeed after transient network error', async () => {
});
});
-test('should retry with exponential backoff timing', async () => {
- const timestamps: number[] = [];
- let attemptCount = 0;
-
- server.use(
- http.post(`${HOST}/api/track/register-usage-point`, () => {
- attemptCount++;
- timestamps.push(Date.now());
-
- if (attemptCount <= 2) {
- // Fail first 2 attempts
- return HttpResponse.error();
- }
-
- return HttpResponse.json({
- success: true,
- billable: true,
- classification: 'billable',
- });
- }),
- );
-
- const startTime = Date.now();
- await registerUsageEvent({
- licenseKey: 'rm_pub_test123',
- host: 'https://test.com',
- succeeded: true,
- event: 'webcodec-conversion',
- });
- const totalTime = Date.now() - startTime;
-
- expect(attemptCount).toBe(3);
- // First retry after ~1000ms, second retry after ~2000ms
- // Total time should be at least 3000ms (1000 + 2000)
- expect(totalTime).toBeGreaterThanOrEqual(2900);
- expect(totalTime).toBeLessThan(4000);
-
- // Verify backoff intervals
- if (timestamps.length >= 2) {
- const firstBackoff = timestamps[1] - timestamps[0];
- expect(firstBackoff).toBeGreaterThanOrEqual(950);
- expect(firstBackoff).toBeLessThan(1200);
- }
-
- if (timestamps.length >= 3) {
- const secondBackoff = timestamps[2] - timestamps[1];
- expect(secondBackoff).toBeGreaterThanOrEqual(1950);
- expect(secondBackoff).toBeLessThan(2200);
- }
-});
-
-test('should exhaust all retries and throw last error', async () => {
+test('should exhaust all retries and throw last error', () => {
let attemptCount = 0;
server.use(
http.post(`${HOST}/api/track/register-usage-point`, () => {
attemptCount++;
- // Always return network error
return HttpResponse.error();
}),
);
- await expect(
+ expect(
registerUsageEvent({
licenseKey: 'rm_pub_test123',
host: 'https://test.com',
@@ -145,11 +116,10 @@ test('should exhaust all retries and throw last error', async () => {
}),
).rejects.toThrow();
- // Should try 4 times total (1 initial + 3 retries)
expect(attemptCount).toBe(4);
});
-test('should not retry on non-retryable errors (invalid API key)', async () => {
+test('should not retry on non-retryable errors (invalid API key)', () => {
let attemptCount = 0;
server.use(
@@ -165,7 +135,7 @@ test('should not retry on non-retryable errors (invalid API key)', async () => {
}),
);
- await expect(
+ expect(
registerUsageEvent({
licenseKey: 'rm_pub_invalid',
host: 'https://test.com',
@@ -174,11 +144,10 @@ test('should not retry on non-retryable errors (invalid API key)', async () => {
}),
).rejects.toThrow('Invalid API key');
- // Should only try once - no retries for non-retryable errors
expect(attemptCount).toBe(1);
});
-test('should not retry on server errors (500)', async () => {
+test('should not retry on server errors (500)', () => {
let attemptCount = 0;
server.use(
@@ -194,7 +163,7 @@ test('should not retry on server errors (500)', async () => {
}),
);
- await expect(
+ expect(
registerUsageEvent({
licenseKey: 'rm_pub_test123',
host: 'https://test.com',
@@ -203,55 +172,15 @@ test('should not retry on server errors (500)', async () => {
}),
).rejects.toThrow('Internal server error');
- // Should only try once
expect(attemptCount).toBe(1);
});
-test('should handle timeout and retry', async () => {
- let attemptCount = 0;
-
- server.use(
- http.post(`${HOST}/api/track/register-usage-point`, async () => {
- attemptCount++;
- if (attemptCount === 1) {
- // Delay longer than 10 second timeout
- await delay(11000);
- return HttpResponse.json({
- success: true,
- billable: true,
- classification: 'billable',
- });
- }
-
- return HttpResponse.json({
- success: true,
- billable: true,
- classification: 'billable',
- });
- }),
- );
-
- const result = await registerUsageEvent({
- licenseKey: 'rm_pub_test123',
- host: 'https://test.com',
- succeeded: true,
- event: 'webcodec-conversion',
- });
-
- expect(attemptCount).toBe(2);
- expect(result).toEqual({
- billable: true,
- classification: 'billable',
- });
-});
-
test('should retry multiple times before succeeding', async () => {
let attemptCount = 0;
server.use(
http.post(`${HOST}/api/track/register-usage-point`, () => {
attemptCount++;
- // Fail first 3 attempts, succeed on 4th (last attempt)
if (attemptCount < 4) {
return HttpResponse.error();
}
@@ -278,14 +207,9 @@ test('should retry multiple times before succeeding', async () => {
});
});
-test('should handle timeout on all attempts and throw timeout error', async () => {
- let attemptCount = 0;
-
+test('should work with apiKey parameter (legacy)', async () => {
server.use(
- http.post(`${HOST}/api/track/register-usage-point`, async () => {
- attemptCount++;
- // Always timeout
- await delay(11000);
+ http.post(`${HOST}/api/track/register-usage-point`, () => {
return HttpResponse.json({
success: true,
billable: true,
@@ -294,22 +218,35 @@ test('should handle timeout on all attempts and throw timeout error', async () =
}),
);
- await expect(
- registerUsageEvent({
- licenseKey: 'rm_pub_test123',
- host: 'https://test.com',
- succeeded: true,
- event: 'webcodec-conversion',
- }),
- ).rejects.toThrow('Request timed out after 10 seconds');
+ const result = await registerUsageEvent({
+ apiKey: 'rm_pub_test123',
+ host: 'https://test.com',
+ succeeded: true,
+ event: 'webcodec-conversion',
+ });
- // Should try 4 times total
- expect(attemptCount).toBe(4);
-}, 60000); // Increase test timeout for this long-running test
+ expect(result).toEqual({
+ billable: true,
+ classification: 'billable',
+ });
+});
+
+test('should call sleep with correct backoff values between retries', async () => {
+ const sleepCalls: number[] = [];
+ spyOn(usageEventModule, 'sleep').mockImplementation((ms: number) => {
+ sleepCalls.push(ms);
+ return Promise.resolve();
+ });
+
+ let attemptCount = 0;
-test('should work with apiKey parameter (legacy)', async () => {
server.use(
http.post(`${HOST}/api/track/register-usage-point`, () => {
+ attemptCount++;
+ if (attemptCount < 4) {
+ return HttpResponse.error();
+ }
+
return HttpResponse.json({
success: true,
billable: true,
@@ -318,15 +255,12 @@ test('should work with apiKey parameter (legacy)', async () => {
}),
);
- const result = await registerUsageEvent({
- apiKey: 'rm_pub_test123',
+ await registerUsageEvent({
+ licenseKey: 'rm_pub_test123',
host: 'https://test.com',
succeeded: true,
event: 'webcodec-conversion',
});
- expect(result).toEqual({
- billable: true,
- classification: 'billable',
- });
+ expect(sleepCalls).toEqual([1000, 2000, 4000]);
});