Skip to content

Commit

Permalink
Ensure Dispatchers.Main != Dispatchers.Main.immediate on Android
Browse files Browse the repository at this point in the history
Fixes #3545
  • Loading branch information
dkhalanskyjb committed Nov 7, 2023
1 parent 6b9274b commit c5d1e5e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ internal class HandlerContext private constructor(
if (invokeImmediately) "$str.immediate" else str
}

override fun equals(other: Any?): Boolean = other is HandlerContext && other.handler === handler
override fun hashCode(): Int = System.identityHashCode(handler)
override fun equals(other: Any?): Boolean =
other is HandlerContext && other.handler === handler && other.invokeImmediately == invokeImmediately
override fun hashCode(): Int = System.identityHashCode(handler) xor invokeImmediately.hashCode()
}

@Volatile
Expand Down
7 changes: 7 additions & 0 deletions ui/kotlinx-coroutines-android/test/HandlerDispatcherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,11 @@ class HandlerDispatcherTest : TestBase() {
mainLooper.scheduler.advanceBy(51, TimeUnit.MILLISECONDS)
finish(5)
}

@Test
fun testHandlerDispatcherNotEqualToImmediate() {
val main = AndroidDispatcherFactory().createDispatcher(listOf())
val immediate = main.immediate
assertNotEquals(main, immediate)
}
}

0 comments on commit c5d1e5e

Please sign in to comment.