From 179fa07d36f1858f6bee3032190d01621015c02e Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 3 Feb 2025 23:07:55 -0800 Subject: [PATCH] make polling function a global function --- .../services/NotificationServiceTests.test.ts | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/test/services/NotificationServiceTests.test.ts b/test/services/NotificationServiceTests.test.ts index ba6b969..a42512f 100644 --- a/test/services/NotificationServiceTests.test.ts +++ b/test/services/NotificationServiceTests.test.ts @@ -15,6 +15,23 @@ ya+yfGi3g2ZUv6hrfe+j08ytekPHjXS0qzJoVELzKHa6EL9YAoZDXBtB6h+fGhXe SOcONbaf -----END PRIVATE KEY-----` +/** + * Wait for a condition to become true until the timeout + * is hit. + * @param condition + * @param timeout + * @param interval + */ +async function waitForCondition(condition: () => boolean, timeout = 5000, interval = 500) { + const startTime = Date.now(); + while (!condition()) { + if (Date.now() - startTime > timeout) { + throw new Error("Timeout waiting for condition"); + } + await new Promise((resolve) => setTimeout(resolve, interval)); + } +} + describe("NotificationService", () => { let repository: UnoptimizedInMemoryRepository let notificationService: NotificationService; @@ -98,15 +115,6 @@ describe("NotificationService", () => { // Assert // Because repository publisher calls subscriber without await // wait for the change to occur first - async function waitForCondition(condition: () => boolean, timeout = 5000, interval = 500) { - const startTime = Date.now(); - while (!condition()) { - if (Date.now() - startTime > timeout) { - throw new Error("Timeout waiting for condition"); - } - await new Promise((resolve) => setTimeout(resolve, interval)); - } - } await waitForCondition(() => !notificationService.isNotificationScheduled(notificationData1));