|
| 1 | +import XCTest |
| 2 | +@testable import FactoryKit |
| 3 | + |
| 4 | +final class FactoryLazyInjectedLockingTests: XCTestCase, @unchecked Sendable { |
| 5 | + |
| 6 | + override func setUp() { |
| 7 | + super.setUp() |
| 8 | + LazyLockingContainer.shared.reset() |
| 9 | + Scope.singleton.reset() |
| 10 | + } |
| 11 | + |
| 12 | + // Verifies the core guarantee: even when many threads race on first access, |
| 13 | + // the factory closure is invoked exactly once. |
| 14 | + func testConcurrentFirstAccessInvokesFactoryExactlyOnce() { |
| 15 | + let counter = InvocationCounter() |
| 16 | + LazyLockingContainer.shared.service.register { |
| 17 | + counter.increment() |
| 18 | + return LazyLockingService() |
| 19 | + } |
| 20 | + |
| 21 | + let holder = LazyInjectedLockingHolder() |
| 22 | + DispatchQueue.concurrentPerform(iterations: 50) { _ in |
| 23 | + _ = holder.service |
| 24 | + } |
| 25 | + |
| 26 | + XCTAssertEqual(counter.value, 1, "@LazyInjected factory should be invoked exactly once under concurrent first access, got \(counter.value)") |
| 27 | + } |
| 28 | + |
| 29 | + // Runs the exactly-once check multiple times to catch probabilistic races. |
| 30 | + func testConcurrentFirstAccessInvokesFactoryExactlyOnceRepeated() { |
| 31 | + for _ in 0..<20 { |
| 32 | + LazyLockingContainer.shared.reset() |
| 33 | + let counter = InvocationCounter() |
| 34 | + LazyLockingContainer.shared.service.register { |
| 35 | + counter.increment() |
| 36 | + return LazyLockingService() |
| 37 | + } |
| 38 | + |
| 39 | + let holder = LazyInjectedLockingHolder() |
| 40 | + DispatchQueue.concurrentPerform(iterations: 50) { _ in |
| 41 | + _ = holder.service |
| 42 | + } |
| 43 | + |
| 44 | + XCTAssertEqual(counter.value, 1, "Factory invocation count should be 1, got \(counter.value)") |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Same guarantee for @WeakLazyInjected. |
| 49 | + func testWeakLazyInjectedConcurrentFirstAccessInvokesFactoryExactlyOnce() { |
| 50 | + let counter = InvocationCounter() |
| 51 | + LazyLockingContainer.shared.service.register { |
| 52 | + counter.increment() |
| 53 | + return LazyLockingService() |
| 54 | + } |
| 55 | + |
| 56 | + let holder = WeakLazyInjectedLockingHolder() |
| 57 | + // Keep strong references so the weak dependency isn't released mid-test. |
| 58 | + let retained = RetainBag<LazyLockingService>() |
| 59 | + DispatchQueue.concurrentPerform(iterations: 50) { _ in |
| 60 | + if let svc = holder.service { |
| 61 | + retained.append(svc) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + XCTAssertEqual(counter.value, 1, "@WeakLazyInjected factory should be invoked exactly once under concurrent first access, got \(counter.value)") |
| 66 | + retained.discard() |
| 67 | + } |
| 68 | + |
| 69 | + // resolvedOrNil() should return nil before first access and non-nil after. |
| 70 | + func testResolvedOrNilSemanticsBeforeAndAfterAccess() { |
| 71 | + let holder = LazyInjectedLockingHolder() |
| 72 | + XCTAssertNil(holder.resolvedOrNil, "resolvedOrNil() should be nil before first access") |
| 73 | + _ = holder.service |
| 74 | + XCTAssertNotNil(holder.resolvedOrNil, "resolvedOrNil() should be non-nil after first access") |
| 75 | + } |
| 76 | + |
| 77 | + // resolve(reset:) should re-invoke the factory and result in a non-nil dependency. |
| 78 | + func testExplicitResolveUpdatesValue() { |
| 79 | + let holder = LazyInjectedLockingHolder() |
| 80 | + let first = holder.service |
| 81 | + holder.$service.resolve(reset: .none) |
| 82 | + let second = holder.resolvedOrNil |
| 83 | + XCTAssertNotNil(second) |
| 84 | + XCTAssertFalse(first === second, "resolve() should produce a new instance with a unique-scoped factory") |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +// MARK: - Infrastructure |
| 90 | + |
| 91 | +private final class InvocationCounter: @unchecked Sendable { |
| 92 | + private let lock = NSLock() |
| 93 | + private var count = 0 |
| 94 | + func increment() { lock.withLock { count += 1 } } |
| 95 | + var value: Int { lock.withLock { count } } |
| 96 | +} |
| 97 | + |
| 98 | +private final class RetainBag<T: AnyObject>: @unchecked Sendable { |
| 99 | + private let lock = NSLock() |
| 100 | + private var storage: [T] = [] |
| 101 | + func append(_ element: T) { lock.withLock { storage.append(element) } } |
| 102 | + func discard() { lock.withLock { storage.removeAll() } } |
| 103 | +} |
| 104 | + |
| 105 | +final class LazyLockingService: @unchecked Sendable { |
| 106 | + init() {} |
| 107 | +} |
| 108 | + |
| 109 | +final class LazyLockingContainer: SharedContainer { |
| 110 | + static let shared = LazyLockingContainer() |
| 111 | + var service: Factory<LazyLockingService> { self { LazyLockingService() } } |
| 112 | + let manager = ContainerManager() |
| 113 | +} |
| 114 | + |
| 115 | +final class LazyInjectedLockingHolder: @unchecked Sendable { |
| 116 | + @LazyInjected(\LazyLockingContainer.service) var service: LazyLockingService |
| 117 | + var resolvedOrNil: LazyLockingService? { $service.resolvedOrNil() } |
| 118 | +} |
| 119 | + |
| 120 | +final class WeakLazyInjectedLockingHolder: @unchecked Sendable { |
| 121 | + @WeakLazyInjected(\LazyLockingContainer.service) var service: LazyLockingService? |
| 122 | +} |
0 commit comments