mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor(concurrent): Introduce SequentialJob to manage service setup (#3983)
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
parent
6d827e9004
commit
4aea88877a
5 changed files with 141 additions and 12 deletions
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright (c) 2025 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.geeksville.mesh.concurrent
|
||||
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.test.advanceTimeBy
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
class SequentialJobTest {
|
||||
|
||||
private val sequentialJob = SequentialJob()
|
||||
|
||||
@Test
|
||||
fun `launch cancels previous job`() = runTest {
|
||||
var job1Active = false
|
||||
var job1Cancelled = false
|
||||
|
||||
// Launch first job
|
||||
sequentialJob.launch(this) {
|
||||
try {
|
||||
job1Active = true
|
||||
delay(1000)
|
||||
} finally {
|
||||
job1Cancelled = true
|
||||
}
|
||||
}
|
||||
|
||||
advanceTimeBy(100)
|
||||
assertTrue("Job 1 should be active", job1Active)
|
||||
|
||||
// Launch second job
|
||||
sequentialJob.launch(this) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
advanceTimeBy(100)
|
||||
assertTrue("Job 1 should be cancelled", job1Cancelled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `cancel stops the job`() = runTest {
|
||||
var jobActive = false
|
||||
var jobCancelled = false
|
||||
|
||||
sequentialJob.launch(this) {
|
||||
try {
|
||||
jobActive = true
|
||||
delay(1000)
|
||||
} finally {
|
||||
jobCancelled = true
|
||||
}
|
||||
}
|
||||
|
||||
advanceTimeBy(100)
|
||||
assertTrue("Job should be active", jobActive)
|
||||
|
||||
sequentialJob.cancel()
|
||||
|
||||
advanceTimeBy(100)
|
||||
assertTrue("Job should be cancelled", jobCancelled)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue