Skip to content

Commit 1ff0bb9

Browse files
committed
misc cleanup
1 parent ff8c09b commit 1ff0bb9

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewFactoryTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ internal class ComposeViewFactoryTest {
3737
@get:Rule val rules: RuleChain =
3838
RuleChain.outerRule(DetectLeaksAfterTestSuccess())
3939
.around(IdleAfterTestRule)
40-
.around(IdlingDispatcherRule)
4140
.around(composeRule)
41+
.around(IdlingDispatcherRule)
4242

4343
@Test fun showsComposeContent() {
4444
val viewFactory = composeViewFactory<Unit> { _, _ ->

workflow-ui/internal-testing-android/src/main/java/com/squareup/workflow1/ui/internal/test/IdlingDispatcherRule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public object IdlingDispatcherRule : TestWatcher() {
1313

1414
private lateinit var dispatcher: IdlingDispatcher
1515

16-
override fun starting(description: Description?) {
16+
override fun starting(description: Description) {
1717
dispatcher = IdlingDispatcher(Dispatchers.Main.immediate)
1818
Dispatchers.setMain(dispatcher)
1919

2020
IdlingRegistry.getInstance().register(dispatcher.counter)
2121
}
2222

23-
override fun finished(description: Description?) {
23+
override fun finished(description: Description) {
2424
Dispatchers.resetMain()
2525
IdlingRegistry.getInstance().unregister(dispatcher.counter)
2626
}

workflow-ui/internal-testing-android/src/test/java/com/squareup/workflow1/ui/internal/test/IdlingDIspatcherTest.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.squareup.workflow1.ui.internal.test
22

33
import com.google.common.truth.Truth.assertThat
4-
import kotlinx.coroutines.CompletableDeferred
54
import kotlinx.coroutines.CoroutineDispatcher
5+
import kotlinx.coroutines.cancel
66
import kotlinx.coroutines.launch
77
import kotlinx.coroutines.runBlocking
88
import kotlinx.coroutines.withContext
@@ -49,22 +49,28 @@ class IdlingDIspatcherTest {
4949

5050
val idler = IdlingDispatcher(a)
5151

52-
// This will never be completed
53-
val lock = CompletableDeferred<Unit>()
54-
55-
assertThat(idler.isIdle()).isTrue()
56-
5752
val job = launch(idler) {
5853
withContext(b) {
59-
lock.await()
60-
fail("unreachable")
54+
55+
// second -- cancel this current coroutine before it's completed. Call yield() right away
56+
// because we need a suspension point in order to make the coroutine check for cancellation.
57+
cancel()
58+
yield()
59+
60+
// should be unreachable
61+
fail("cancellation somehow didn't stop this coroutine?")
6162
}
6263
}
6364

64-
// ensure that the launched job has a chance to dispatch
65+
// first -- nothing has actually dispatched yet, so the idler could be idle. Use `yield()` to
66+
// allow the coroutine within `job` to start executing
67+
assertThat(idler.isIdle()).isTrue()
6568
yield()
6669

67-
job.cancel()
70+
// third -- the job should actually have completed before this is called, but use `.join()` just
71+
// to be super sure. If we're able to get past `.join()` that means the coroutine was
72+
// cancelled, so the call to `fail()` didn't happen.
73+
job.join()
6874
assertThat(idler.isIdle()).isTrue()
6975
}
7076

@@ -77,6 +83,6 @@ class IdlingDIspatcherTest {
7783
block.run()
7884
}
7985

80-
override fun toString(): String = "RecordingDispatcher ($name)"
86+
override fun toString(): String = "NamedDispatcher ($name)"
8187
}
8288
}

0 commit comments

Comments
 (0)