Testing parallel effects #3366
-
|
Hey guys! In my feature I have two actions that run side effects. And I'm trying to test that result of one specific effect does not override result of another. Here is a sample: TestStore does not seem to respect provided delay. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hi @m-makhovyk, it is not necessary to spin up an unstructured task to send actions "in parallel": // ❌ Not recommended
Task {
await store.send(.setValue(value: 10, delay: 1))
}
Task {
await store.send(.setValue(value: 20, delay: 0))
}The This means you can typically just do this: await store.send(.setValue(value: 10, delay: 1))
await store.send(.setValue(value: 20, delay: 0))
await store.receive(\.valueSet, 20, timeout: 5)
await store.receive(\.valueSet, 10, timeout: 5)However, another problem with your code is that you have hardcoded the immediate clock in your reducer: try await Task.sleep(for: .seconds(delay), clock: .immediate)An immediate clock does not actually suspend for any time, and so the await store.send(.setValue(value: 10, delay: 1))
await store.send(.setValue(value: 20, delay: 0))
await store.receive(\.valueSet, 10, timeout: 5)
await store.receive(\.valueSet, 20, timeout: 5)…even though that seems counterintuitive. A await store.send(.setValue(value: 10, delay: 1))
await store.send(.setValue(value: 20, delay: 0))
await clock.advanced(by: .seconds(1))
await store.receive(\.valueSet, 20)
await store.receive(\.valueSet, 10)Also, we recommend against providing a |
Beta Was this translation helpful? Give feedback.
-
|
Hey @mbrandonw, thanks a lot for your response! It works 👍 For some reason I thought using |
Beta Was this translation helpful? Give feedback.
Hi @m-makhovyk, it is not necessary to spin up an unstructured task to send actions "in parallel":
The
awaitinTestStore's send doesn't mean "await until action and effects complete". It just means "await until action is processed and effects start."This means you can typically just do this:
However, another problem with your code is that you have hardcoded …