I want to create a library with a custom time-base...
# coroutines
f
I want to create a library with a custom time-based operator extension on Flow, such as for example
Flow<T>.throttleFirst()
here. @elizarov wrote that to test such an operator we would have to use the
DelayController
if available instead of
System.currentTimeMills()
, which of course makes total sense. My question: How could I publish this library without having
kotlinx-coroutines-test
in the release dependencies? Meaning how would I prevent the user of my library having
kotlinx-coroutines-test
in their release binaries when they use my dependency?
l
You can just use
TimeSource.Monotonic
and
delay
(experimental time API). Then, if users use delay controlling dispatcher, it will just work, without you having to do anything special.
f
Thanks! That would mean I would require the library user to use an experimental api, which is fine for my use-case. But is there a way that this could be implemented without this experimental API and not publishing it with test dependencies in release?
l
@flosch Yes, you can use
System.nanoTime()
yourself (on JVM/ART, other platforms have different APIs), and count milliseconds after converting from nanos, or count it all in nanoseconds.
f
Sure, yes, that is what I am currently doing! However this does not let me use the
DelayController
, when testing my custom operator. Or am I missing something?
l
You can use
delay
which is controlled by
DelayController
in test conditions.
Having a library control the
DelayController
from under would be surprising to the consumer projects and might break other parts of the test(s).
f
Ahh okay, yea that actually makes sense! Thank you very much