ktorio / ktorio/ktor

HttpTimeout plugin does not work with test dispatchers

Open
#4,720 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
14.5k
Forks
1.3k
Avg merge
2d 13h
Merged PRs (30d)
49

Description

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?

```kotlin
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 {
client.get("/")
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example with kotlinx.coroutines runTest, MockEngine, Mutex, and the HttpTimeout plugin. Read the HttpTimeout and test-dispatcher behavior involved in the example, then determine whether the timeout should follow virtual time or whether an alternative test approach is needed. Done means the behavior is clarified and covered by a regression test or documented testing guidance.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.