synctest

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package synctest is sync's seeded simulation (ADR-0020, docs/design/sync.md#testing): a hub and devices, each a whole notesd from rpctest over its own data directory and vault, syncing over loopback through links the simulation cuts and heals, while a seeded plan of steps edits, creates, moves, deletes, crashes and restarts. At each quiet point it checks that every vault equals the hub's heads, that no inserted token was lost, that no conflict copy exists, that the log holds its invariants, and that every device agrees with the hub.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Files

func Files(n *Node) map[string]string

Files returns node n's vault files on disk, hidden ones aside, by path.

func PlanText

func PlanText(plan []Step) string

PlanText renders a plan as testdata/regressions lines read it back.

func Run

func Run(t testing.TB, o Options, plan []Step) (err error)

Run makes the world, runs plan's steps in it and returns the first violated check, or nil.

Types

type Node

type Node struct {
	Name string
	Dir  string // the vault's folder

	Srv   *rpctest.Server
	C     *api.Client
	Vault int64
	// contains filtered or unexported fields
}

Node is one notesd of the world, with one vault.

type Op

type Op int

Op is what a step does.

const (
	Edit       Op = iota // an editor's write with the current base
	StaleEdit            // an editor's write with an older base: a local merge
	Append               // an agent's append
	Outside              // another program's edit on disk
	Create               // a new note
	Daily                // today's note, a path two devices may create offline
	Delete               // a delete with the current base
	Move                 // a move through notesd
	DiskMove             // a rename on disk
	Attach               // an attachment that is not text, written on disk
	Offline              // the device's link cut
	Online               // the device's link healed
	Crash                // a crash at a point, then a restart
	HubRestart           // the hub stopped and started again
	HubCrash             // the hub crashing after a push's rename
	DropAnswer           // the hub losing a push's answer
	Reset                // sync.reset
	// The ops below plan only in a Profile that weighs them, so the default
	// plans stay what they were.
	Swap          // two notes swap paths through notesd
	CaseRename    // a note's name changes only in case
	BigAttach     // an attachment over 10 MB, written on disk
	RevokeAndPair // the hub revokes the device, which pairs again
	Snapshot      // the device's vault copied, for a later Rejoin
	Rejoin        // the device replaced by a new one joining with its copy

)

The steps a plan is made of (ADR-0020).

func (Op) String

func (o Op) String() string

type Options

type Options struct {
	Seed    uint64
	Devices int // [3]
	Steps   int
	// Concurrent lets steps overlap sync; stepwise (the default) lets the
	// world go quiet after every step, so a seed replays exactly.
	Concurrent bool
	// CheckEvery is how many steps pass between full checks [25 stepwise,
	// 100 concurrent]; a run always checks at its end.
	CheckEvery int
	Log        *slog.Logger // [discard]
	// Watch, when set, logs each node's copy of the file at that path
	// after every step: a debugging aid.
	Watch string
	// Profile, when set, adds a scenario's checks to every quiet point;
	// its weights are PlanWith's to plan with.
	Profile *Profile
}

Options configure a run.

type Profile

type Profile struct {
	// Weights are how often each op is planned, against the others; an op
	// without a weight is never planned. Nil plans the default weights.
	Weights map[Op]int
	// Check runs at each quiet point, after the world's own checks.
	Check func(w *World) error
}

Profile shapes a seeded run for a scenario (plan 0004, Phase 6): which ops it plans and how often, and checks of its own at each quiet point.

type Step

type Step struct {
	Op     Op
	Device int
	A, B   uint64
}

Step is one planned step. A and B are choices, resolved against the world as the step runs (the k-th file by path, a line), so a plan with steps dropped still runs: what the reducer needs.

func ParsePlan

func ParsePlan(text string) ([]Step, error)

ParsePlan reads a plan PlanText wrote; lines starting with # are comments.

func Plan

func Plan(seed uint64, devices, steps int) []Step

Plan makes the steps of a seed.

func PlanWith

func PlanWith(seed uint64, devices, steps int, weights map[Op]int) []Step

PlanWith makes the steps of a seed with weights, as Plan does with the default ones.

func Reduce

func Reduce(t testing.TB, o Options, plan []Step, budget int) ([]Step, error)

Reduce shrinks a failing plan by dropping steps while it still fails (ADR-0020): halves, then quarters, down to single steps, stepwise, at most budget runs. It returns the smallest failing plan it found and its failure.

func (Step) Line

func (s Step) Line() string

Line is a step as one line of a regression file.

func (Step) String

func (s Step) String() string

type World

type World struct {
	Hub     *Node
	Devices []*Node
	// contains filtered or unexported fields
}

World is a hub and devices syncing one vault.

func NewWorld

func NewWorld(t testing.TB, o Options) *World

NewWorld starts a hub serving the seed files and o.Devices devices that have joined it.

func (*World) AddDevice

func (w *World) AddDevice(name string, vault map[string]string) (*Node, error)

AddDevice starts a device named name whose vault holds vault, pairs it with the hub and joins the hub's vault, as a user does with a copy of their notes. Tokens in vault the oracle has not seen are written by it.

func (*World) Check

func (w *World) Check() error

Check lets the world go quiet and checks it (ADR-0020): every device's files are the hub's heads; every token no writer removed is in a head or a conflict revision; no file is a conflict copy; the log has no gaps, every base is earlier, and no two live files share a path ignoring case; every device agrees with the hub.

func (*World) CheckIndexes

func (w *World) CheckIndexes() error

CheckIndexes checks that every node's index equals a fresh rebuild of its vault: the last check of a run, since a rebuild costs a notesd.

func (*World) Close

func (w *World) Close()

Close stops every notesd of the world.

func (*World) Delete

func (w *World) Delete(d *Node, p string) error

Delete deletes the file at p on device d through notesd, with the base it reads; the tokens it held are removed on purpose.

func (*World) Heads

func (w *World) Heads() map[string]string

Heads returns the hub's files, by path.

func (*World) History

func (w *World) History(p string) ([]api.Revision, error)

History returns the hub's revisions of the live file at p, newest first.

func (*World) HubFile

func (w *World) HubFile(p string) (api.LogFile, bool)

HubFile returns the hub's log entry for the live file at p.

func (*World) Move

func (w *World) Move(d *Node, from, to string) error

Move moves the file at from to to on device d through notesd, updating the links that name it.

func (*World) MoveDisk

func (w *World) MoveDisk(d *Node, from, to string) error

MoveDisk renames from to to on device d's disk, as a file manager would, and has notesd index both paths.

func (*World) Offline

func (w *World) Offline(d *Node)

Offline cuts device d's link to the hub.

func (*World) Online

func (w *World) Online(d *Node)

Online heals device d's link and wakes its loop.

func (*World) PairAgain

func (w *World) PairAgain(d *Node) error

PairAgain pairs device d with the hub again, as its user does after a revocation; its vault then syncs again.

func (*World) RemoveDevice

func (w *World) RemoveDevice(d *Node)

RemoveDevice stops device d and takes it out of the world. Settle first: what d has not pushed goes with it.

func (*World) Reset

func (w *World) Reset(d *Node) error

Reset drops device d's sync state and joins again (sync.reset).

func (*World) RestartHub

func (w *World) RestartHub()

RestartHub stops the hub's notesd and starts it again on its data.

func (*World) Revoke

func (w *World) Revoke(d *Node) error

Revoke revokes device d on the hub and waits until d's sync says so.

func (*World) Settle

func (w *World) Settle() error

Settle heals every link and waits for a quiet point: no device has anything to push, and each has pulled all the hub's log holds.

func (*World) SettleOnline

func (w *World) SettleOnline() error

SettleOnline waits for a quiet point of the devices whose links are up, leaving cut links cut.

func (*World) Snapshot

func (w *World) Snapshot(d *Node) map[string]string

Snapshot returns a copy of device d's vault files, for AddDevice to join with later: an older copy of the vault.

func (*World) Status

func (w *World) Status(d *Node) (api.SyncStatus, error)

Status returns device d's sync status.

func (*World) Token

func (w *World) Token() string

Token returns a new token for content a script writes. It counts as written once a write that holds it succeeds.

func (*World) Write

func (w *World) Write(d *Node, p, content string) error

Write makes the note at p on device d hold content, through notesd: a write with the base it reads first, or a create when there is no note at p.

func (*World) WriteDisk

func (w *World) WriteDisk(d *Node, p string, content []byte) error

WriteDisk writes content at p on device d's disk, as another program would, and has notesd index it.

func (*World) WriteFrom

func (w *World) WriteFrom(d *Node, p, base, content string) error

WriteFrom writes content to the note at p on device d with an older base, the hash of a version it read before: notesd merges it into what the note holds now. Only the tokens content gains are written; a merge that conflicts keeps the content as a conflict version.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL