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() and registerWithContext()

  • 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"))

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

All tags that have been cancelled via cancelByTag.

Link copied to clipboard

All task IDs that have been cancelled.

Link copied to clipboard

All task requests that have been enqueued.

Link copied to clipboard

Map of task ID → number of times it should fail before succeeding. Use to simulate retry scenarios.

Functions

Link copied to clipboard

Returns all emitted states for a given task ID in order.

Link copied to clipboard
open suspend override fun cancel(taskId: String)
Link copied to clipboard
open suspend fun cancelBatch(taskIds: List<String>)
Link copied to clipboard
open suspend override fun cancelByTag(tag: String)
Link copied to clipboard
open suspend fun clearExecutionHistory()
Link copied to clipboard
open suspend override fun enqueue(request: TaskRequest)
Link copied to clipboard
open suspend fun enqueueBatch(requests: List<TaskRequest>)
Link copied to clipboard
open suspend fun enqueueChain(chain: TaskChain)

open suspend override fun enqueueChain(chain: TaskChain, policy: ChainPolicy)

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.

Link copied to clipboard

Returns how many times the given task was executed.

Link copied to clipboard
open suspend fun getExecutionHistory(limit: Int): List<ExecutionRecord>
Link copied to clipboard

Returns the last emitted state for a given task ID.

Link copied to clipboard
open override fun observe(taskId: String): Flow<TaskState>
Link copied to clipboard
open override fun observeAll(): Flow<Pair<String, TaskState>>
Link copied to clipboard
open override fun observeChain(chainId: String): Flow<TaskState>
Link copied to clipboard
open override fun register(taskId: String, block: suspend () -> Unit)
Link copied to clipboard
open override fun registerWithContext(taskId: String, block: suspend TaskExecutionContext.() -> Unit)
Link copied to clipboard
fun reset()

Resets all state — handlers, enqueued/cancelled lists, execution counts, and state history.

Link copied to clipboard

Returns true if the task was cancelled.

Link copied to clipboard
fun wasEnqueued(taskId: String): Boolean

Returns true if the task was enqueued at least once.

Link copied to clipboard

Returns true if a tag was cancelled.