FakeRaftNode

class FakeRaftNode(val selfId: NodeId = NodeId("self"), initialRole: RaftRole = RaftRole.Follower, initialLeader: NodeId? = null, initialCommitIndex: Long = 0, val clientId: ClientId = ClientId.auto(selfId, Random(selfId.value.hashCode()))) : RaftNode

A test double for RaftNode with driver helpers for driving role transitions, injecting committed entries, emitting trace events, and inspecting outgoing proposals.

Defaults make FakeRaftNode() ready to use in one line:

val node = FakeRaftNode()
node.setRole(RaftRole.Leader)
val entry = node.propose("set x=1".encodeToByteArray())
val committed = node.committed.first() as Committed.Entry
// committed.entry.command == "set x=1".encodeToByteArray()

Stream semantics — a deliberate divergence from the real RaftNode. The real RaftNode documents committed and trace as hot, no-replay flows (late collectors miss history). For test ergonomics this double backs them with unbounded-buffering channels instead, so pushCommitted(...) followed by committed.first() works without racing a collector. Two consequences a consumer should not encode as RaftNode guarantees:

  • entries/events emitted before collection are buffered and replayed here, whereas the real RaftNode would drop them;

  • close completes committed/trace (channel close), whereas the real engine cancels its backing scope without completing the flows.

Constructors

Link copied to clipboard
constructor(selfId: NodeId = NodeId("self"), initialRole: RaftRole = RaftRole.Follower, initialLeader: NodeId? = null, initialCommitIndex: Long = 0, clientId: ClientId = ClientId.auto(selfId, Random(selfId.value.hashCode())))

Properties

Link copied to clipboard

This node's Raft §8 dedup identity, stamped onto the LogEntry returned by propose. Defaults to a deterministic auto id derived from selfId (distinct per node id, stable per instance); pass a fixed ClientId to assert exact dedup keys.

Link copied to clipboard

Whether close has been called.

Link copied to clipboard
open override val commitIndex: StateFlow<Long>
Link copied to clipboard
open override val committed: Flow<Committed>
Link copied to clipboard
open override val compactionFloor: StateFlow<Long>
Link copied to clipboard
open override val leader: StateFlow<NodeId?>
Link copied to clipboard

All commands passed to propose, in call order.

Link copied to clipboard

Behavior of propose. Defaults to contract-faithful: throws NotLeaderException unless role is RaftRole.Leader, otherwise appends the command to committed and returns the resulting LogEntry.

Link copied to clipboard
open override val role: StateFlow<RaftRole>
Link copied to clipboard

Stable identifier for this node — exposed as a convenience field, not on RaftNode.

Link copied to clipboard
open override val snapshots: MutableStateFlow<Snapshot?>

Snapshot publish channel — present to satisfy RaftNode. This double runs no real compaction loop, so setting it has no functional effect; drive setCompactionFloor to simulate a floor and pushInstall to inject a Committed.Install on the committed stream.

Link copied to clipboard
open override val trace: Flow<RaftTraceEvent>

Functions

Link copied to clipboard
open suspend fun awaitLeadership()
Link copied to clipboard
open fun cancelTransfer()
Link copied to clipboard
open suspend fun changeMembership(target: ClusterConfig): ClusterConfig
Link copied to clipboard
open suspend override fun close()
Link copied to clipboard
open override fun committedFrom(fromIndex: Long): Flow<Committed>
Link copied to clipboard
suspend fun emitTrace(event: RaftTraceEvent)

Push event onto trace.

Link copied to clipboard
open suspend override fun propose(command: ByteArray): LogEntry
open suspend override fun propose(command: ByteArray, requestId: Long): LogEntry
Link copied to clipboard
suspend fun pushCommitted(command: ByteArray): LogEntry

Convenience: create a LogEntry at the next auto-incremented index (term=1) with command, push it onto committed, and return it.

suspend fun pushCommitted(entry: LogEntry): LogEntry

Push entry onto committed and advance commitIndex to entry's index.

Link copied to clipboard
suspend fun pushInstall(snapshot: Snapshot)

Push a Committed.Install onto committed, advancing compactionFloor and commitIndex to snapshot's throughIndex — the test-double analogue of the engine accepting an InstallSnapshot.

Link copied to clipboard
open suspend fun readIndex(): Long
Link copied to clipboard
fun setCommitIndex(index: Long)

Set commitIndex to index. Does not push any entry onto committed.

Link copied to clipboard

Set compactionFloor to index without emitting a Committed.Install.

Link copied to clipboard
fun setLeader(newLeader: NodeId?)

Update leader to newLeader.

Link copied to clipboard
fun setRole(newRole: RaftRole)

Transition role to newRole.

Link copied to clipboard
open suspend fun transferLeadership(target: NodeId)