Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test to document how 'task tree' cancellation happens. #3499

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading