-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix caching of dependencies during re-entrancy (#249)
* Fix cache. * wip; * wip * simplify test * simplify * fix * Clear cache at end of test so to not bleed over into other tests. * fix test * wip
- Loading branch information
Showing
2 changed files
with
118 additions
and
70 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
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,44 @@ | ||
@_spi(Internals) import Dependencies | ||
import XCTest | ||
|
||
final class CachedValueTests: XCTestCase { | ||
override func tearDown() { | ||
super.tearDown() | ||
CacheLocals.$skipFailure.withValue(true) { | ||
DependencyValues._current.cachedValues.cached = [:] | ||
} | ||
} | ||
|
||
func testCacheWithReEntrantAccess() { | ||
@Dependency(OuterDependencyTests.self) var outerDependency | ||
_ = outerDependency | ||
} | ||
} | ||
|
||
private struct OuterDependencyTests: TestDependencyKey { | ||
static var testValue: OuterDependencyTests { | ||
@Dependency(InnerDependency.self) var innerDependency | ||
_ = innerDependency | ||
return Self() | ||
} | ||
} | ||
private struct InnerDependency: TestDependencyKey { | ||
let perform: @Sendable () -> Void | ||
static var testValue: InnerDependency { | ||
final class Ref: Sendable { | ||
deinit { | ||
guard !CacheLocals.skipFailure | ||
else { return } | ||
XCTFail("This should not deinit") | ||
} | ||
} | ||
let ref = Ref() | ||
return Self { | ||
_ = ref | ||
} | ||
} | ||
} | ||
|
||
private enum CacheLocals { | ||
@TaskLocal static var skipFailure = false | ||
} |