FakeKmpWorker
Test double for KmpWorker that executes tasks immediately in-process.
No WorkManager or BGTaskScheduler involved — runs handlers synchronously in the calling coroutine context.
Features:
Immediate execution (no background scheduling delay)
Tracks all enqueued and cancelled task IDs
Supports simulating failures via failureCount
Supports retry scenario simulation
Fully observable via observe and observeAll
Supports both
register()andregisterWithContext()Group cancellation via
cancelByTag()
Usage:
val fakeWorker = FakeKmpWorker()
fakeWorker.register("sync") { repository.sync() }
fakeWorker.enqueue(TaskRequest(id = "sync", type = TaskType.OneTime))
assertEquals(TaskState.Success, fakeWorker.lastStateFor("sync"))Properties
All tags that have been cancelled via cancelByTag.
All task IDs that have been cancelled.
All task requests that have been enqueued.
Map of task ID → number of times it should fail before succeeding. Use to simulate retry scenarios.
Functions
Returns all emitted states for a given task ID in order.
Executes a TaskChain step-by-step in-process (no background scheduling). Ideal for unit tests — each step runs immediately and synchronously. Chain-level Success/Failed states are emitted to both states and TaskMonitor.
Returns how many times the given task was executed.
Returns the last emitted state for a given task ID.
Returns true if the task was cancelled.
Returns true if the task was enqueued at least once.
Returns true if a tag was cancelled.