Skip to content

Commit 3110d6e

Browse files
committed
update exception messages with info about threading. Update threading tests
1 parent f297b21 commit 3110d6e

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

lib-threadsafe/src/jvmTest/kotlin/org/reduxkotlin/util/CreateThreadSafeStoreSpec.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.reduxkotlin.util
22

33
import kotlinx.coroutines.*
4-
import org.reduxkotlin.*
4+
import org.reduxkotlin.createThreadSafeStore
55
import org.spekframework.spek2.Spek
66
import org.spekframework.spek2.style.specification.describe
77
import kotlin.system.measureTimeMillis
8-
import kotlin.test.*
8+
import kotlin.test.assertEquals
99

10-
object CreateThreadSafeStoreSpec : Spek({
11-
describe("createThreadSafeStore") {
10+
object MultiThreadedSpec : Spek({
11+
describe("createStore") {
1212
it("multithreaded increments massively") {
1313
suspend fun massiveRun(action: suspend () -> Unit) {
1414
val n = 100 // number of coroutines to launch
@@ -26,19 +26,15 @@ object CreateThreadSafeStoreSpec : Spek({
2626
println("Completed ${n * k} actions in $time ms")
2727
}
2828

29-
val counterContext = newFixedThreadPoolContext(5, "CounterContext")
30-
3129
//NOTE: changing this to createStore() breaks the tests
3230
val store = createThreadSafeStore(counterReducer, TestCounterState())
3331
runBlocking {
34-
withContext(counterContext) {
32+
withContext(Dispatchers.Default) {
3533
massiveRun {
3634
store.dispatch(Increment())
3735
}
3836
}
39-
withContext(counterContext) {
40-
assertEquals(100000, store.state.counter)
41-
}
37+
assertEquals(100000, store.state.counter)
4238
}
4339
}
4440
}

lib/src/commonMain/kotlin/org/reduxkotlin/CreateStore.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ fun <State> createStore(
7373
"""|You may not call store.getState() while the reducer is executing.
7474
|The reducer has already received the state as an argument.
7575
|Pass it down from the top reducer instead of reading it from the
76-
|store.""".trimMargin()
76+
|store.
77+
|You may be accessing getState while dispatching from another
78+
|thread. Try createThreadSafeStore().
79+
|https://reduxkotlin.org/introduction/threading""".trimMargin()
7780
}
7881

7982
return currentState
@@ -111,7 +114,10 @@ fun <State> createStore(
111114
|subscribe from a component and invoke store.getState() in the
112115
|callback to access the latest state. See
113116
|https://www.reduxkotlin.org/api/store#subscribelistener-storesubscriber
114-
|for more details.""".trimMargin()
117+
|for more details.
118+
|You may be seeing this due accessing the store from multiplethreads.
119+
|Try createThreadSafeStore()
120+
|https://reduxkotlin.org/introduction/threading""".trimMargin()
115121
}
116122

117123
var isSubscribed = true
@@ -169,10 +175,18 @@ fun <State> createStore(
169175
|actions.""".trimMargin()
170176
}
171177

178+
/*
172179
check(!isDispatching) {
173-
"Reducers may not dispatch actions."
180+
"""You may not dispatch while state is being reduced.
181+
|2 conditions can cause this error:
182+
| 1) Dispatching from a reducer
183+
| 2) Dispatching from multiple threads
184+
|If #2 switch to createThreadSafeStore().
185+
|https://reduxkotlin.org/introduction/threading""".trimMargin()
174186
}
175187
188+
*/
189+
176190
try {
177191
isDispatching = true
178192
currentState = currentReducer(currentState, action)

0 commit comments

Comments
 (0)