|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 New Vector Ltd |
| 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 | + |
| 17 | +package im.vector.app.core.utils |
| 18 | + |
| 19 | +import im.vector.app.test.test |
| 20 | +import kotlinx.coroutines.CoroutineScope |
| 21 | +import kotlinx.coroutines.Dispatchers |
| 22 | +import kotlinx.coroutines.delay |
| 23 | +import kotlinx.coroutines.flow.launchIn |
| 24 | +import kotlinx.coroutines.flow.onEach |
| 25 | +import kotlinx.coroutines.test.TestCoroutineScheduler |
| 26 | +import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 27 | +import kotlinx.coroutines.test.runTest |
| 28 | +import kotlinx.coroutines.withContext |
| 29 | +import org.amshove.kluent.shouldContainSame |
| 30 | +import org.junit.Test |
| 31 | + |
| 32 | +class DataSourceTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `given PublishDataSource, when posting values before observing, then no value is observed`() = runTest { |
| 36 | + val publishDataSource = PublishDataSource<Int>() |
| 37 | + publishDataSource.post(0) |
| 38 | + publishDataSource.post(1) |
| 39 | + |
| 40 | + publishDataSource.stream() |
| 41 | + .test(this) |
| 42 | + .assertNoValues() |
| 43 | + .finish() |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `given PublishDataSource with a large enough buffer size, when posting values after observing, then only the latest values are observed`() = runTest { |
| 48 | + val valuesToPost = listOf(2, 3, 4, 5, 6, 7, 8, 9) |
| 49 | + val publishDataSource = PublishDataSource<Int>(bufferSize = valuesToPost.size) |
| 50 | + publishDataSource.test(testScheduler, valuesToPost, valuesToPost) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `given PublishDataSource with a too small buffer size, when posting values after observing, then we are missing some values`() = runTest { |
| 55 | + val valuesToPost = listOf(2, 3, 4, 5, 6, 7, 8, 9) |
| 56 | + val expectedValues = listOf(2, 9) |
| 57 | + val publishDataSource = PublishDataSource<Int>(bufferSize = 1) |
| 58 | + publishDataSource.test(testScheduler, valuesToPost, expectedValues) |
| 59 | + } |
| 60 | + |
| 61 | + private suspend fun PublishDataSource<Int>.test(testScheduler: TestCoroutineScheduler, valuesToPost: List<Int>, expectedValues: List<Int>) { |
| 62 | + val values = ArrayList<Int>() |
| 63 | + val job = stream() |
| 64 | + .onEach { |
| 65 | + // Artificial delay to make consumption longer than production |
| 66 | + delay(10) |
| 67 | + values.add(it) |
| 68 | + } |
| 69 | + .launchIn(CoroutineScope(UnconfinedTestDispatcher(testScheduler))) |
| 70 | + |
| 71 | + valuesToPost.forEach { |
| 72 | + post(it) |
| 73 | + } |
| 74 | + withContext(Dispatchers.Default) { |
| 75 | + delay(11L * valuesToPost.size) |
| 76 | + } |
| 77 | + job.cancel() |
| 78 | + |
| 79 | + values shouldContainSame expectedValues |
| 80 | + } |
| 81 | +} |
0 commit comments