|
| 1 | +package kotlinx.coroutines.flow.operators |
| 2 | + |
| 3 | +import kotlinx.coroutines.* |
| 4 | +import kotlinx.coroutines.channels.* |
| 5 | +import kotlinx.coroutines.flow.* |
| 6 | +import kotlinx.coroutines.testing.* |
| 7 | +import kotlin.test.* |
| 8 | + |
| 9 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 10 | +class ChunkedTest : TestBase() { |
| 11 | + |
| 12 | + @Test |
| 13 | + fun testChunked() = runTest { |
| 14 | + doTest(flowOf(1, 2, 3, 4, 5), 2, listOf(listOf(1, 2), listOf(3, 4), listOf(5))) |
| 15 | + doTest(flowOf(1, 2, 3, 4, 5), 3, listOf(listOf(1, 2, 3), listOf(4, 5))) |
| 16 | + doTest(flowOf(1, 2, 3, 4), 2, listOf(listOf(1, 2), listOf(3, 4))) |
| 17 | + doTest(flowOf(1), 3, listOf(listOf(1))) |
| 18 | + } |
| 19 | + |
| 20 | + private suspend fun <T> doTest(flow: Flow<T>, chunkSize: Int, expected: List<List<T>>) { |
| 21 | + assertEquals(expected, flow.chunked(chunkSize).toList()) |
| 22 | + assertEquals(flow.toList().chunked(chunkSize), flow.chunked(chunkSize).toList()) |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun testEmpty() = runTest { |
| 27 | + doTest(emptyFlow<Int>(), 1, emptyList()) |
| 28 | + doTest(emptyFlow<Int>(), 2, emptyList()) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun testChunkedCancelled() = runTest { |
| 33 | + val result = flow { |
| 34 | + expect(1) |
| 35 | + emit(1) |
| 36 | + emit(2) |
| 37 | + expect(2) |
| 38 | + }.chunked(1).buffer().take(1).toList() |
| 39 | + assertEquals(listOf(listOf(1)), result) |
| 40 | + finish(3) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun testChunkedCancelledWithSuspension() = runTest { |
| 45 | + val result = flow { |
| 46 | + expect(1) |
| 47 | + emit(1) |
| 48 | + yield() |
| 49 | + expectUnreached() |
| 50 | + emit(2) |
| 51 | + }.chunked(1).buffer().take(1).toList() |
| 52 | + assertEquals(listOf(listOf(1)), result) |
| 53 | + finish(2) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun testChunkedDoesNotIgnoreCancellation() = runTest { |
| 58 | + expect(1) |
| 59 | + val result = flow { |
| 60 | + coroutineScope { |
| 61 | + launch { |
| 62 | + hang { expect(2) } |
| 63 | + } |
| 64 | + yield() |
| 65 | + emit(1) |
| 66 | + emit(2) |
| 67 | + } |
| 68 | + }.chunked(1).take(1).toList() |
| 69 | + assertEquals(listOf(listOf(1)), result) |
| 70 | + finish(3) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun testIae() { |
| 75 | + assertFailsWith<IllegalArgumentException> { emptyFlow<Int>().chunked(-1) } |
| 76 | + assertFailsWith<IllegalArgumentException> { emptyFlow<Int>().chunked(0) } |
| 77 | + assertFailsWith<IllegalArgumentException> { emptyFlow<Int>().chunked(Int.MIN_VALUE) } |
| 78 | + assertFailsWith<IllegalArgumentException> { emptyFlow<Int>().chunked(Int.MIN_VALUE + 1) } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun testSample() = runTest { |
| 83 | + val result = flowOf("a", "b", "c", "d", "e") |
| 84 | + .chunked(2) |
| 85 | + .map { it.joinToString(separator = "") } |
| 86 | + .toList() |
| 87 | + assertEquals(listOf("ab", "cd", "e"), result) |
| 88 | + } |
| 89 | +} |
0 commit comments