-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #13 Fix threading issue in local inject
- Loading branch information
Showing
14 changed files
with
294 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Sources/WhoopDIKit/Container/ThreadSafeDependencyGraph.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Foundation | ||
|
||
final class ThreadSafeDependencyGraph: Sendable { | ||
private let lock = NSRecursiveLock() | ||
nonisolated(unsafe) private let serviceDict: ServiceDictionary<DependencyDefinition> = .init() | ||
private let options: WhoopDIOptionProvider | ||
|
||
init(options: WhoopDIOptionProvider) { | ||
self.options = options | ||
} | ||
|
||
func aquireDependencyGraph<T>(block: (ServiceDictionary<DependencyDefinition>) -> T) -> T { | ||
let threadSafe = options.isOptionEnabled(.threadSafeLocalInject) | ||
if threadSafe { | ||
lock.lock() | ||
} | ||
let result = block(serviceDict) | ||
if threadSafe { | ||
lock.unlock() | ||
} | ||
return result | ||
} | ||
|
||
func resetDependencyGraph() { | ||
let threadSafe = options.isOptionEnabled(.threadSafeLocalInject) | ||
if threadSafe { | ||
lock.lock() | ||
} | ||
serviceDict.removeAll() | ||
if threadSafe { | ||
lock.unlock() | ||
} | ||
} | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
...s/WhoopDIKit/Module/DependencyError.swift → Sources/WhoopDIKit/DependencyError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
struct DefaultOptionProvider: WhoopDIOptionProvider { | ||
func isOptionEnabled(_ option: WhoopDIOption) -> Bool { | ||
false | ||
} | ||
} | ||
|
||
public func defaultWhoopDIOptions() -> WhoopDIOptionProvider { | ||
DefaultOptionProvider() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/// Options for WhoopDI. These are typically experimental features which may be enabled or disabled. | ||
public enum WhoopDIOption: Sendable { | ||
case threadSafeLocalInject | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/// Implement this protocol and pass it into WhoopDI via `WhoopDI.setOptions` to enable and disable various options for WhoopDI. | ||
public protocol WhoopDIOptionProvider: Sendable { | ||
func isOptionEnabled(_ option: WhoopDIOption) -> Bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,94 @@ | ||
import XCTest | ||
import Testing | ||
@testable import WhoopDIKit | ||
|
||
class ContainerTests: XCTestCase { | ||
private let container = Container() | ||
// This is unchecked Sendable so we can run our local inject concurrency test | ||
@Suite(.serialized) | ||
class ContainerTests: @unchecked Sendable { | ||
private let container: Container | ||
|
||
init() { | ||
let options = MockOptionProvider(options: [.threadSafeLocalInject: true]) | ||
container = Container(options: options) | ||
} | ||
|
||
func test_inject() { | ||
@Test | ||
func inject() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: Dependency = container.inject("C_Factory", "param") | ||
XCTAssertTrue(dependency is DependencyC) | ||
#expect(dependency is DependencyC) | ||
} | ||
|
||
func test_inject_generic_integer() { | ||
@Test | ||
func inject_generic_integer() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: GenericDependency<Int> = container.inject() | ||
XCTAssertEqual(42, dependency.value) | ||
#expect(42 == dependency.value) | ||
} | ||
|
||
func test_inject_generic_string() { | ||
@Test | ||
func inject_generic_string() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: GenericDependency<String> = container.inject() | ||
XCTAssertEqual("string", dependency.value) | ||
#expect("string" == dependency.value) | ||
} | ||
|
||
func test_inject_localDefinition() { | ||
@Test | ||
func inject_localDefinition() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: Dependency = container.inject("C_Factory") { module in | ||
// Typically you'd override or provide a transient dependency. I'm using the top level dependency here | ||
// for the sake of simplicity. | ||
module.factory(name: "C_Factory") { DependencyA() as Dependency } | ||
} | ||
XCTAssertTrue(dependency is DependencyA) | ||
#expect(dependency is DependencyA) | ||
} | ||
|
||
@Test(.bug("https://github.com/WhoopInc/WhoopDI/issues/13")) | ||
func inject_localDefinition_concurrency() async { | ||
// You can run this test repeatedly to verify we don't have a concurrency issue when | ||
// performing a local inject. 1000 times should do the trick. | ||
container.registerModules(modules: [GoodTestModule()]) | ||
|
||
async let resultA = Task { | ||
let _: Dependency = container.inject("C_Factory") { module in | ||
module.factory(name: "C_Factory") { DependencyA() as Dependency } | ||
} | ||
}.result | ||
|
||
async let resultB = Task { | ||
let _: DependencyA = container.inject() | ||
}.result | ||
|
||
let _ = await [resultA, resultB] | ||
} | ||
|
||
func test_inject_localDefinition_noOverride() { | ||
@Test | ||
func inject_localDefinition_noOverride() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: Dependency = container.inject("C_Factory", params: "params") { _ in } | ||
XCTAssertTrue(dependency is DependencyC) | ||
#expect(dependency is DependencyC) | ||
} | ||
|
||
func test_inject_localDefinition_withParams() { | ||
@Test | ||
func inject_localDefinition_withParams() { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
let dependency: Dependency = container.inject("C_Factory", params: "params") { module in | ||
module.factoryWithParams(name: "C_Factory") { params in DependencyB(params) as Dependency } | ||
} | ||
XCTAssertTrue(dependency is DependencyB) | ||
#expect(dependency is DependencyB) | ||
} | ||
|
||
func test_injectableWithDependency() throws { | ||
@Test | ||
func injectableWithDependency() throws { | ||
container.registerModules(modules: [FakeTestModuleForInjecting()]) | ||
let testInjecting: InjectableWithDependency = container.inject() | ||
XCTAssertEqual(testInjecting, InjectableWithDependency(dependency: DependencyA())) | ||
#expect(testInjecting == InjectableWithDependency(dependency: DependencyA())) | ||
} | ||
|
||
func test_injectableWithNamedDependency() throws { | ||
@Test | ||
func injectableWithNamedDependency() throws { | ||
container.registerModules(modules: [FakeTestModuleForInjecting()]) | ||
let testInjecting: InjectableWithNamedDependency = container.inject() | ||
XCTAssertEqual(testInjecting, InjectableWithNamedDependency(name: 1)) | ||
#expect(testInjecting == InjectableWithNamedDependency(name: 1)) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
Tests/WhoopDIKitTests/Container/ThreadSafeDependencyGraphTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Testing | ||
@testable import WhoopDIKit | ||
|
||
struct ThreadSafeDependencyGraphTests { | ||
private let key = "key" | ||
|
||
@Test(arguments: [false, true]) | ||
func aquireDependencyGraph_notThreadSafe(threadsafe: Bool) { | ||
let options = MockOptionProvider(options: [.threadSafeLocalInject: threadsafe]) | ||
let graph = ThreadSafeDependencyGraph(options: options) | ||
|
||
graph.aquireDependencyGraph { serviceDict in | ||
serviceDict[DependencyA.self] = FactoryDefinition(name: nil) { _ in DependencyA() } | ||
} | ||
graph.aquireDependencyGraph { serviceDict in | ||
let dependency = serviceDict[DependencyA.self] | ||
#expect(dependency != nil) | ||
} | ||
|
||
graph.resetDependencyGraph() | ||
|
||
graph.aquireDependencyGraph { serviceDict in | ||
let dependency = serviceDict[DependencyA.self] | ||
#expect(dependency == nil) | ||
} | ||
} | ||
|
||
@Test | ||
func aquireDependencyGraph_recursive() { | ||
let options = MockOptionProvider(options: [.threadSafeLocalInject: true]) | ||
let graph = ThreadSafeDependencyGraph(options: options) | ||
|
||
graph.aquireDependencyGraph { outer in | ||
graph.aquireDependencyGraph { serviceDict in | ||
serviceDict[DependencyA.self] = FactoryDefinition(name: nil) { _ in DependencyA() } | ||
} | ||
let dependency = outer[DependencyA.self] | ||
#expect(dependency != nil) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.