Skip to content

Commit

Permalink
Bucket dependency cache by Swift Testing test
Browse files Browse the repository at this point in the history
Currently, the global cache can bleed between test cases. This updates
the underlying cache key to take the current test into account.
  • Loading branch information
stephencelis committed Sep 4, 2024
1 parent 3ef38bb commit 9b783ec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
28 changes: 14 additions & 14 deletions Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "b78e1fa69770050e204024326b4d77e8451007e7f7ecc1b640d5889ffbe0b3a7",
"originHash" : "080e6d4495c721b4761386bc163d806e1763708183d59c6d3bc26200a063b001",
"pins" : [
{
"identity" : "combine-schedulers",
Expand All @@ -15,8 +15,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-clocks",
"state" : {
"revision" : "3581e280bf0d90c3fb9236fb23e75a5d8c46b533",
"version" : "1.0.4"
"revision" : "b9b24b69e2adda099a1fa381cda1eeec272d5b53",
"version" : "1.0.5"
}
},
{
Expand All @@ -33,14 +33,14 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin",
"state" : {
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247",
"version" : "1.3.0"
"revision" : "2eb22993b3dfd0c0d32729b357c8dabb6cd44680",
"version" : "1.4.2"
}
},
{
"identity" : "swift-docc-symbolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-symbolkit",
"location" : "https://github.com/swiftlang/swift-docc-symbolkit",
"state" : {
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
Expand All @@ -51,35 +51,35 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-macro-testing",
"state" : {
"revision" : "a35257b7e9ce44e92636447003a8eeefb77b145c",
"version" : "0.5.1"
"revision" : "20c1a8f3b624fb5d1503eadcaa84743050c350f4",
"version" : "0.5.2"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "c097f955b4e724690f0fc8ffb7a6d4b881c9c4e3",
"version" : "1.17.2"
"revision" : "6d932a79e7173b275b96c600c86c603cf84f153c",
"version" : "1.17.4"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-syntax",
"state" : {
"revision" : "4c6cc0a3b9e8f14b3ae2307c5ccae4de6167ac2c",
"version" : "600.0.0-prerelease-2024-06-12"
"revision" : "515f79b522918f83483068d99c68daeb5116342d",
"version" : "600.0.0-prerelease-2024-08-20"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "357ca1e5dd31f613a1d43320870ebc219386a495",
"version" : "1.2.2"
"branch" : "testing-test-context",
"revision" : "4bcf83574adc0e2f932719107eb239c132e98f3f"
}
}
],
Expand Down
5 changes: 4 additions & 1 deletion Package@swift-6.0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ let package = Package(
.package(url: "https://github.com/pointfreeco/combine-schedulers", from: "1.0.2"),
.package(url: "https://github.com/pointfreeco/swift-clocks", from: "1.0.4"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),
.package(
url: "https://github.com/pointfreeco/xctest-dynamic-overlay",
branch: "testing-test-context"
),
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0-prerelease"),
],
targets: [
Expand Down
12 changes: 12 additions & 0 deletions Sources/Dependencies/DependencyValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@ public final class CachedValues: @unchecked Sendable {
public struct CacheKey: Hashable, Sendable {
let id: ObjectIdentifier
let context: DependencyContext
let testIdentifier: TestContext.Testing.Test.ID?

init(id: ObjectIdentifier, context: DependencyContext) {
self.id = id
self.context = context
switch TestContext.current {
case let .swiftTesting(testing):
self.testIdentifier = testing.test.id
default:
self.testIdentifier = nil
}
}
}

private let lock = NSRecursiveLock()
Expand Down
30 changes: 11 additions & 19 deletions Tests/DependenciesTests/SwiftTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,21 @@
struct SwiftTestingTests {
@Test
func cachePollution1() async {
await withDependencies {
$0 = DependencyValues()
} operation: {
@Dependency(\.cachedDependency) var cachedDependency: CachedDependency
let value = await cachedDependency.increment()
#expect(value == 1)
}
@Dependency(\.cachedDependency) var cachedDependency: CachedDependency
let value = await cachedDependency.increment()
#expect(value == 1)
}

@Test
func cachePollution2() async {
await withDependencies {
$0 = DependencyValues()
} operation: {
@Dependency(\.cachedDependency) var cachedDependency: CachedDependency
let value = await cachedDependency.increment()
// NB: Wasm has different behavior here.
#if os(WASI)
#expect(value == 2)
#else
#expect(value == 1)
#endif
}
@Dependency(\.cachedDependency) var cachedDependency: CachedDependency
let value = await cachedDependency.increment()
// NB: Wasm has different behavior here.
#if os(WASI)
#expect(value == 2)
#else
#expect(value == 1)
#endif
}
}
#endif

0 comments on commit 9b783ec

Please sign in to comment.