calidion
02/04/2025, 4:48 PMrunBlocking {
val pm = PeerManager(app)
pm.start()
val timer = Timer()
timer.schedule(object : TimerTask() {
override fun run() {
pm.stop()
}
}, PEER_LOOP_DELAY*5)
}
This code seems not workingMichael Krussel
02/04/2025, 4:58 PMrunBlocking
and why are you using it in the snippet. The runBlocking
won't wait for the timer to finish since schedule
is not a blocking call.calidion
02/04/2025, 5:04 PMMichael Krussel
02/04/2025, 5:10 PMcalidion
02/04/2025, 5:11 PMcalidion
02/04/2025, 5:12 PMpm.stop
is executed.Michael Krussel
02/04/2025, 5:15 PMrunBlocking
and delay, or a CountdownLatch waiting for the timer to go off.
In general I would rework PeerManager
to use a virtual timer that allows you to advance its time by a given amount for testing. If it is using coroutines internally, I would look at the test dispatchers and runTest
.calidion
02/04/2025, 5:17 PMMichael Krussel
02/04/2025, 5:19 PMfun test() = runTest {
val pm = PeerManager(app, backgroundScope)
pm.start()
delay(PEER_LOOP_DELAY * 5)
pm.stop()
// asserts
}
Michael Krussel
02/04/2025, 5:22 PMcalidion
02/04/2025, 5:29 PM