Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test 'attemptNoCacheOnlineRaceCondition' for subsequent fix #34

Merged
Merged
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
40 changes: 38 additions & 2 deletions dod/src/test/java/com/revolut/rxdata/dod/DodFunctionalTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.revolut.rxdata.dod

import com.revolut.rxdata.core.extensions.takeUntilLoaded
import io.reactivex.Single
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.RepeatedTest
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
Expand Down Expand Up @@ -49,6 +50,13 @@ class DodFunctionalTest {
fromStorage = { null },
toStorage = toStorage
)
private val noCacheOnlineDod = DataObservableDelegate<Unit, String>(
fromNetwork = { Single.fromCallable { "test" } },
fromMemory = { null },
toMemory = toMemory,
fromStorage = { null },
toStorage = toStorage
)

init {
whenever(fromNetwork.invoke(Unit)).thenReturn(Single.fromCallable {
Expand Down Expand Up @@ -131,7 +139,35 @@ class DodFunctionalTest {
}

val finishedWithLoadingTrueCount = isLastEmitLoadingMap.count { it.value }
assert( finishedWithLoadingTrueCount == 0) { "Finished with loading true count = $finishedWithLoadingTrueCount" }
assert(finishedWithLoadingTrueCount == 0) { "Finished with loading true count = $finishedWithLoadingTrueCount" }
}

@Disabled("waiting for fix")
@RepeatedTest(1000)
fun attemptNoCacheOnlineRaceCondition() {
val count = 3
val maxThreadDelay = 3
val maxDelay: Long = (count + 2).toLong() * maxThreadDelay + 30
val isLastEmitLoadingMap = ConcurrentHashMap<Int, Boolean>(count)

(1..count).map { num ->
val delay = Random.nextInt(0, maxThreadDelay).toLong()
sleep(delay)
Thread {
noCacheOnlineDod.observe(Unit, forceReload = true)
.doOnNext {
println("Stream $num: data emitted $it")
isLastEmitLoadingMap[num] = it.loading
}
.test()
.awaitTerminalEvent(maxDelay, MILLISECONDS)
}.apply { start() }
}.forEach {
it.join(maxDelay)
}

val finishedWithLoadingTrueCount = isLastEmitLoadingMap.count { it.value }
assert(finishedWithLoadingTrueCount == 0) { "Finished with loading true count = $finishedWithLoadingTrueCount" }
}

}
}