Skip to content

Commit 333bc82

Browse files
authored
Add a test to document how 'task tree' cancellation happens. (#3499)
* Add a test to document how 'task tree' cancellation happens. * fix
1 parent 69247ba commit 333bc82

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Tests/ComposableArchitectureTests/StoreTests.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
@_spi(Internals) import ComposableArchitecture
33
import XCTest
44

5+
#if canImport(Testing)
6+
import Testing
7+
#endif
8+
59
final class StoreTests: BaseTCATestCase {
610
var cancellables: Set<AnyCancellable> = []
711

@@ -1172,6 +1176,53 @@ final class StoreTests: BaseTCATestCase {
11721176
}
11731177
}
11741178

1179+
#if canImport(Testing)
1180+
@Suite
1181+
struct ModernStoreTests {
1182+
@Reducer
1183+
fileprivate struct TaskTreeFeature {
1184+
let clock: TestClock<Duration>
1185+
@ObservableState
1186+
struct State { var count = 0 }
1187+
enum Action { case tap, response1, response2 }
1188+
var body: some ReducerOf<Self> {
1189+
Reduce { state, action in
1190+
switch action {
1191+
case .tap:
1192+
return Effect.run { send in
1193+
await send(.response1)
1194+
}
1195+
case .response1:
1196+
state.count = 42
1197+
return Effect.run { send in
1198+
try await clock.sleep(for: .seconds(1))
1199+
await send(.response2)
1200+
}
1201+
case .response2:
1202+
state.count = 1729
1203+
return .none
1204+
}
1205+
}
1206+
}
1207+
}
1208+
1209+
@MainActor
1210+
@Test
1211+
func cancellation() async throws {
1212+
let clock = TestClock()
1213+
let store = Store(initialState: TaskTreeFeature.State()) { TaskTreeFeature(clock: clock) }
1214+
let task = store.send(.tap)
1215+
try await Task.sleep(for: .seconds(0.1))
1216+
#expect(store.count == 42)
1217+
task.cancel()
1218+
await clock.run()
1219+
withKnownIssue("Cancelling the root effect should not cancel the child effects.") {
1220+
#expect(store.count == 1729)
1221+
}
1222+
}
1223+
}
1224+
#endif
1225+
11751226
private struct Count: TestDependencyKey {
11761227
var value: Int
11771228
static let liveValue = Count(value: 0)

0 commit comments

Comments
 (0)