Skip to content

Commit

Permalink
Add a test to document how 'task tree' cancellation happens. (#3499)
Browse files Browse the repository at this point in the history
* Add a test to document how 'task tree' cancellation  happens.

* fix
  • Loading branch information
mbrandonw authored Nov 19, 2024
1 parent 69247ba commit 333bc82
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Tests/ComposableArchitectureTests/StoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@_spi(Internals) import ComposableArchitecture
import XCTest

#if canImport(Testing)
import Testing
#endif

final class StoreTests: BaseTCATestCase {
var cancellables: Set<AnyCancellable> = []

Expand Down Expand Up @@ -1172,6 +1176,53 @@ final class StoreTests: BaseTCATestCase {
}
}

#if canImport(Testing)
@Suite
struct ModernStoreTests {
@Reducer
fileprivate struct TaskTreeFeature {
let clock: TestClock<Duration>
@ObservableState
struct State { var count = 0 }
enum Action { case tap, response1, response2 }
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .tap:
return Effect.run { send in
await send(.response1)
}
case .response1:
state.count = 42
return Effect.run { send in
try await clock.sleep(for: .seconds(1))
await send(.response2)
}
case .response2:
state.count = 1729
return .none
}
}
}
}

@MainActor
@Test
func cancellation() async throws {
let clock = TestClock()
let store = Store(initialState: TaskTreeFeature.State()) { TaskTreeFeature(clock: clock) }
let task = store.send(.tap)
try await Task.sleep(for: .seconds(0.1))
#expect(store.count == 42)
task.cancel()
await clock.run()
withKnownIssue("Cancelling the root effect should not cancel the child effects.") {
#expect(store.count == 1729)
}
}
}
#endif

private struct Count: TestDependencyKey {
var value: Int
static let liveValue = Count(value: 0)
Expand Down

0 comments on commit 333bc82

Please sign in to comment.