FakeBudgetStore

class FakeBudgetStore(initial: BudgetSnapshot = BudgetSnapshot.Empty) : BudgetStore(source)

A scripted BudgetStore for tests, the worked example of the seam. Holds one snapshot (snapshot, preloaded through the constructor), counts calls, records every successful save, injects failures and delays.

Stable Core, source compatibility only. Thread-safe: one lock guards the state and is never held across a suspension. A delay is kotlinx.coroutines.delay in the caller's context, so it is virtual when the budget runs on the test scheduler, and a call the budget abandons at storeTimeout is cancelled in its delay and changes nothing (the store is cancellable, as the port requires).

Parameters

initial

what the first load returns; BudgetSnapshot.Empty by default.

Constructors

Link copied to clipboard
constructor(initial: BudgetSnapshot = BudgetSnapshot.Empty)

Properties

Link copied to clipboard

How many times load was called, whatever it returned.

Link copied to clipboard

How long load takes before it answers (virtual under a test scheduler). Zero by default.

Link copied to clipboard

How many times save was called, whatever it returned.

Link copied to clipboard

How long save takes before it answers. Zero by default.

Link copied to clipboard

Every snapshot saved successfully, in order.

Link copied to clipboard

The snapshot the next load returns: the preloaded one, or the last one saved.

Functions

Link copied to clipboard
fun failLoads(cause: Throwable, thrown: Boolean = false, times: Int = Int.MAX_VALUE)

Makes the next times loads fail with cause: returned inside Result.failure, or thrown when thrown is true (a store that leaks TimeoutCancellationException throws it).

Link copied to clipboard
fun failSaves(cause: Throwable, thrown: Boolean = false, times: Int = Int.MAX_VALUE)

As failLoads, for save. A failed save stores nothing.

Link copied to clipboard
fun heal()

Stops failing loads and saves.

Link copied to clipboard
open suspend override fun load(): Result<BudgetSnapshot>

Counts the call in loadCount, waits loadDelay (virtual under a test scheduler), then returns snapshot, or delivers the failure armed by failLoads: returned in the Result, or thrown when it was armed with thrown = true.

Link copied to clipboard
open suspend override fun save(snapshot: BudgetSnapshot): Result<Unit>

Counts the call in saveAttemptCount, waits saveDelay (virtual under a test scheduler), then stores snapshot and records it in saves, or delivers the failure armed by failSaves and stores nothing: returned in the Result, or thrown when it was armed with thrown = true. A save cancelled during the delay stores nothing.

Link copied to clipboard
open override fun toString(): String

A diagnostic string for logs: loadCount and the number of saves. Its format is not part of the API and may change.