Skip to content

Commit 75183d2

Browse files
stephencelismluisbrown
authored andcommitted
Fix flakey test (#1364)
* Fix flakey test * Try something more general (cherry picked from commit d4834b56e92f347ac6c8b9908d415fd51c3d18f4) # Conflicts: # Sources/ComposableArchitecture/TestStore.swift
1 parent a8fb64c commit 75183d2

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
@@ -272,9 +272,12 @@
272272
effects
273273
.producer
274274
.on(
275-
starting: { [weak self] in
275+
starting: { [effectDidSubscribe = self.effectDidSubscribe, weak self] in
276276
self?.inFlightEffects.insert(effect)
277-
self?.effectDidSubscribe.continuation.yield()
277+
Task {
278+
await Task.megaYield()
279+
effectDidSubscribe.continuation.yield()
280+
}
278281
},
279282
completed: { [weak self] in self?.inFlightEffects.remove(effect) },
280283
disposed: { [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
@@ -114,11 +114,7 @@ final class ComposableArchitectureTests: XCTestCase {
114114
case response(Int)
115115
}
116116

117-
struct Environment {
118-
let fetch: (Int) async -> Int
119-
}
120-
121-
let reducer = Reducer<Int, Action, Environment> { state, action, environment in
117+
let reducer = Reducer<Int, Action, Void> { state, action, _ in
122118
enum CancelID {}
123119

124120
switch action {
@@ -129,7 +125,7 @@ final class ComposableArchitectureTests: XCTestCase {
129125
state += 1
130126
return .task { [state] in
131127
try await mainQueue.sleep(for: .seconds(1))
132-
return .response(await environment.fetch(state))
128+
return .response(state * state)
133129
}
134130
.cancellable(id: CancelID.self)
135131

@@ -142,9 +138,7 @@ final class ComposableArchitectureTests: XCTestCase {
142138
let store = TestStore(
143139
initialState: 0,
144140
reducer: reducer,
145-
environment: Environment(
146-
fetch: { value in value * value }
147-
)
141+
environment: ()
148142
)
149143

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

0 commit comments

Comments
 (0)