|
| 1 | +package org.ole.planet.myplanet.services |
| 2 | + |
| 3 | +import io.mockk.every |
| 4 | +import io.mockk.mockk |
| 5 | +import io.mockk.unmockkAll |
| 6 | +import kotlinx.coroutines.CoroutineDispatcher |
| 7 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 8 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 9 | +import kotlinx.coroutines.test.TestScope |
| 10 | +import kotlinx.coroutines.test.advanceUntilIdle |
| 11 | +import kotlinx.coroutines.test.runTest |
| 12 | +import org.junit.After |
| 13 | +import org.junit.Assert.assertEquals |
| 14 | +import org.junit.Assert.assertTrue |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import org.ole.planet.myplanet.utils.DispatcherProvider |
| 18 | +import kotlin.coroutines.ContinuationInterceptor |
| 19 | + |
| 20 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 21 | +class SubmissionUploadExecutorTest { |
| 22 | + private val testDispatcher = StandardTestDispatcher() |
| 23 | + private val testScope = TestScope(testDispatcher) |
| 24 | + private val ioDispatcher = StandardTestDispatcher() |
| 25 | + private lateinit var mockDispatcherProvider: DispatcherProvider |
| 26 | + private lateinit var executor: SubmissionUploadExecutor |
| 27 | + |
| 28 | + @Before |
| 29 | + fun setUp() { |
| 30 | + mockDispatcherProvider = mockk() |
| 31 | + every { mockDispatcherProvider.io } returns ioDispatcher |
| 32 | + executor = SubmissionUploadExecutor(testScope, mockDispatcherProvider) |
| 33 | + } |
| 34 | + |
| 35 | + @After |
| 36 | + fun tearDown() { |
| 37 | + unmockkAll() |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun testExecute_invokesBlockOnCorrectDispatcher() = testScope.runTest { |
| 42 | + var invoked = false |
| 43 | + var capturedDispatcher: CoroutineDispatcher? = null |
| 44 | + |
| 45 | + executor.execute { |
| 46 | + invoked = true |
| 47 | + capturedDispatcher = coroutineContext[ContinuationInterceptor] as CoroutineDispatcher |
| 48 | + } |
| 49 | + |
| 50 | + ioDispatcher.scheduler.advanceUntilIdle() |
| 51 | + |
| 52 | + assertTrue(invoked) |
| 53 | + assertEquals(ioDispatcher, capturedDispatcher) |
| 54 | + } |
| 55 | +} |
0 commit comments