Skip to content

Commit accbd32

Browse files
committed
Ensure Dispatchers.Main != Dispatchers.Main.immediate on Android
Fixes #3545
1 parent f31a62a commit accbd32

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ internal class HandlerContext private constructor(
169169
if (invokeImmediately) "$str.immediate" else str
170170
}
171171

172-
override fun equals(other: Any?): Boolean = other is HandlerContext && other.handler === handler
173-
override fun hashCode(): Int = System.identityHashCode(handler)
172+
override fun equals(other: Any?): Boolean =
173+
other is HandlerContext && other.handler === handler && other.invokeImmediately == invokeImmediately
174+
override fun hashCode(): Int = System.identityHashCode(handler) xor invokeImmediately.hashCode()
174175
}
175176

176177
@Volatile

ui/kotlinx-coroutines-android/test/HandlerDispatcherTest.kt

+7
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,11 @@ class HandlerDispatcherTest : TestBase() {
132132
mainLooper.scheduler.advanceBy(51, TimeUnit.MILLISECONDS)
133133
finish(5)
134134
}
135+
136+
@Test
137+
fun testHandlerDispatcherNotEqualToImmediate() {
138+
val main = AndroidDispatcherFactory().createDispatcher(listOf())
139+
val immediate = main.immediate
140+
assertNotEquals(main, immediate)
141+
}
135142
}

0 commit comments

Comments
 (0)