aaverin
08/24/2018, 12:32 PMasync
only runs when I call await
Martin Devillers
08/24/2018, 12:36 PMasync
isnt’ lazy, but it is… asynchronous (shocker). So if you check immediately after calling your registerDevice
whether the registerDeviceForService
method has been called on lib
, then it likely won’t be the case. A new thread is spawned, so you have to wait for the method to be called on that thread.lib
mock into your instance, but in any case, you need to find a mechanism to suspend until `lib`’s method has been calledlouiscad
08/24/2018, 12:37 PMasync
does not spawn a new threadMartin Devillers
08/24/2018, 12:38 PMlouiscad
08/24/2018, 12:38 PMCoroutineDispatcher
it's been assigned to (DefaultDispatcher
in the current case where you pass no parameter)async
for single thread operations. As long as you have suspension points, it can be usefulaaverin
08/24/2018, 12:41 PMasync
methods? I thought wrapping test into runBlocking
solves the problemMartin Devillers
08/24/2018, 12:45 PMlouiscad
08/24/2018, 12:47 PMDeferred
. I use suspend fun
instead. I use async
for parallel coroutines execution, not in place of suspend fun
or launch
. If you really need to return a Deferred
, be sure to follow the recommendations here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#async-style-functions and use await()
to await your Deferred
inside your coroutine launched inside runBlocking
aaverin
08/24/2018, 12:51 PMsuspendCoroutine {}
. How can I test it then? If I re-write and remove async
– this suspendCoroutine still waits foreverlouiscad
08/24/2018, 12:56 PMasync
is still not the right solution in this case. Have you read this guide? https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md If not, you shouldCoroutineDispatcher
inside a suspend fun
, I use withContext(theDispatcherIWantHere) { ... }
suspendCoroutine { ... }
doesn't resume, it's because resume
has not be called, or because the CoroutineDispatcher
is blocked somewhere in your codeaaverin
08/24/2018, 1:33 PMresume
will be called only if I will have chance to mock the response
The way it usually works with RxJava, you call the method, then you catch callback on a mocked instance, and manually call that callback methods to simulate successful network responsesuspendCoroutine
return instantly so I could do the mock magicsuspendCoroutine
?louiscad
08/24/2018, 1:39 PMgildor
08/27/2018, 5:21 AMthus my original question – how to test methods withI don’t see any problem. You call this method (probably for tests easiest way to wrap this call to runBlocking), you check result of this method?suspendCoroutine