-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
15 changed files
with
350 additions
and
119 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: @unchecked Sendable { | ||
private let lock = NSRecursiveLock() | ||
private let serviceDict: ServiceDictionary<DependencyDefinition> = .init() | ||
private let options: WhoopDIOptionProvider | ||
|
||
init(options: WhoopDIOptionProvider) { | ||
self.options = options | ||
} | ||
|
||
func acquireDependencyGraph<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,95 @@ | ||
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 | ||
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 { | ||
container.registerModules(modules: [GoodTestModule()]) | ||
// Run many times to try and capture race condition | ||
for _ in 0..<500 { | ||
let taskA = Task.detached { | ||
let _: Dependency = self.container.inject("C_Factory") { module in | ||
module.factory(name: "C_Factory") { DependencyA() as Dependency } | ||
} | ||
} | ||
|
||
let taskB = Task.detached { | ||
let _: DependencyA = self.container.inject() | ||
} | ||
|
||
for task in [taskA, taskB] { | ||
let _ = await task.result | ||
} | ||
} | ||
} | ||
|
||
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)) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
import Testing | ||
@testable import WhoopDIKit | ||
|
||
struct ThreadSafeDependencyGraphTests { | ||
@Test(arguments: [false, true]) | ||
func acquireDependencyGraph_notThreadSafe(threadsafe: Bool) { | ||
let options = MockOptionProvider(options: [.threadSafeLocalInject: threadsafe]) | ||
let graph = ThreadSafeDependencyGraph(options: options) | ||
|
||
graph.acquireDependencyGraph { serviceDict in | ||
serviceDict[DependencyA.self] = FactoryDefinition(name: nil) { _ in DependencyA() } | ||
} | ||
graph.acquireDependencyGraph { serviceDict in | ||
let dependency = serviceDict[DependencyA.self] | ||
#expect(dependency != nil) | ||
} | ||
|
||
graph.resetDependencyGraph() | ||
|
||
graph.acquireDependencyGraph { serviceDict in | ||
let dependency = serviceDict[DependencyA.self] | ||
#expect(dependency == nil) | ||
} | ||
} | ||
|
||
@Test | ||
func acquireDependencyGraph_recursive() { | ||
let options = MockOptionProvider(options: [.threadSafeLocalInject: true]) | ||
let graph = ThreadSafeDependencyGraph(options: options) | ||
|
||
graph.acquireDependencyGraph { outer in | ||
graph.acquireDependencyGraph { 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.