From c94e484b898a236f64871725925c26cd33839709 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 28 Jan 2025 10:06:58 +0200 Subject: [PATCH] Fix flakey user indicator controller tests; switch them over to the deferred fulfillment. --- .../UserIndicatorControllerTests.swift | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/UnitTests/Sources/UserIndicatorControllerTests.swift b/UnitTests/Sources/UserIndicatorControllerTests.swift index f50c5b0801..9c3ddc7273 100644 --- a/UnitTests/Sources/UserIndicatorControllerTests.swift +++ b/UnitTests/Sources/UserIndicatorControllerTests.swift @@ -42,7 +42,7 @@ class UserIndicatorControllerTests: XCTestCase { XCTAssertEqual(indicatorController.indicatorQueue.count, 0) } - func testChainedPresentation() { + func testChainedPresentation() async throws { indicatorController.minimumDisplayDuration = 0.25 indicatorController.nonPersistentDisplayDuration = 2.5 @@ -52,18 +52,17 @@ class UserIndicatorControllerTests: XCTestCase { XCTAssertEqual(indicatorController.activeIndicator?.id, "Third") - let expectation = expectation(description: "Waiting for last indicator to be dismissed") - DispatchQueue.main.asyncAfter(deadline: .now() + indicatorController.nonPersistentDisplayDuration) { - expectation.fulfill() + let fulfillment = deferFulfillment(indicatorController.$activeIndicator, message: "Waiting for last indicator to be dismissed") { indicator in + indicator?.id == "Second" } - waitForExpectations(timeout: 5.0) + try await fulfillment.fulfill() XCTAssertEqual(indicatorController.indicatorQueue.count, 2) XCTAssertEqual(indicatorController.activeIndicator?.id, "Second") } - func testMinimumDisplayDuration() { + func testMinimumDisplayDuration() async throws { indicatorController.minimumDisplayDuration = 0.25 indicatorController.nonPersistentDisplayDuration = 2.5 @@ -71,28 +70,26 @@ class UserIndicatorControllerTests: XCTestCase { indicatorController.submitIndicator(.init(id: "Second", title: "")) indicatorController.submitIndicator(.init(id: "Third", title: "")) - indicatorController.retractIndicatorWithId("Second") - XCTAssertEqual(indicatorController.indicatorQueue.count, 3) - let dismissalExpectation = expectation(description: "Waiting for minimum display duration to pass") - DispatchQueue.main.asyncAfter(deadline: .now() + indicatorController.minimumDisplayDuration) { - dismissalExpectation.fulfill() + var fulfillment = deferFulfillment(indicatorController.$activeIndicator, message: "Waiting for minimum display duration to pass") { indicator in + indicator?.id == "First" } - waitForExpectations(timeout: 5.0) + indicatorController.retractIndicatorWithId("Second") - XCTAssertEqual(indicatorController.indicatorQueue.count, 2) - XCTAssertEqual(indicatorController.activeIndicator?.id, "Third") + try await fulfillment.fulfill() - let dismissalExpectation2 = expectation(description: "Waiting for last indicator to be dismissed") - DispatchQueue.main.asyncAfter(deadline: .now() + indicatorController.nonPersistentDisplayDuration) { - dismissalExpectation2.fulfill() + XCTAssertEqual(indicatorController.indicatorQueue.count, 1) + XCTAssertEqual(indicatorController.activeIndicator?.id, "First") + + fulfillment = deferFulfillment(indicatorController.$activeIndicator, message: "Waiting for last indicator to be dismissed") { indicator in + indicator == nil } - waitForExpectations(timeout: 5.0) + try await fulfillment.fulfill() - XCTAssertEqual(indicatorController.indicatorQueue.count, 1) - XCTAssertEqual(indicatorController.activeIndicator?.id, "First") + XCTAssertEqual(indicatorController.indicatorQueue.count, 0) + XCTAssertNil(indicatorController.activeIndicator) } }