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

HttpTimeout plugin does not work with test dispatchers #4720

Open
schielek opened this issue Mar 3, 2025 · 1 comment
Open

HttpTimeout plugin does not work with test dispatchers #4720

schielek opened this issue Mar 3, 2025 · 1 comment

Comments

@schielek
Copy link

schielek commented Mar 3, 2025

I want to test my clients timeout behavior. To have a deterministic test which also runs as fast as possible I decided to use the kotlinx.coroutines test dispatcher which comes with a virtual clock. It seems that the ktor's HttpTimeoutPlugin does not respect the virtual clock of kotlinx coroutines. Is there an alternative way to test this or should the HttpTimeoutPlugin respect the timing behavior of kotlinx.coroutines?

runTest {
    val mutex = Mutex(locked = true)

    val mockEngine = MockEngine {
        mutex.withLock {
            respond("OK")
        }
    }

    val client = HttpClient(mockEngine) {
        install(HttpTimeout) {
            requestTimeoutMillis = 100
        }
    }

    launch {
        delay(200)
        mutex.unlock()
    }
    
    // Assertion error since no exception is thrown
    // Test is green when no coroutine test scope is used
    assertThrows<HttpRequestTimeoutException> {
        client.get("/")
    }
}
@Stexxe
Copy link
Contributor

Stexxe commented Mar 4, 2025

Thank you for the report. I've reproduced the issue (https://youtrack.jetbrains.com/issue/KTOR-8271).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants