ktor icon indicating copy to clipboard operation
ktor copied to clipboard

KTOR-2021 Add GMTClock

Open hfhbd opened this issue 3 years ago • 3 comments
trafficstars

Subsystem Server, Client Core

Motivation

  • Add a mockable Clock Clock for mocking time: https://youtrack.jetbrains.com/issue/KTOR-2021

Status

  • [x] Compiling
  • [ ] Tests: Use TestTimeSource() via TestScope to provide a GMTClock to the new needed parameter. When coroutines 1.6.0 is merged, and https://youtrack.jetbrains.com/issue/KTOR-3571 is implemented, the testTimeSource from runTest can be used.
  • [ ] APIDump

Next Steps

  • Refactor all tests to support delay skipping runTest via testTimeSource.toClock()

hfhbd avatar Dec 22 '21 13:12 hfhbd

@rsinukov Hey Rustam, because this PR is quite large, before fixing all test code, could you provide some feedback regarding the main changes (GMTDate, GMTClock, Environment and HttpResponse)? :)

hfhbd avatar Jan 19 '22 12:01 hfhbd

HI @hfhbd Is there a reason why we may need multiple clocks in the application? If not, we can use a service loader or some similar pattern to provide a single clock to different parts of the app. This will make PR less invasive and easier to upgrade to kotlinx.datetime in the future.

rsinukov avatar Jan 20 '22 14:01 rsinukov

@rsinukov For the user, there are only "2" clocks needed:

@Test
fun testing() = runTest {
    val testClock = testTimeSource.toGMTClock()
    val server = embeddedServer(CIO) {
        environment.clock = testClock
    }
    val client = HttpClient(io.ktor.client.engine.cio.CIO) {
        engine {
            clock = testClock
        }
    }
   // to something with client and server having a mocked clock
  testTimeSource += 10.seconds
}

In ktor code basis, I often needed to add the clock as a parameter, because WeakTimeQueue needs its own clock, see also https://youtrack.jetbrains.com/issue/KTOR-3658. Otherwise, internal client.engine.clock/environment.config.clock is used (if possible). But of course, we could switch to some kind of dependency injection/service loader.

hfhbd avatar Jan 20 '22 14:01 hfhbd

Hey @hfhbd, thanks for the contribution. We prefer to migrate to kotlinx.datetime clock instead

e5l avatar Jan 04 '24 12:01 e5l

So it's stable now? :D https://github.com/ktorio/ktor/pull/2750#issuecomment-999404419

hfhbd avatar Jan 04 '24 12:01 hfhbd

Nope, but we have a clear estimate

e5l avatar Jan 04 '24 12:01 e5l

So you want to implement it or should I rebase it and merge it with #2750?

hfhbd avatar Jan 05 '24 08:01 hfhbd

Let's merge it with #2750. Could you help me with that?

e5l avatar Jan 05 '24 08:01 e5l

Sure

hfhbd avatar Jan 05 '24 08:01 hfhbd