diff --git a/src/index.ts b/src/index.ts index 75b9d20..d1a1d14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,11 +30,13 @@ async function main() { const appleNotificationSender = new AppleNotificationSender(false); notificationRepository = new InMemoryNotificationRepository(); + notificationService = new ETANotificationScheduler( shuttleRepository, notificationRepository, appleNotificationSender ); + notificationService.startListeningForUpdates(); } else { const repositoryDataUpdater = new TimedApiBasedShuttleRepositoryLoader( shuttleRepository, @@ -46,6 +48,7 @@ async function main() { shuttleRepository, notificationRepository ); + notificationService.startListeningForUpdates(); } const { url } = await startStandaloneServer(server, { diff --git a/test/notifications/schedulers/ETANotificationSchedulerTests.test.ts b/test/notifications/schedulers/ETANotificationSchedulerTests.test.ts index cadee33..974774d 100644 --- a/test/notifications/schedulers/ETANotificationSchedulerTests.test.ts +++ b/test/notifications/schedulers/ETANotificationSchedulerTests.test.ts @@ -1,7 +1,6 @@ import { beforeEach, describe, expect, it, jest } from "@jest/globals"; import { ETANotificationScheduler } from "../../../src/notifications/schedulers/ETANotificationScheduler"; import { UnoptimizedInMemoryShuttleRepository } from "../../../src/repositories/UnoptimizedInMemoryShuttleRepository"; -import http2 from "http2"; import { IEta, IShuttle, IStop } from "../../../src/entities/entities"; import { addMockShuttleToRepository, addMockStopToRepository } from "../../testHelpers/repositorySetupHelpers"; import { AppleNotificationSender } from "../../../src/notifications/senders/AppleNotificationSender"; @@ -17,23 +16,6 @@ function mockNotificationSenderMethods(shouldSimulateNotificationSend: boolean) MockAppleNotificationSender.prototype.sendNotificationImmediately = jest.fn(async () => shouldSimulateNotificationSend); } -/** - * Wait for a condition to become true until the timeout - * is hit. - * @param condition - * @param timeoutMilliseconds - * @param intervalMilliseconds - */ -async function waitForCondition(condition: () => boolean, timeoutMilliseconds = 5000, intervalMilliseconds = 500) { - const startTime = Date.now(); - while (!condition()) { - if (Date.now() - startTime > timeoutMilliseconds) { - throw new Error("Timeout waiting for condition"); - } - await new Promise((resolve) => setTimeout(resolve, intervalMilliseconds)); - } -} - /** * Wait for a specified number of milliseconds. * @param ms @@ -60,6 +42,7 @@ describe("ETANotificationScheduler", () => { notificationRepository, appleNotificationSender ); + notificationService.startListeningForUpdates(); }); function generateNotificationDataAndEta(shuttle: IShuttle, stop: IStop) { @@ -97,9 +80,8 @@ describe("ETANotificationScheduler", () => { await shuttleRepository.addOrUpdateEta(eta); // Assert - // Because repository publisher calls subscriber without await - // wait for the change to occur first - await waitForCondition(() => !notificationRepository.isNotificationScheduled(notificationData1)); + // Wait for the callback to actually be called + await waitForMilliseconds(1000); const isFirstNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData1); const isSecondNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData2); @@ -132,12 +114,18 @@ describe("ETANotificationScheduler", () => { const stop = await addMockStopToRepository(shuttleRepository, "1"); const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop) + // replace the old notification scheduler with a new one + // detach the old callback method from the shuttle repo + notificationService.stopListeningForUpdates(); + mockNotificationSenderMethods(false); + const updatedNotificationSender = new MockAppleNotificationSender(false); notificationService = new ETANotificationScheduler( shuttleRepository, - new InMemoryNotificationRepository(), - new MockAppleNotificationSender(), - ) + notificationRepository, + updatedNotificationSender + ); + notificationService.startListeningForUpdates(); // Act await notificationRepository.addOrUpdateNotification(notificationData1);