Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package com.badoo.mvicore.extension

class SameThreadVerifier {
class SameThreadVerifier(private val clazz: Class<*>) {

companion object {
var isEnabled : Boolean = true
}

private val originalThread = Thread.currentThread().id
private val originalThreadId: Long
private val originalThreadName: String

init {
val currentThread = Thread.currentThread()
originalThreadId = currentThread.id
originalThreadName = currentThread.name
}

fun verify() {
if (isEnabled && (Thread.currentThread().id != originalThread)) {
throw AssertionError("Not on same thread as previous verification")
val currentThread = Thread.currentThread()
if (isEnabled && (currentThread.id != originalThreadId)) {
throw AssertionError(
"${clazz.name} was interacted with on the wrong thread. " +
"Expected: '$originalThreadName', Actual: '${currentThread.name}'"
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ open class BaseAsyncFeature<Wish : Any, in Action : Any, in Effect : Any, State
private val schedulers: AsyncFeatureSchedulers
) : AsyncFeature<Wish, State, News> {

private val threadVerifier by lazy { SameThreadVerifier() }
private val threadVerifier by lazy { SameThreadVerifier(javaClass) }
private val actionSubject = PublishSubject.create<Action>().toSerialized()
// store last state to make best effort to return it in getState()
private val lastState = AtomicReference<State>(initialState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ open class BaseFeature<Wish : Any, in Action : Any, in Effect : Any, State : Any
private val featureScheduler: FeatureScheduler? = null
) : Feature<Wish, State, News> {

private val threadVerifier = if (featureScheduler == null) SameThreadVerifier() else null
private val threadVerifier = if (featureScheduler == null) SameThreadVerifier(javaClass) else null
private val actionSubject = PublishSubject.create<Action>().toSerialized()
private val stateSubject = BehaviorSubject.createDefault(initialState)
private val newsSubject = PublishSubject.create<News>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.badoo.mvicore.extension

import org.junit.Test
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.concurrent.thread
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

internal class SameThreadVerifierTest {
@Test
fun `GIVEN same thread WHEN verify THEN expect no exceptions`() {
val threadVerifier = SameThreadVerifier(String::class.java)
threadVerifier.verify()
}

@Test
fun `GIVEN different thread WHEN verify THEN expect exception`() {
val threadVerifier = SameThreadVerifier(String::class.java)
val testWorkerThreadName = Thread.currentThread().name

var assertionError: AssertionError? = null
val latch = CountDownLatch(1)
thread(name = "wrong-thread") {
try {
threadVerifier.verify()
} catch (e: AssertionError) {
assertionError = e
}
latch.countDown()
}
latch.await(1, TimeUnit.SECONDS)

assertNotNull(assertionError)
assertEquals(
"java.lang.String was interacted with on the wrong thread. Expected: '$testWorkerThreadName', Actual: 'wrong-thread'",
assertionError!!.message
)
}
}