From ebbbbd52f7ee72a98f48e28f568528834434f96f Mon Sep 17 00:00:00 2001 From: Leif Date: Fri, 23 Feb 2024 09:55:43 -0700 Subject: [PATCH] Add Waiter to handle threading for tests --- Package.swift | 8 ++++++-- .../Application/Application+public.swift | 20 ++++++++++++++----- .../Dependency/Application+Dependency.swift | 4 ++-- Tests/AppStateTests/AppDependencyTests.swift | 9 ++++++++- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Package.swift b/Package.swift index f1da4e2..40bf75b 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,8 @@ let package = Package( ) ], dependencies: [ - .package(url: "https://github.com/0xLeif/Cache", from: "2.0.0") + .package(url: "https://github.com/0xLeif/Cache", from: "2.0.0"), + .package(url: "https://github.com/0xLeif/Waiter", from: "1.0.0") ], targets: [ .target( @@ -29,7 +30,10 @@ let package = Package( ), .testTarget( name: "AppStateTests", - dependencies: ["AppState"] + dependencies: [ + "AppState", + "Waiter" + ] ) ] ) diff --git a/Sources/AppState/Application/Application+public.swift b/Sources/AppState/Application/Application+public.swift index e519d58..b2735fe 100644 --- a/Sources/AppState/Application/Application+public.swift +++ b/Sources/AppState/Application/Application+public.swift @@ -163,10 +163,20 @@ public extension Application { column: column ) - shared.cache.set( - value: dependency, - forKey: dependency.scope.key - ) + let setValue = { + shared.cache.set( + value: dependency, + forKey: dependency.scope.key + ) + } + + #if (!os(Linux) && !os(Windows)) + Task { + setValue() + } + #else + setValue() + #endif } } @@ -199,7 +209,7 @@ public extension Application { ) var promotions = Application.state(\.dependencyPromotions) - promotions.value.append(promotionOverride) + promotions.value[String(describing: keyPath)] = promotionOverride return Application.self } diff --git a/Sources/AppState/Application/Types/Dependency/Application+Dependency.swift b/Sources/AppState/Application/Types/Dependency/Application+Dependency.swift index 27efce6..785b48c 100644 --- a/Sources/AppState/Application/Types/Dependency/Application+Dependency.swift +++ b/Sources/AppState/Application/Types/Dependency/Application+Dependency.swift @@ -1,6 +1,6 @@ extension Application { - var dependencyPromotions: State<[DependencyOverride]> { - state(initial: []) + var dependencyPromotions: State<[String: DependencyOverride]> { + state(initial: [:]) } /// `Dependency` struct encapsulates dependencies used throughout the app. diff --git a/Tests/AppStateTests/AppDependencyTests.swift b/Tests/AppStateTests/AppDependencyTests.swift index a065ec1..61c3821 100644 --- a/Tests/AppStateTests/AppDependencyTests.swift +++ b/Tests/AppStateTests/AppDependencyTests.swift @@ -1,3 +1,4 @@ +import Waiter import XCTest @testable import AppState @@ -58,7 +59,7 @@ final class AppDependencyTests: XCTestCase { composableService.networking.fetch() } - func testDependency() { + func testDependency() async throws { Application.promote(\.networking, with: NetworkService()) let networkingOverride = Application.override(\.networking, with: MockNetworking()) @@ -75,6 +76,12 @@ final class AppDependencyTests: XCTestCase { networkingOverride.cancel() + try await Waiter.wait( + on: Application.shared, + for: \.networking, + expecting: { $0.value is NetworkService } + ) + let networkingService = Application.dependency(\.networking) XCTAssertNotNil(networkingService as? NetworkService)