Skip to content

Commit d4834b5

Browse files
authored
Fix flakey test (#1364)
* Fix flakey test * Try something more general
1 parent 544228f commit d4834b5

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

Sources/ComposableArchitecture/TestStore.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,12 @@
267267
return
268268
effects
269269
.handleEvents(
270-
receiveSubscription: { [weak self] _ in
270+
receiveSubscription: { [effectDidSubscribe = self.effectDidSubscribe, weak self] _ in
271271
self?.inFlightEffects.insert(effect)
272-
self?.effectDidSubscribe.continuation.yield()
272+
Task {
273+
await Task.megaYield()
274+
effectDidSubscribe.continuation.yield()
275+
}
273276
},
274277
receiveCompletion: { [weak self] _ in self?.inFlightEffects.remove(effect) },
275278
receiveCancel: { [weak self] in self?.inFlightEffects.remove(effect) }

Tests/ComposableArchitectureTests/ComposableArchitectureTests.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ final class ComposableArchitectureTests: XCTestCase {
123123
case response(Int)
124124
}
125125

126-
struct Environment {
127-
let fetch: (Int) async -> Int
128-
}
129-
130-
let reducer = Reducer<Int, Action, Environment> { state, action, environment in
126+
let reducer = Reducer<Int, Action, Void> { state, action, _ in
131127
enum CancelID {}
132128

133129
switch action {
@@ -138,7 +134,7 @@ final class ComposableArchitectureTests: XCTestCase {
138134
state += 1
139135
return .task { [state] in
140136
try await mainQueue.sleep(for: .seconds(1))
141-
return .response(await environment.fetch(state))
137+
return .response(state * state)
142138
}
143139
.cancellable(id: CancelID.self)
144140

@@ -151,9 +147,7 @@ final class ComposableArchitectureTests: XCTestCase {
151147
let store = TestStore(
152148
initialState: 0,
153149
reducer: reducer,
154-
environment: Environment(
155-
fetch: { value in value * value }
156-
)
150+
environment: ()
157151
)
158152

159153
await store.send(.incr) { $0 = 1 }

0 commit comments

Comments
 (0)