Skip to content

Commit 03ad0cc

Browse files
authored
Bucket effect cancel IDs. (#3374)
* Bucket effect cancel IDs. * wip * wip * remove cache reset * wip * wip * wip
1 parent 912192b commit 03ad0cc

File tree

9 files changed

+110
-38
lines changed

9 files changed

+110
-38
lines changed

.github/package.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ComposableArchitecture.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ let package = Package(
2424
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.5.4"),
2525
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.1.0"),
2626
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.2"),
27-
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.3.5"),
27+
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.4.0"),
2828
.package(url: "https://github.com/pointfreeco/swift-identified-collections", from: "1.1.0"),
2929
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.2.0"),
3030
.package(url: "https://github.com/pointfreeco/swift-navigation", from: "2.1.0"),
3131
.package(url: "https://github.com/pointfreeco/swift-perception", from: "1.3.4"),
32-
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),
32+
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.3.0"),
3333
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"),
3434
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0-prerelease"),
3535
],

[email protected]

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ let package = Package(
2424
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.5.4"),
2525
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.1.0"),
2626
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.2"),
27-
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.3.5"),
27+
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.4.0"),
2828
.package(url: "https://github.com/pointfreeco/swift-identified-collections", from: "1.1.0"),
2929
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.2.0"),
3030
.package(url: "https://github.com/pointfreeco/swift-navigation", from: "2.1.0"),
3131
.package(url: "https://github.com/pointfreeco/swift-perception", from: "1.3.4"),
32-
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),
32+
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.3.0"),
3333
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"),
3434
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0-prerelease"),
3535
],

Sources/ComposableArchitecture/Effects/Cancellation.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,18 @@ extension Task<Never, Never> {
244244
let discriminator: ObjectIdentifier
245245
let id: AnyHashable
246246
let navigationIDPath: NavigationIDPath
247+
let testIdentifier: TestContext.Testing.Test.ID?
247248

248249
init(id: some Hashable, navigationIDPath: NavigationIDPath) {
249250
self.discriminator = ObjectIdentifier(type(of: id))
250251
self.id = id
251252
self.navigationIDPath = navigationIDPath
253+
switch TestContext.current {
254+
case let .swiftTesting(.some(testing)):
255+
self.testIdentifier = testing.test.id
256+
default:
257+
self.testIdentifier = nil
258+
}
252259
}
253260
}
254261

Sources/ComposableArchitecture/TestStore.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,6 @@ public final class TestStore<State, Action> {
535535
where State: Equatable, R.State == State, R.Action == Action {
536536
let sharedChangeTracker = SharedChangeTracker()
537537
let reducer = Dependencies.withDependencies {
538-
if TestContext.current == .swiftTesting {
539-
$0.resetCache()
540-
}
541538
prepareDependencies(&$0)
542539
$0.sharedChangeTrackers.insert(sharedChangeTracker)
543540
} operation: {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#if canImport(Testing)
2+
import ComposableArchitecture
3+
import Testing
4+
5+
@Suite
6+
struct EffectCancellationIsolationTests {
7+
@Test
8+
func testIsolation1() async {
9+
let store = await TestStore(initialState: Feature.State()) {
10+
Feature()
11+
}
12+
await store.send(.start)
13+
await store.receive(\.response) {
14+
$0.value = 42
15+
}
16+
await store.send(.stop)
17+
}
18+
19+
@Test
20+
func testIsolation2() async {
21+
let store = await TestStore(initialState: Feature.State()) {
22+
Feature()
23+
}
24+
await store.send(.start)
25+
await store.receive(\.response) {
26+
$0.value = 42
27+
}
28+
await store.send(.stop)
29+
}
30+
}
31+
32+
@Reducer
33+
private struct Feature {
34+
struct State: Equatable {
35+
var value = 0
36+
}
37+
enum Action {
38+
case response(Int)
39+
case start
40+
case stop
41+
}
42+
enum CancelID { case longLiving }
43+
var body: some ReducerOf<Self> {
44+
Reduce { state, action in
45+
switch action {
46+
case .response(let value):
47+
state.value = value
48+
return .none
49+
case .start:
50+
return .run { send in
51+
await send(.response(42))
52+
try await Task.never()
53+
}
54+
.cancellable(id: CancelID.longLiving, cancelInFlight: true)
55+
case .stop:
56+
return .cancel(id: CancelID.longLiving)
57+
}
58+
}
59+
}
60+
}
61+
62+
#endif

Tests/ComposableArchitectureTests/EffectCancellationTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ final class EffectCancellationTests: BaseTCATestCase {
1111
self.cancellables.removeAll()
1212
}
1313

14+
override func invokeTest() {
15+
withMainSerialExecutor {
16+
super.invokeTest()
17+
}
18+
}
19+
1420
func testCancellation() async {
1521
let values = LockIsolated<[Int]>([])
1622

0 commit comments

Comments
 (0)