Youssef Shoaib [MOD]
01/25/2025, 3:23 AMsuspend fun main
bridge machinery in a test? I have a library that does some coroutines magic, and I want to test that it works even in suspend fun main
and all the missing things that that entails (no Dispatcher
or Interceptor
).jw
01/25/2025, 3:33 AMCoroutineScope(EmptyCoroutineContext)
or something? Maybe plus a Job
?jw
01/25/2025, 3:34 AMYoussef Shoaib [MOD]
01/25/2025, 3:51 AMJob
since that's kotlinx-coroutines
. I'll have a look at what it generates! Was just wondering if there's some official-ish way instead of going down this rabbit-hole.
I remember from digging a while back that the implementation is platform-specific or something, which might be annoying since I'll have to do 3 different behaviours.Youssef Shoaib [MOD]
01/25/2025, 3:58 AMkotlin.coroutines.jvm.internal.RunSuspend
, which is a very simple implementation. I think I'll either use it and suppress the invisible error, or just copy that implementation.Youssef Shoaib [MOD]
01/25/2025, 4:16 AMfunction mainWrapper() {
main(get_EmptyContinuation());
}
Where EmptyContinuation is just Continuation(EmptyCoroutineContext) { result -> result.getOrThrow() }
I can't find what native does by just looking on Github. I'll look through the compiled code laterephemient
01/25/2025, 5:49 AMsuspend fun main()
on native. https://youtrack.jetbrains.com/issue/KT-52753kevin.cianfarini
01/25/2025, 2:39 PMsuspend fun main
.kevin.cianfarini
01/25/2025, 2:40 PMkevin.cianfarini
01/25/2025, 2:42 PMephemient
01/25/2025, 3:22 PMsuspend fun main()
is the only reasonable recourse for top-level async in Kotlin/JS IMOkevin.cianfarini
01/25/2025, 3:26 PMrunTest
? eg.
// on kotlin/JS
fun main = runSuspendingEntrypoint { ... }
suspending main functions are documented primarily as a good entrypoint into kotlinx-coroutines, but it's missing specific features. Notably, it's not cancellation aware and calls to delay
on concurrent platforms will spin up the default delay on additional threads. In contrast, runBlocking's event loop implements delay so no additional threads are required.ephemient
01/25/2025, 3:29 PMactual typealias TestResult
being a completely different type is an incredible hack and makes it far too easy to accidentally discardkevin.cianfarini
01/25/2025, 3:30 PMrunTest
ever think about it, and instead the library maintainers are the ones who have to manage the test result. Wouldn't the same hold true here?Youssef Shoaib [MOD]
01/25/2025, 3:33 PMsuspend fun main
doesn't use the TestResult
mechanism or promise-returning or anything like it. It just passes an empty continuation to main
, so I'm not sure if discarding is a concern here.kevin.cianfarini
01/25/2025, 3:34 PMTestResult
dance is to make sure that exceptions are reported properly in tests from JS. I'm not sure that'd be required for a top level suspending entrypoint. For example, it might be a quirk of the JS test runner. I'm not certain, thoughjw
01/25/2025, 3:49 PMjw
01/25/2025, 3:49 PMjw
01/25/2025, 3:51 PMTestResult
type exists to abstract over the need to return a value (or not) from common code. The suspend fun main
feature is in the language and implemented by the compiler, so even if it did need to return a promise it could do so directly as there's no common library API here.kevin.cianfarini
01/25/2025, 3:58 PMsuspend fun main
shows that there's not any actual real need for the compiler to support this. A common entrypoint offered in kotlinx-coroutines could simply be:
• Run the function with a new continuation on JS, and
• an alias for runBlocking elsewherekevin.cianfarini
01/25/2025, 4:00 PMsuspend fun main() = coroutineScope { ... }
And on multi threaded platforms this is strictly worse than runBlocking imoYoussef Shoaib [MOD]
01/25/2025, 4:01 PMrunSuspend
function that does what the suspend fun main
currently does. I think the fear is maybe that it'd be misused? I also like the elegance of having suspend
literally to the entry point of the application, but that's aesthetics and nothing else.kevin.cianfarini
01/25/2025, 4:02 PMkevin.cianfarini
01/25/2025, 4:02 PMkevin.cianfarini
01/25/2025, 4:11 PMI think the fear is maybe that it'd be misused? I also like the elegance of havingThis issue is perhaps also relevant.literally to the entry point of the application, but that's aesthetics and nothing else.suspend