FakeWorkManager class

In-memory IWorkManager test double.

Records every call and lets you inject TaskEvents / TaskProgress updates via emitEvent and emitProgress.

Important lifecycle rules:

  • Always call dispose in tearDown to prevent resource leaks and to restore TaskChainBuilder.enqueueCallback to its original value.
  • Calling reset recreates the stream controllers; existing stream subscriptions are cancelled. Re-subscribe after reset if needed.

Example

group('SyncService', () {
  late FakeWorkManager wm;
  late SyncService service;

  setUp(() {
    wm = FakeWorkManager();
    service = SyncService(wm);
  });

  tearDown(wm.dispose); // ← always required

  test('schedules one task on start', () async {
    await service.start();
    expect(wm.enqueued, hasLength(1));
    expect(wm.enqueued.first.taskId, 'periodic-sync');
  });

  test('reacts to task failure', () async {
    await service.start();
    wm.emitEvent(TaskEvent(
      taskId: 'periodic-sync',
      success: false,
      message: 'Network error',
      timestamp: DateTime.now(),
    ));
    expect(service.lastError, 'Network error');
  });

  test('cancels on stop', () async {
    await service.start();
    await service.stop();
    expect(wm.cancelAllCalled, isTrue);
  });

  test('full chain structure is visible', () async {
    await service.startWithChain();
    expect(wm.chains, hasLength(1));
    expect(wm.chains.first.allTasks.map((t) => t.id), ['step1', 'step2', 'step3']);
  });
});
Implemented types

Constructors

FakeWorkManager()
Creates a new FakeWorkManager.

Properties

allTagsResult List<String>
Return value for getAllTags.
getter/setter pair
allTasksResult List<TaskRecord>
Return value for allTasks.
getter/setter pair
cancelAllCalled bool
Whether cancelAll was called.
getter/setter pair
cancelled List<String>
All cancel task IDs in order.
final
cancelledTags List<String>
All cancelByTag values in order.
final
chains List<FakeChainRecord>
All recorded chains from beginWith, with their full step structures.
final
enqueued List<EnqueueCall>
All enqueue / enqueueAll calls in order.
final
enqueueResult ScheduleResult
Default return value for enqueue / enqueueAll.
getter/setter pair
enqueueResultByTaskId Map<String, ScheduleResult>
Per-task result overrides. Falls back to enqueueResult if not set.
final
events Stream<TaskEvent>
Task completion and lifecycle events.
no setteroverride
hashCode int
The hash code for this object.
no setterinherited
paused List<String>
All pause task IDs in order.
final
progress Stream<TaskProgress>
Task progress updates (downloads, uploads, chains).
no setteroverride
resumed List<String>
All resume task IDs in order.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
tasksByTag Map<String, List<String>>
Stub tasks-by-tag. getTasksByTag returns [] for unknown tags.
final
taskStatuses Map<String, TaskStatus>
Stub task statuses. getTaskStatus returns null for unknown IDs.
final

Methods

allTasks() Future<List<TaskRecord>>
Returns all task records from the persistent store.
override
beginWith(TaskRequest task) TaskChainBuilder
Records the chain start and intercepts TaskChainBuilder.enqueue so the full chain structure (all steps from all .then() / .thenAll() calls) is captured in chains.
override
cancel({required String taskId}) Future<void>
Cancel a specific task by ID.
override
cancelAll() Future<void>
Cancel all pending and running tasks.
override
cancelByTag({required String tag}) Future<void>
Cancel all tasks with the given tag.
override
dispose() → void
Close stream controllers and restore TaskChainBuilder.enqueueCallback.
override
emitEvent(TaskEvent event) → void
Push a TaskEvent into the events stream.
emitProgress(TaskProgress p) → void
Push a TaskProgress into the progress stream.
enqueue({required String taskId, required TaskTrigger trigger, required Worker worker, Constraints constraints = const Constraints(), ExistingTaskPolicy existingPolicy = ExistingTaskPolicy.replace, String? tag}) Future<TaskHandler>
Schedule a single background task.
override
enqueueAll(List<EnqueueRequest> requests) Future<List<TaskHandler>>
Schedule multiple tasks in one call.
override
enqueueGraph(TaskGraph graph) Future<GraphExecution>
Schedule a TaskGraph (directed acyclic graph) of background tasks.
override
getAllTags() Future<List<String>>
Returns all active tags.
override
getRunningProgress() Future<Map<String, TaskProgress>>
Get the current progress of all running tasks.
override
getTaskRecord({required String taskId}) Future<TaskRecord?>
Returns the detailed record of a task, or null if not found.
override
getTasksByTag({required String tag}) Future<List<String>>
Returns all task IDs associated with tag.
override
getTaskStatus({required String taskId}) Future<TaskStatus?>
Returns the current status of a task, or null if not found.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pause({required String taskId}) Future<void>
Pause a running task (best-effort; effective for download workers).
override
reset() → void
Clear all recorded state and recreate stream controllers.
resume({required String taskId}) Future<void>
Resume a previously paused task.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited