https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

Dmytro Danylyk

03/07/2017, 10:06 AM
You mean use
runBlocking
in test function to wrap my original code which uses
launch(CommPool)
?
e

elizarov

03/07/2017, 10:10 AM
dmytrodanylyk: Replace
launch(CommonPool)
in your test code with `runBlocking`:
Copy code
@Test
fun myTest() = runBlocking { 
   // I can use suspending functions here!
}
There are lots of worked out examples in the guide: https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#bridging-blocking-and-non-blocking-worlds
d

Dmytro Danylyk

03/07/2017, 10:12 AM
Thx for code samples
java.lang.Exception: Method myTest() should be void
runBlocking { // code } did the trick
e

elizarov

03/07/2017, 8:53 PM
Alternatively, you can do
runBlocking<Unit>
d

Dmytro Danylyk

03/07/2017, 9:07 PM
ok, thx
Any idea how I can test following function:
Copy code
fun sendFeedback(message: String) = launch(MainThread) {
        val request = asyncSendFeedback(message)
        val response = request.await()
}
MainThread - android ui dispatcher
e

elizarov

03/08/2017, 9:21 PM
You can test
asyncSendFeedback
d

Dmytro Danylyk

03/09/2017, 9:51 AM
Yep, but in
sendFeedback
after I got
response
I have a lot more logic, so would be cool if I could write test for
sendFeedback
e

elizarov

03/09/2017, 9:53 AM
Then I’d recommend to replace the line
Copy code
fun sendFeedback(message: String) = launch(MainThread) {
with
Copy code
suspend fun sendFeedback(message: String) {
This way it becomes a suspending function that is easy to use whenever you need and to test, too
d

Dmytro Danylyk

03/09/2017, 9:54 AM
ok I see, thanks
5 Views