Skip to content

Commit

Permalink
Bump coroutines to 1.6.1-native-mt
Browse files Browse the repository at this point in the history
  • Loading branch information
bnvinay92 committed Apr 13, 2022
1 parent 244d58e commit b246f3b
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 271 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ kotest = "5.1.0"
kotlin = "1.6.10"

kotlinx-binary-compatibility = "0.6.0"
kotlinx-coroutines = "1.5.1"
kotlinx-coroutines = "1.6.1-native-mt"
kotlinx-serialization-json = "1.3.2"

ktlint = "10.2.1"
Expand Down
108 changes: 54 additions & 54 deletions workflow-core/src/test/java/com/squareup/workflow1/SinkTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import java.util.concurrent.atomic.AtomicInteger
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -23,40 +27,38 @@ internal class SinkTest {

private val sink = RecordingSink()

@Test fun `collectToSink sends action`() {
runBlockingTest {
val flow = MutableStateFlow(1)
val collector = launch {
flow.collectToSink(sink) {
action {
state = "$props $state $it"
setOutput("output: $it")
}
@Test fun `collectToSink sends action`() = runTest {
val flow = MutableStateFlow(1)
val collector = launch {
flow.collectToSink(sink) {
action {
state = "$props $state $it"
setOutput("output: $it")
}
}
}

advanceUntilIdle()
assertEquals(1, sink.actions.size)
sink.actions.removeFirst()
.let { action ->
val (newState, output) = action.applyTo("props", "state")
assertEquals("props state 1", newState)
assertEquals("output: 1", output?.value)
}
assertTrue(sink.actions.isEmpty())

flow.value = 2
advanceUntilIdle()
assertEquals(1, sink.actions.size)
sink.actions.removeFirst()
.let { action ->
val (newState, output) = action.applyTo("props", "state")
assertEquals("props state 2", newState)
assertEquals("output: 2", output?.value)
}
advanceUntilIdle()
assertEquals(1, sink.actions.size)
sink.actions.removeFirst()
.let { action ->
val (newState, output) = action.applyTo("props", "state")
assertEquals("props state 1", newState)
assertEquals("output: 1", output?.value)
}
assertTrue(sink.actions.isEmpty())

flow.value = 2
advanceUntilIdle()
assertEquals(1, sink.actions.size)
sink.actions.removeFirst()
.let { action ->
val (newState, output) = action.applyTo("props", "state")
assertEquals("props state 2", newState)
assertEquals("output: 2", output?.value)
}

collector.cancel()
}
collector.cancel()
}

@Test fun `collectToSink propagates backpressure`() {
Expand All @@ -69,7 +71,7 @@ internal class SinkTest {
sentActions += it
}

runBlockingTest {
runTest(UnconfinedTestDispatcher()) {
val collectJob = launch {
flow.collectToSink(sink) { action { setOutput(it) } }
}
Expand Down Expand Up @@ -118,7 +120,7 @@ internal class SinkTest {
setOutput("output")
}

runBlockingTest {
runTest {
launch { sink.sendAndAwaitApplication(action) }
advanceUntilIdle()

Expand All @@ -130,33 +132,32 @@ internal class SinkTest {
}
}

@Test fun `sendAndAwaitApplication suspends until after applied`() {
runBlockingTest {
var resumed = false
val action = action<String, String, String> {
assertFalse(resumed)
}
launch {
sink.sendAndAwaitApplication(action)
resumed = true
}
advanceUntilIdle()
@Test fun `sendAndAwaitApplication suspends until after applied`() = runTest {
var resumed = false
val action = action<String, String, String> {
assertFalse(resumed)
assertEquals(1, sink.actions.size)
}
launch {
sink.sendAndAwaitApplication(action)
resumed = true
}
advanceUntilIdle()
assertFalse(resumed)
assertEquals(1, sink.actions.size)

val enqueuedAction = sink.actions.removeFirst()
pauseDispatcher()
enqueuedAction.applyTo("props", "state")
val enqueuedAction = sink.actions.removeFirst()

withContext(StandardTestDispatcher(testScheduler)) {
enqueuedAction.applyTo("props", "state")
assertFalse(resumed)
resumeDispatcher()
advanceUntilIdle()
assertTrue(resumed)
}

advanceUntilIdle()
assertTrue(resumed)
}

@Test fun `sendAndAwaitApplication doesn't apply action when cancelled while suspended`() {
runBlockingTest {
@Test fun `sendAndAwaitApplication doesn't apply action when cancelled while suspended`() =
runTest {
var applied = false
val action = action<String, String, String> {
applied = true
Expand All @@ -176,7 +177,6 @@ internal class SinkTest {
assertEquals("state", newState)
assertNull(output)
}
}

private class RecordingSink : Sink<WorkflowAction<String, String, String>> {
val actions = mutableListOf<WorkflowAction<String, String, String>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal class RealRenderContext<out PropsT, StateT, OutputT>(
"Expected sink to not be sent to until after the render pass. Received action: $value"
)
}
eventActionsChannel.offer(value)
eventActionsChannel.trySend(value)
}

override fun <ChildPropsT, ChildOutputT, ChildRenderingT> renderChild(
Expand Down
Loading

0 comments on commit b246f3b

Please sign in to comment.