Skip to content

Commit

Permalink
Be resilient to dispatch order
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Oct 29, 2023
1 parent df396a6 commit a34ebe7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package app.cash.redwood.treehouse

import assertk.assertThat
import assertk.assertions.containsExactlyInAnyOrder
import assertk.assertions.isEqualTo
import kotlinx.coroutines.channels.Channel

Expand All @@ -35,6 +36,18 @@ class EventLog {
assertThat(takeEvent()).isEqualTo(event)
}

/**
* Take all the events in [events], in any order. Use this when events published are dependent on
* dispatch order.
*/
suspend fun takeEventsInAnyOrder(vararg events: String) {
val actual = mutableListOf<String>()
while (actual.size < events.size) {
actual += takeEvent()
}
assertThat(actual).containsExactlyInAnyOrder(*events)
}

fun assertNoEvents() {
val received = events.tryReceive()
check(received.isFailure) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class FakeCodeListener(
view: TreehouseView<*>,
e: Throwable,
) {
eventLog += "codeListener.onUncaughtException($view, $e)"
// Canonicalize "java.lang.Exception(boom!)" to "kotlin.Exception(boom!)".
val exceptionString = e.toString().replace("java.lang.", "kotlin.")
eventLog += "codeListener.onUncaughtException($view, $exceptionString)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,11 @@ class TreehouseAppContentTest {
eventLog.takeEvent("codeSessionA.app.uis[0].start()")

codeHost.triggerException(Exception("boom!"))
eventLog.takeEvent("codeSessionA.app.uis[0].close()")
eventLog.takeEvent("codeListener.onUncaughtException(view1, java.lang.Exception: boom!)")
eventLog.takeEvent("codeSessionA.cancel()")
eventLog.takeEventsInAnyOrder(
"codeSessionA.app.uis[0].close()",
"codeListener.onUncaughtException(view1, kotlin.Exception: boom!)",
"codeSessionA.cancel()",
)

content.unbind()
}
Expand Down Expand Up @@ -346,9 +348,11 @@ class TreehouseAppContentTest {
eventLog.takeEvent("codeSessionA.app.uis[0].start()")

codeHost.triggerException(Exception("boom!"))
eventLog.takeEvent("codeSessionA.app.uis[0].close()")
eventLog.takeEvent("codeListener.onUncaughtException(view1, java.lang.Exception: boom!)")
eventLog.takeEvent("codeSessionA.cancel()")
eventLog.takeEventsInAnyOrder(
"codeSessionA.app.uis[0].close()",
"codeListener.onUncaughtException(view1, kotlin.Exception: boom!)",
"codeSessionA.cancel()",
)

codeHost.session = FakeCodeSession("codeSessionB", eventLog)
eventLog.takeEvent("codeSessionB.start()")
Expand Down

0 comments on commit a34ebe7

Please sign in to comment.