|
| 1 | +/* |
| 2 | + * Copyright 2020 Square Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.squareup.workflow |
| 17 | + |
| 18 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 19 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 20 | +import kotlinx.coroutines.launch |
| 21 | +import kotlinx.coroutines.test.runBlockingTest |
| 22 | +import kotlin.test.Test |
| 23 | +import kotlin.test.assertEquals |
| 24 | +import kotlin.test.assertFalse |
| 25 | +import kotlin.test.assertNull |
| 26 | +import kotlin.test.assertTrue |
| 27 | +import kotlin.test.fail |
| 28 | + |
| 29 | +@OptIn( |
| 30 | + ExperimentalCoroutinesApi::class, |
| 31 | + ExperimentalStdlibApi::class, |
| 32 | + ExperimentalWorkflow::class |
| 33 | +) |
| 34 | +class SinkTest { |
| 35 | + |
| 36 | + private val sink = RecordingSink() |
| 37 | + |
| 38 | + @Test fun `collectToActionSink sends action`() { |
| 39 | + runBlockingTest { |
| 40 | + val flow = MutableStateFlow(1) |
| 41 | + val collector = launch { |
| 42 | + flow.collectToSink(sink) { |
| 43 | + action { |
| 44 | + nextState = "$nextState $it" |
| 45 | + setOutput("output: $it") |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + advanceUntilIdle() |
| 51 | + assertEquals(1, sink.actions.size) |
| 52 | + sink.actions.removeFirst() |
| 53 | + .let { action -> |
| 54 | + val (newState, output) = action.applyTo("state") |
| 55 | + assertEquals("state 1", newState) |
| 56 | + assertEquals("output: 1", output) |
| 57 | + } |
| 58 | + assertTrue(sink.actions.isEmpty()) |
| 59 | + |
| 60 | + flow.value = 2 |
| 61 | + advanceUntilIdle() |
| 62 | + assertEquals(1, sink.actions.size) |
| 63 | + sink.actions.removeFirst() |
| 64 | + .let { action -> |
| 65 | + val (newState, output) = action.applyTo("state") |
| 66 | + assertEquals("state 2", newState) |
| 67 | + assertEquals("output: 2", output) |
| 68 | + } |
| 69 | + |
| 70 | + collector.cancel() |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test fun `sendAndAwaitApplication applies action`() { |
| 75 | + var applications = 0 |
| 76 | + val action = action<String, String> { |
| 77 | + applications++ |
| 78 | + nextState = "$nextState applied" |
| 79 | + setOutput("output") |
| 80 | + } |
| 81 | + |
| 82 | + runBlockingTest { |
| 83 | + launch { sink.sendAndAwaitApplication(action) } |
| 84 | + advanceUntilIdle() |
| 85 | + |
| 86 | + val enqueuedAction = sink.actions.removeFirst() |
| 87 | + val result = enqueuedAction.applyTo("state") |
| 88 | + assertEquals(1, applications) |
| 89 | + assertEquals("state applied", result.first) |
| 90 | + assertEquals("output", result.second) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test fun `sendAndAwaitApplication suspends until after applied`() { |
| 95 | + runBlockingTest { |
| 96 | + var resumed = false |
| 97 | + val action = action<String, String> { |
| 98 | + assertFalse(resumed) |
| 99 | + } |
| 100 | + launch { |
| 101 | + sink.sendAndAwaitApplication(action) |
| 102 | + resumed = true |
| 103 | + } |
| 104 | + advanceUntilIdle() |
| 105 | + assertFalse(resumed) |
| 106 | + assertEquals(1, sink.actions.size) |
| 107 | + |
| 108 | + val enqueuedAction = sink.actions.removeFirst() |
| 109 | + pauseDispatcher() |
| 110 | + enqueuedAction.applyTo("state") |
| 111 | + |
| 112 | + assertFalse(resumed) |
| 113 | + resumeDispatcher() |
| 114 | + advanceUntilIdle() |
| 115 | + assertTrue(resumed) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test fun `sendAndAwaitApplication doesn't apply action when cancelled while suspended`() { |
| 120 | + runBlockingTest { |
| 121 | + var applied = false |
| 122 | + val action = action<String, String> { |
| 123 | + applied = true |
| 124 | + fail() |
| 125 | + } |
| 126 | + val sendJob = launch { sink.sendAndAwaitApplication(action) } |
| 127 | + advanceUntilIdle() |
| 128 | + assertFalse(applied) |
| 129 | + assertEquals(1, sink.actions.size) |
| 130 | + |
| 131 | + val enqueuedAction = sink.actions.removeFirst() |
| 132 | + sendJob.cancel() |
| 133 | + advanceUntilIdle() |
| 134 | + val result = enqueuedAction.applyTo("ignored") |
| 135 | + |
| 136 | + assertFalse(applied) |
| 137 | + assertEquals("ignored", result.first) |
| 138 | + assertNull(result.second) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private class RecordingSink : Sink<WorkflowAction<String, String>> { |
| 143 | + val actions = mutableListOf<WorkflowAction<String, String>>() |
| 144 | + |
| 145 | + override fun send(value: WorkflowAction<String, String>) { |
| 146 | + actions += value |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments