feat(sync): добавил cooperative-отмену sync и pipeline-задач

ensureActive в StorageSyncEngine, flush журнала перед чтением, Cancelled
в StorageSyncRunOutcome и TaskContext.ensureNotCancelled.
This commit is contained in:
2026-05-22 13:22:15 +03:00
parent bc2b354820
commit 2618df41e3
8 changed files with 80 additions and 18 deletions

View File

@@ -22,6 +22,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlin.coroutines.coroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
@@ -238,6 +240,10 @@ class TaskOrchestrator(
override fun fail(error: WallencException): Nothing {
throw TaskFailedException(error)
}
override suspend fun ensureNotCancelled() {
coroutineContext.ensureActive()
}
}
private class TaskFailedException(val error: WallencException) : RuntimeException()

View File

@@ -32,6 +32,24 @@ class TaskOrchestratorTest {
assertTrue(task.state is TaskRunState.Completed)
}
@Test
fun cancelAllMarksRunningTaskCancelled() = runTest(dispatcher) {
val orchestrator = TaskOrchestrator(dispatcher)
val id = orchestrator.enqueue(
title = "Long",
dispatcher = dispatcher,
work = { ctx ->
ctx.reportProgress(null, null)
kotlinx.coroutines.delay(60_000)
},
)
advanceTimeBy(1)
orchestrator.cancelAll()
advanceUntilIdle()
val task = orchestrator.pipelineState.value.tasks.first { it.id == id }
assertTrue(task.state is TaskRunState.Cancelled)
}
@Test
fun cancelMarksTaskCancelled() = runTest(dispatcher) {
val orchestrator = TaskOrchestrator(dispatcher)