From 936984ec35c165358e48f88b8b5ebfad3c8eab06 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 8 Feb 2023 14:35:21 +0100 Subject: [PATCH 1/3] waitUntil rather than delay --- .../core/test/lib/transports/offline.test.ts | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/core/test/lib/transports/offline.test.ts b/packages/core/test/lib/transports/offline.test.ts index 95d274d20d77..1d7fbcf8736a 100644 --- a/packages/core/test/lib/transports/offline.test.ts +++ b/packages/core/test/lib/transports/offline.test.ts @@ -115,8 +115,19 @@ function createTestStore(...popResults: MockResult[]): { }; } -function delay(ms: number): Promise { - return new Promise(resolve => setTimeout(resolve, ms)); +function waitUntil(fn: () => boolean, timeout: number): Promise { + return new Promise((resolve, reject) => { + let runtime = 0; + + const interval = setInterval(() => { + runtime += 100; + + if (fn() || runtime >= timeout) { + clearTimeout(interval); + resolve(); + } + }, 100); + }); } describe('makeOfflineTransport', () => { @@ -138,7 +149,7 @@ describe('makeOfflineTransport', () => { expect(queuedCount).toEqual(0); expect(getSendCount()).toEqual(1); - await delay(MIN_DELAY * 2); + await waitUntil(() => getCalls().length == 1, 1_000); // After a successful send, the store should be checked expect(getCalls()).toEqual(['pop']); @@ -152,7 +163,7 @@ describe('makeOfflineTransport', () => { expect(result).toEqual({ statusCode: 200 }); - await delay(MIN_DELAY * 3); + await waitUntil(() => getCalls().length == 2, 1_000); expect(getSendCount()).toEqual(2); // After a successful send from the store, the store should be checked again to ensure it's empty @@ -175,7 +186,7 @@ describe('makeOfflineTransport', () => { expect(result).toEqual({}); - await delay(MIN_DELAY * 2); + await waitUntil(() => getCalls().length === 1, 1_000); expect(getSendCount()).toEqual(0); expect(queuedCount).toEqual(1); @@ -198,7 +209,7 @@ describe('makeOfflineTransport', () => { expect(result).toEqual({ statusCode: 500 }); - await delay(MIN_DELAY * 2); + await waitUntil(() => getSendCount() === 1, 1_000); expect(getSendCount()).toEqual(1); expect(queuedCount).toEqual(0); @@ -215,7 +226,7 @@ describe('makeOfflineTransport', () => { expect(result).toEqual({}); expect(getCalls()).toEqual(['add']); - await delay(START_DELAY + 1_000); + await waitUntil(() => getCalls().length === 3 && getSendCount() === 1, 7_000); expect(getSendCount()).toEqual(1); expect(getCalls()).toEqual(['add', 'pop', 'pop']); @@ -235,7 +246,7 @@ describe('makeOfflineTransport', () => { flushAtStartup: true, }); - await delay(START_DELAY + 1_000); + await waitUntil(() => getCalls().length === 3 && getSendCount() === 2, 7_000); expect(getSendCount()).toEqual(2); expect(getCalls()).toEqual(['pop', 'pop', 'pop']); @@ -303,11 +314,11 @@ describe('makeOfflineTransport', () => { headers: { 'x-sentry-rate-limits': '', 'retry-after': '1' }, }); - await delay(MIN_DELAY * 2); + await waitUntil(() => getSendCount() === 1, 500); expect(getSendCount()).toEqual(1); - await delay(3_000); + await waitUntil(() => getCalls().length === 2, 6_000); expect(getSendCount()).toEqual(2); expect(queuedCount).toEqual(0); From 4b992b9f53e4adcc62a4faca0389599da8998e18 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 8 Feb 2023 14:45:57 +0100 Subject: [PATCH 2/3] Fix linting --- packages/core/test/lib/transports/offline.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/test/lib/transports/offline.test.ts b/packages/core/test/lib/transports/offline.test.ts index 1d7fbcf8736a..15f3fc25ff85 100644 --- a/packages/core/test/lib/transports/offline.test.ts +++ b/packages/core/test/lib/transports/offline.test.ts @@ -18,7 +18,7 @@ import { TextEncoder } from 'util'; import { createTransport } from '../../../src'; import type { CreateOfflineStore, OfflineTransportOptions } from '../../../src/transports/offline'; -import { makeOfflineTransport, MIN_DELAY, START_DELAY } from '../../../src/transports/offline'; +import { makeOfflineTransport, START_DELAY } from '../../../src/transports/offline'; const ERROR_ENVELOPE = createEnvelope({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [ [{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem, @@ -116,7 +116,7 @@ function createTestStore(...popResults: MockResult[]): { } function waitUntil(fn: () => boolean, timeout: number): Promise { - return new Promise((resolve, reject) => { + return new Promise(resolve => { let runtime = 0; const interval = setInterval(() => { From 4e95ce8baf155643bcb8d1d27282c44c8ea9ef63 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 8 Feb 2023 15:39:36 +0100 Subject: [PATCH 3/3] Use START_DELAY --- .../core/test/lib/transports/offline.test.ts | 70 ++++++++++--------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/packages/core/test/lib/transports/offline.test.ts b/packages/core/test/lib/transports/offline.test.ts index 15f3fc25ff85..ad38f0363279 100644 --- a/packages/core/test/lib/transports/offline.test.ts +++ b/packages/core/test/lib/transports/offline.test.ts @@ -226,7 +226,7 @@ describe('makeOfflineTransport', () => { expect(result).toEqual({}); expect(getCalls()).toEqual(['add']); - await waitUntil(() => getCalls().length === 3 && getSendCount() === 1, 7_000); + await waitUntil(() => getCalls().length === 3 && getSendCount() === 1, START_DELAY * 2); expect(getSendCount()).toEqual(1); expect(getCalls()).toEqual(['add', 'pop', 'pop']); @@ -246,7 +246,7 @@ describe('makeOfflineTransport', () => { flushAtStartup: true, }); - await waitUntil(() => getCalls().length === 3 && getSendCount() === 2, 7_000); + await waitUntil(() => getCalls().length === 3 && getSendCount() === 2, START_DELAY * 2); expect(getSendCount()).toEqual(2); expect(getCalls()).toEqual(['pop', 'pop', 'pop']); @@ -288,40 +288,44 @@ describe('makeOfflineTransport', () => { expect(getCalls()).toEqual([]); }); - it('Follows the Retry-After header', async () => { - const { getCalls, store } = createTestStore(ERROR_ENVELOPE); - const { getSendCount, baseTransport } = createTestTransport( - { - statusCode: 429, - headers: { 'x-sentry-rate-limits': '', 'retry-after': '1' }, - }, - { statusCode: 200 }, - ); - - let queuedCount = 0; - const transport = makeOfflineTransport(baseTransport)({ - ...transportOptions, - createStore: store, - shouldStore: () => { - queuedCount += 1; - return true; - }, - }); - const result = await transport.send(ERROR_ENVELOPE); + it( + 'Follows the Retry-After header', + async () => { + const { getCalls, store } = createTestStore(ERROR_ENVELOPE); + const { getSendCount, baseTransport } = createTestTransport( + { + statusCode: 429, + headers: { 'x-sentry-rate-limits': '', 'retry-after': '3' }, + }, + { statusCode: 200 }, + ); + + let queuedCount = 0; + const transport = makeOfflineTransport(baseTransport)({ + ...transportOptions, + createStore: store, + shouldStore: () => { + queuedCount += 1; + return true; + }, + }); + const result = await transport.send(ERROR_ENVELOPE); - expect(result).toEqual({ - statusCode: 429, - headers: { 'x-sentry-rate-limits': '', 'retry-after': '1' }, - }); + expect(result).toEqual({ + statusCode: 429, + headers: { 'x-sentry-rate-limits': '', 'retry-after': '3' }, + }); - await waitUntil(() => getSendCount() === 1, 500); + await waitUntil(() => getSendCount() === 1, 500); - expect(getSendCount()).toEqual(1); + expect(getSendCount()).toEqual(1); - await waitUntil(() => getCalls().length === 2, 6_000); + await waitUntil(() => getCalls().length === 2, START_DELAY * 2); - expect(getSendCount()).toEqual(2); - expect(queuedCount).toEqual(0); - expect(getCalls()).toEqual(['pop', 'pop']); - }, 7_000); + expect(getSendCount()).toEqual(2); + expect(queuedCount).toEqual(0); + expect(getCalls()).toEqual(['pop', 'pop']); + }, + START_DELAY * 3, + ); });