-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Send async messages to the Android main looper #577
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
ui/kotlinx-coroutines-android/test/HandlerDispatcherTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.experimental.android | ||
|
||
import android.os.Build | ||
import android.os.Looper | ||
import android.os.Message | ||
import android.os.MessageQueue | ||
import kotlinx.coroutines.experimental.Dispatchers | ||
import kotlinx.coroutines.experimental.GlobalScope | ||
import kotlinx.coroutines.experimental.launch | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.Shadows.shadowOf | ||
import org.robolectric.annotation.Config | ||
import org.robolectric.shadows.ShadowLooper | ||
import org.robolectric.util.ReflectionHelpers | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
@Config(manifest = Config.NONE, sdk = [28]) | ||
class HandlerDispatcherTest { | ||
|
||
/** | ||
* Because [Dispatchers.Main] is a singleton, we cannot vary its initialization behavior. As a | ||
* result we only test its behavior on the newest API level and assert that it uses async | ||
* messages. We rely on the other tests to exercise the variance of the mechanism that the main | ||
* dispatcher uses to ensure it has correct behavior on all API levels. | ||
*/ | ||
@Test fun mainIsAsync() { | ||
ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 28) | ||
|
||
val mainLooper = ShadowLooper.getShadowMainLooper() | ||
mainLooper.pause() | ||
val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) | ||
|
||
val job = GlobalScope.launch(Dispatchers.Main) {} | ||
|
||
val message = mainMessageQueue.head | ||
assertTrue(message.isAsynchronous) | ||
|
||
job.cancel() | ||
} | ||
|
||
@Test fun asyncMessagesApi14() { | ||
ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 14) | ||
|
||
val main = Looper.getMainLooper().asHandler(async = true).asCoroutineDispatcher() | ||
|
||
val mainLooper = ShadowLooper.getShadowMainLooper() | ||
mainLooper.pause() | ||
val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) | ||
|
||
val job = GlobalScope.launch(main) {} | ||
|
||
val message = mainMessageQueue.head | ||
assertFalse(message.isAsynchronous) | ||
|
||
job.cancel() | ||
} | ||
|
||
@Test fun asyncMessagesApi16() { | ||
ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 16) | ||
|
||
val main = Looper.getMainLooper().asHandler(async = true).asCoroutineDispatcher() | ||
|
||
val mainLooper = ShadowLooper.getShadowMainLooper() | ||
mainLooper.pause() | ||
val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) | ||
|
||
val job = GlobalScope.launch(main) {} | ||
|
||
val message = mainMessageQueue.head | ||
assertTrue(message.isAsynchronous) | ||
|
||
job.cancel() | ||
} | ||
|
||
@Test fun asyncMessagesApi28() { | ||
ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 28) | ||
|
||
val main = Looper.getMainLooper().asHandler(async = true).asCoroutineDispatcher() | ||
|
||
val mainLooper = ShadowLooper.getShadowMainLooper() | ||
mainLooper.pause() | ||
val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) | ||
|
||
val job = GlobalScope.launch(main) {} | ||
|
||
val message = mainMessageQueue.head | ||
assertTrue(message.isAsynchronous) | ||
|
||
job.cancel() | ||
} | ||
|
||
@Test fun noAsyncMessagesIfNotRequested() { | ||
ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 28) | ||
|
||
val main = Looper.getMainLooper().asHandler(async = false).asCoroutineDispatcher() | ||
|
||
val mainLooper = ShadowLooper.getShadowMainLooper() | ||
mainLooper.pause() | ||
val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) | ||
|
||
val job = GlobalScope.launch(main) {} | ||
|
||
val message = mainMessageQueue.head | ||
assertFalse(message.isAsynchronous) | ||
|
||
job.cancel() | ||
} | ||
|
||
// TODO compile against API 23+ so this can be invoked without reflection. | ||
private val Looper.queue: MessageQueue | ||
get() = Looper::class.java.getDeclaredMethod("getQueue").invoke(this) as MessageQueue | ||
|
||
// TODO compile against API 22+ so this can be invoked without reflection. | ||
private val Message.isAsynchronous: Boolean | ||
get() = Message::class.java.getDeclaredMethod("isAsynchronous").invoke(this) as Boolean | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we provide system property to opt-out this behaviour?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... maybe. It's a challenging situation because disabling this makes the entire app slower where you might just want to change the behavior for a single coroutine.
It may be worth discussing providing a dispatcher that is always
async=false
to the main thread so that decisions can be made on a coroutine-by-coroutine basis rather than globally to the whole app.I worry about the case when someone has a problem, disables async to "fix" it, and then that starts getting cargo culted around slowing down everyone's apps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.
Let's wait for the feedback from the users and will discuss solutions for their particular problems.
This PR carries enough value to release it as is.