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

Documentation on Kotlin's suspend #3

Open
binkley opened this issue Aug 20, 2021 · 3 comments
Open

Documentation on Kotlin's suspend #3

binkley opened this issue Aug 20, 2021 · 3 comments

Comments

@binkley
Copy link

binkley commented Aug 20, 2021

Is there documentation on the interaction of Awaitility with Kotlin's suspend?

I'd like to use Awaitility in a Kotlin library that itself it built around use of suspend. Presently there are icky sleep-like calls in tests, eg:

suspend fun waitForDispatch(millis: Long = 50) = delay(millis)

And the test functions themselves do not guard against timeout, etc. :(

I've used Awaitility on Java projects, and am eager to do the same for Kotlin.

@johanhaleby
Copy link
Contributor

There is a Kotlin DSL but it's not using suspend function. It's simply a nicer API from the Kotlin perspective but it uses the Java code underneath. Would be cool with better support for coroutines though. What would such an API look like?

@binkley
Copy link
Author

binkley commented Aug 20, 2021

One of the nuisances of suspend is that the compiler transforms your function signatures, so to have an API like this:

interface Foo {
    fun somethingCool()
}

requires a parallel API:

interface CoroutineFoo {
    suspend fun somethingCool()
}

The bytecode of Foo and SuspendFoo are not the same! The compiler adds a hidden coroutine context parameter to CoroutineFoo.somethingCool() visible in the bytecode, transparent to the caller.

I'm not sure what the shape of API would be for Awaitility. Take this example from the docs:

val data = await untilNotNull { myDataRepository.findById("id") }

and say it looks like this:

val data = await untilNotNull { foo.somethingCool() }

The code block needs a coroutine context to execute because somethingCool() is a suspending function.

@binkley
Copy link
Author

binkley commented Aug 20, 2021

Thinking about it more.

It might mean a parallel API of imports so that what the caller types is the same with or without suspend functions. Of course, that would be messy if caller mixes suspend and non-suspend functions in the same test file.

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