https://kotlinlang.org logo
Title
b

bbaldino

05/28/2020, 6:44 PM
Is there a good way to enforce a matcher is true...within some timeout? I'm trying to write tests for some ktor websocket code and I have to use a real server (there's no websocket client test harness) so I need some timing tolerances. I was thinking some helper function which could wrap a matcher call, like:
within (5.milliseconds) {
    someField shouldBe someValue
}
I'm playing with:
private suspend fun within(timeout: Duration, block: () -> Unit) {
    withTimeout(timeout.toMillis()) {
        while (true) {
            try {
                block()
                break
            } catch (t: Throwable) {
                // Test every 5 ms
                delay(5)
            }
        }
    }
}
but this weirdly seems to make the test fail instantly, complaining
Test did not completed within 600000ms
AH...it's firing the same exceptiona s the timeout does