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:
James Rich 2025-12-12 13:02:17 -06:00 committed by GitHub
parent 6d827e9004
commit 4aea88877a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 141 additions and 12 deletions

View file

@ -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)
}
}