forked from hush/SilentDragonAndroid
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
package cash.z.ecc.android.feedback
|
|
|
|
import kotlinx.coroutines.runBlocking
|
|
import org.junit.Assert.assertEquals
|
|
import org.junit.Test
|
|
|
|
class FeedbackObserverTest {
|
|
|
|
private val feedback: Feedback = Feedback()
|
|
private val feedbackCoordinator: FeedbackCoordinator = FeedbackCoordinator(feedback)
|
|
|
|
private var counter: Int = 0
|
|
private val simpleAction = object : Feedback.Action {
|
|
override val key = "ButtonClick"
|
|
}
|
|
|
|
@Test
|
|
fun testConcurrency() = runBlocking {
|
|
val actionCount = 50
|
|
val processorCount = 50
|
|
val expectedTotal = actionCount * processorCount
|
|
|
|
repeat(processorCount) {
|
|
addObserver()
|
|
}
|
|
|
|
feedback.start()
|
|
repeat(actionCount) {
|
|
sendAction()
|
|
}
|
|
|
|
feedback.await() // await sends
|
|
feedbackCoordinator.await() // await processing
|
|
feedback.stop()
|
|
|
|
assertEquals(
|
|
"Concurrent modification happened ${expectedTotal - counter} times",
|
|
expectedTotal,
|
|
counter
|
|
)
|
|
}
|
|
|
|
private fun addObserver() {
|
|
feedbackCoordinator.addObserver(object : FeedbackCoordinator.FeedbackObserver {
|
|
override fun onAction(action: Feedback.Action) {
|
|
counter++
|
|
}
|
|
})
|
|
}
|
|
|
|
private fun sendAction() {
|
|
feedback.report(simpleAction)
|
|
}
|
|
|
|
}
|