|
| 1 | +package app.futured.kmptemplate.util.domain |
| 2 | + |
| 3 | +import app.futured.kmptemplate.util.domain.base.BaseUseCaseExecutionScopeTest |
| 4 | +import app.futured.kmptemplate.util.domain.error.UseCaseErrorHandler |
| 5 | +import app.futured.kmptemplate.util.domain.usecases.TestFailureFlowUseCase |
| 6 | +import app.futured.kmptemplate.util.domain.usecases.TestFailureUseCase |
| 7 | +import app.futured.kmptemplate.util.domain.usecases.TestFlowUseCase |
| 8 | +import app.futured.kmptemplate.util.domain.usecases.TestUseCase |
| 9 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 10 | +import kotlinx.coroutines.launch |
| 11 | +import kotlinx.coroutines.test.TestScope |
| 12 | +import kotlin.coroutines.cancellation.CancellationException |
| 13 | +import kotlin.test.AfterTest |
| 14 | +import kotlin.test.BeforeTest |
| 15 | +import kotlin.test.Test |
| 16 | +import kotlin.test.assertEquals |
| 17 | +import kotlin.test.assertNotNull |
| 18 | +import kotlin.test.assertNull |
| 19 | +import kotlin.test.assertTrue |
| 20 | +import kotlin.test.fail |
| 21 | + |
| 22 | +/** |
| 23 | + * Sanity check UseCase tests ported from [Arkitekt](https://github.com/futuredapp/arkitekt). |
| 24 | + */ |
| 25 | +class UseCaseExecutionScopeTest : BaseUseCaseExecutionScopeTest() { |
| 26 | + |
| 27 | + @BeforeTest |
| 28 | + fun setUp() { |
| 29 | + UseCaseErrorHandler.globalOnErrorLogger = {} |
| 30 | + } |
| 31 | + |
| 32 | + @AfterTest |
| 33 | + fun tearDown() { |
| 34 | + UseCaseErrorHandler.globalOnErrorLogger = {} |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun `given 1s delay use case when executed two times then first execution cancelled`() { |
| 39 | + val testUseCase = TestUseCase() |
| 40 | + var executionCount = 0 |
| 41 | + |
| 42 | + testUseCase.execute(1) { |
| 43 | + onSuccess { executionCount++ } |
| 44 | + onError { |
| 45 | + fail("Exception thrown where shouldn't") |
| 46 | + } |
| 47 | + } |
| 48 | + viewModelScope.advanceTimeByCompat(500) |
| 49 | + |
| 50 | + testUseCase.execute(1) { |
| 51 | + onSuccess { executionCount++ } |
| 52 | + onError { fail("Exception thrown where shouldn't") } |
| 53 | + } |
| 54 | + viewModelScope.advanceTimeByCompat(1000) |
| 55 | + |
| 56 | + assertEquals(1, executionCount) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `given failing test use case when executed then indicates onError`() { |
| 61 | + val testFailureUseCase = TestFailureUseCase() |
| 62 | + var resultError: Throwable? = null |
| 63 | + |
| 64 | + testFailureUseCase.execute(IllegalStateException()) { |
| 65 | + onError { resultError = it } |
| 66 | + } |
| 67 | + viewModelScope.advanceTimeByCompat(1000) |
| 68 | + |
| 69 | + assertNotNull(resultError) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `given test flow use case when executed two times then first execution cancelled`() { |
| 74 | + val testFlowUseCase = TestFlowUseCase() |
| 75 | + val testingList = listOfNotNull(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) |
| 76 | + val resultList = mutableListOf<Int>() |
| 77 | + |
| 78 | + testFlowUseCase.execute(TestFlowUseCase.Data(testingList, 1000)) { |
| 79 | + onNext { resultList.add(it) } |
| 80 | + onError { fail("Exception thrown where shouldn't") } |
| 81 | + onComplete { fail("onComplete called where shouldn't") } |
| 82 | + } |
| 83 | + |
| 84 | + testFlowUseCase.execute(TestFlowUseCase.Data(testingList, 1000)) { |
| 85 | + onNext { resultList.add(it) } |
| 86 | + onError { fail("Exception thrown where shouldn't") } |
| 87 | + } |
| 88 | + viewModelScope.advanceTimeByCompat(10000) |
| 89 | + |
| 90 | + assertEquals(testingList, resultList) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `given test flow use case when executed and all items emitted then completes`() { |
| 95 | + val testFlowUseCase = TestFlowUseCase() |
| 96 | + var completed = false |
| 97 | + val testingList = listOfNotNull(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) |
| 98 | + |
| 99 | + testFlowUseCase.execute(TestFlowUseCase.Data(testingList, 1000)) { |
| 100 | + onError { fail("Exception thrown where shouldn't") } |
| 101 | + onComplete { completed = true } |
| 102 | + } |
| 103 | + viewModelScope.advanceTimeByCompat(10000) |
| 104 | + |
| 105 | + assertEquals(true, completed) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun `given failing flow use case when executed then indicates onError`() { |
| 110 | + val testFlowFailureUseCase = TestFailureFlowUseCase() |
| 111 | + var resultError: Throwable? = null |
| 112 | + |
| 113 | + testFlowFailureUseCase.execute(IllegalStateException()) { |
| 114 | + onNext { fail("onNext called where shouldn't") } |
| 115 | + onError { resultError = it } |
| 116 | + onComplete { fail("onComplete called where shouldn't") } |
| 117 | + } |
| 118 | + viewModelScope.advanceTimeByCompat(1000) |
| 119 | + |
| 120 | + assertNotNull(resultError) |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun `given success use case launched in coroutine then result is set to success`() { |
| 125 | + val testUseCase = TestUseCase() |
| 126 | + |
| 127 | + var result: Result<Int>? = null |
| 128 | + viewModelScope.launch { |
| 129 | + result = testUseCase.execute(1) |
| 130 | + } |
| 131 | + viewModelScope.advanceTimeByCompat(10000) |
| 132 | + |
| 133 | + assertEquals(Result.success(1), result) |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + fun `given failing use case launched in coroutine then result is set to error`() { |
| 138 | + val testUseCase = TestFailureUseCase() |
| 139 | + |
| 140 | + var result: Result<Unit>? = null |
| 141 | + viewModelScope.launch { |
| 142 | + result = testUseCase.execute(IllegalStateException()) |
| 143 | + } |
| 144 | + viewModelScope.advanceTimeByCompat(10000) |
| 145 | + |
| 146 | + assertTrue { result?.isFailure == true } |
| 147 | + assertTrue { result?.exceptionOrNull() is IllegalStateException } |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + fun `given failing use case with CancellationException launched in coroutine then error is rethrown`() { |
| 152 | + val testUseCase = TestFailureUseCase() |
| 153 | + |
| 154 | + var result: Result<Unit>? = null |
| 155 | + viewModelScope.launch { |
| 156 | + result = testUseCase.execute(CancellationException()) |
| 157 | + } |
| 158 | + viewModelScope.advanceTimeByCompat(10000) |
| 159 | + |
| 160 | + assertNull(result) |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + fun `given success use case launched two times in coroutine then the first one is cancelled`() { |
| 165 | + val testUseCase = TestUseCase() |
| 166 | + |
| 167 | + var result: Result<Int>? = null |
| 168 | + viewModelScope.launch { |
| 169 | + testUseCase.execute(1) |
| 170 | + fail("Execute should be cancelled") |
| 171 | + } |
| 172 | + viewModelScope.launch { |
| 173 | + result = testUseCase.execute(1) |
| 174 | + } |
| 175 | + viewModelScope.advanceTimeByCompat(10000) |
| 176 | + |
| 177 | + assertEquals(Result.success(1), result) |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + fun `given success use case launched two times with cancelPrevious set to false in coroutine then the first one is not cancelled`() { |
| 182 | + val testUseCase = TestUseCase() |
| 183 | + |
| 184 | + var result1: Result<Int>? = null |
| 185 | + var result2: Result<Int>? = null |
| 186 | + viewModelScope.launch { |
| 187 | + result1 = testUseCase.execute(1, cancelPrevious = false) |
| 188 | + } |
| 189 | + viewModelScope.launch { |
| 190 | + result2 = testUseCase.execute(2, cancelPrevious = false) |
| 191 | + } |
| 192 | + viewModelScope.advanceTimeByCompat(10000) |
| 193 | + |
| 194 | + assertEquals(Result.success(1), result1) |
| 195 | + assertEquals(Result.success(2), result2) |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + fun `when launchWithHandler throws an exception then this exception is send to logUnhandledException and defaultErrorHandler`() { |
| 200 | + var logException: Throwable? = null |
| 201 | + var handlerException: Throwable? = null |
| 202 | + val testOwner = object : BaseUseCaseExecutionScopeTest() { |
| 203 | + override fun defaultErrorHandler(exception: Throwable) { |
| 204 | + handlerException = exception |
| 205 | + } |
| 206 | + } |
| 207 | + UseCaseErrorHandler.globalOnErrorLogger = { exception -> |
| 208 | + logException = exception |
| 209 | + } |
| 210 | + |
| 211 | + val exception = IllegalStateException() |
| 212 | + testOwner.launchWithHandler { throw exception } |
| 213 | + testOwner.viewModelScope.advanceTimeByCompat(10000) |
| 214 | + |
| 215 | + assertEquals(exception, logException) |
| 216 | + assertEquals(exception, handlerException) |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + fun `when launchWithHandler throws an CancellationException then this exception is not send to logUnhandledException and defaultErrorHandler`() { |
| 221 | + var logException: Throwable? = null |
| 222 | + var handlerException: Throwable? = null |
| 223 | + val testOwner = object : BaseUseCaseExecutionScopeTest() { |
| 224 | + override fun defaultErrorHandler(exception: Throwable) { |
| 225 | + handlerException = exception |
| 226 | + } |
| 227 | + } |
| 228 | + UseCaseErrorHandler.globalOnErrorLogger = { exception -> |
| 229 | + logException = exception |
| 230 | + } |
| 231 | + |
| 232 | + val exception = CancellationException() |
| 233 | + testOwner.launchWithHandler { throw exception } |
| 234 | + testOwner.viewModelScope.advanceTimeByCompat(10000) |
| 235 | + |
| 236 | + assertEquals(null, logException) |
| 237 | + assertEquals(null, handlerException) |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + fun `when launchWithHandler throws an CancellationException with non cancellation cause then this exception is send to logUnhandledException only`() { |
| 242 | + var logException: Throwable? = null |
| 243 | + var handlerException: Throwable? = null |
| 244 | + val testOwner = object : BaseUseCaseExecutionScopeTest() { |
| 245 | + override fun defaultErrorHandler(exception: Throwable) { |
| 246 | + handlerException = exception |
| 247 | + } |
| 248 | + } |
| 249 | + UseCaseErrorHandler.globalOnErrorLogger = { exception -> |
| 250 | + logException = exception |
| 251 | + } |
| 252 | + |
| 253 | + val exception = CancellationException("Message", cause = IllegalStateException()) |
| 254 | + testOwner.launchWithHandler { throw exception } |
| 255 | + testOwner.viewModelScope.advanceTimeByCompat(10000) |
| 256 | + |
| 257 | + assertEquals(exception, logException) |
| 258 | + assertEquals(null, handlerException) |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + fun `when useCase is executed and onError is called then globalOnErrorLogger is called`() { |
| 263 | + val errorUseCase = TestFailureUseCase() |
| 264 | + var logException: Throwable? = null |
| 265 | + UseCaseErrorHandler.globalOnErrorLogger = { exception -> |
| 266 | + logException = exception |
| 267 | + } |
| 268 | + |
| 269 | + var resultError: Throwable? = null |
| 270 | + errorUseCase.execute(IllegalStateException()) { |
| 271 | + onError { error -> |
| 272 | + resultError = error |
| 273 | + } |
| 274 | + } |
| 275 | + viewModelScope.advanceTimeByCompat(10000) |
| 276 | + |
| 277 | + assertTrue(resultError is IllegalStateException) |
| 278 | + assertTrue(logException is IllegalStateException) |
| 279 | + } |
| 280 | + |
| 281 | + @Test |
| 282 | + fun `when flowUseCase is executed and onError is called then globalOnErrorLogger is called`() { |
| 283 | + val errorUseCase = TestFailureFlowUseCase() |
| 284 | + var logException: Throwable? = null |
| 285 | + UseCaseErrorHandler.globalOnErrorLogger = { exception -> |
| 286 | + logException = exception |
| 287 | + } |
| 288 | + |
| 289 | + var resultError: Throwable? = null |
| 290 | + errorUseCase.execute(IllegalStateException()) { |
| 291 | + onError { error -> |
| 292 | + resultError = error |
| 293 | + } |
| 294 | + } |
| 295 | + viewModelScope.advanceTimeByCompat(10000) |
| 296 | + |
| 297 | + assertTrue(resultError is IllegalStateException) |
| 298 | + assertTrue(logException is IllegalStateException) |
| 299 | + } |
| 300 | + |
| 301 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 302 | + private fun TestScope.advanceTimeByCompat(delayTimeMillis: Long) { |
| 303 | + this.testScheduler.apply { advanceTimeBy(delayTimeMillis); runCurrent() } |
| 304 | + } |
| 305 | +} |
0 commit comments