This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalCoroutinesApi::class) | |
class JobStatusComponentTest { | |
@Test | |
fun `should call refresh function only once`() = runTest { | |
val jobs = mutableListOf<Job>() | |
val component = JobStatusComponent(scope = this) | |
repeat(10){ | |
jobs += launch { component.refresh() } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JobStatusComponent(private val scope: CoroutineScope): Component { | |
var job: Job? = null | |
var refreshesCount: Int = 0 | |
private set | |
override suspend fun refresh() { | |
if (job?.isActive == false){ | |
job = scope.launch { | |
try { | |
delay(1L) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalCoroutinesApi::class) | |
class MutexEnabledComponentTest { | |
@Test | |
fun `should call refresh function only once`() = runTest { | |
val jobs = mutableListOf<Job>() | |
val component = MutexEnabledComponent() | |
repeat(10){ | |
jobs += launch { component.refresh() } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalCoroutinesApi::class) | |
class PlainComponentTest { | |
@Test | |
fun `should call refresh function multiple times`() = runTest { | |
val jobs = mutableListOf<Job>() | |
val component = PlainComponent() | |
repeat(10){ | |
jobs += launch { component.refresh() } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MutexEnabledComponent: Component { | |
private val mutex = Mutex() | |
var refreshesCount: Int = 0 | |
private set | |
override suspend fun refresh() { | |
if (!mutex.isLocked){ | |
mutex.withLock { | |
delay(1L) // Delay to simulate a long running operation | |
refreshesCount++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PlainComponent: Component { | |
var refreshesCount: Int = 0 | |
private set | |
override suspend fun refresh(){ | |
delay(1L) // Delay to simulate a long running operation | |
refreshesCount++ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Component { | |
suspend fun refresh() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FlowViewModelTest { | |
@get:Rule | |
val rule = MainCoroutineRule() | |
private companion object { | |
val CHARACTERS = listOf('A', 'B', 'C') | |
} | |
private val repository = mock<CharacterRepository>().apply { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainCoroutineRule : TestWatcher() { | |
private val testDispatcher = TestCoroutineDispatcher() | |
override fun starting(description: Description?) { | |
Dispatchers.setMain(testDispatcher) | |
} | |
fun setup(setupBody: () -> Unit){ | |
testDispatcher.pauseDispatcher() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun ConverterScreen( | |
navController: NavController, | |
viewModel: ConverterViewModel | |
) { | |
val scaffoldState = rememberScaffoldState() | |
val viewState by viewModel.viewState.collectAsState() | |
val viewEffects by viewModel.viewEffects.collectAsState(initial = null) |
NewerOlder