segmenttest

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 17 Imported by: ? 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 segmenttest is the conformance suite of segment.Provider: the contract of the seam as executable cases. A provider conforms when Run passes on it.

An implementation gives Run a Factory. The factory returns a provider that holds the members of the Data the suite asks for, together with a Backend, the handle through which the suite makes the store fail or hang. A fault that a backend cannot arrange (an in-memory provider has no store to fail) is declared in Fixture.Unsupported with its reason: the cases that need it are then reported as unsupported, never skipped silently. A provider without a backend is wrapped in a decorator of the suite that injects the faults: the cases that need one then check what the provider answers when healthy and the expectations of the suite, not the error paths of the provider itself (a documented limit). Check returns the Report of what ran, what failed and what is unsupported; Run logs it and fails the test for each failed case.

Index

Constants

View Source
const (
	CasePositions        = "positions-are-preserved"
	CaseUnknownSegment   = "missing-segment-answers-its-policy"
	CaseInputUntouched   = "segment-and-id-are-not-changed"
	CaseDuplicates       = "duplicates-in-one-call-get-the-same-answer"
	CaseEmptyCall        = "an-empty-call-is-not-an-error"
	CaseLargeBatch       = "a-large-batch-is-answered-in-place"
	CaseOddIDs           = "odd-ids-are-never-members"
	CasePartialFailure   = "partial-failure-leaves-the-failed-unknown"
	CaseFailure          = "a-failed-call-answers-nothing-wrongly"
	CaseRecovers         = "a-provider-recovers-after-a-failure"
	CaseCtxEndedBefore   = "a-context-that-ended-before-is-honoured"
	CaseCtxCancelDuring  = "a-cancel-during-the-call-is-honoured"
	CaseSliceNotRetained = "the-slice-is-not-retained"
	CaseConcurrent       = "concurrent-calls-are-independent"
	CasePrefilled        = "prefilled-states-are-reset"
	CaseAfterShutdown    = "a-call-after-shutdown-answers-nothing"
	CaseLongID           = "an-over-long-id-is-never-a-member"
	CaseAtomicExistence  = "a-segment-loaded-during-a-lookup-is-seen-whole"
)

The names of the cases.

View Source
const (
	CaseSnapEmptyVsMissing  = "empty-is-a-set-and-missing-is-unknown"
	CaseSnapStreamsMembers  = "members-are-streamed-exactly"
	CaseSnapVersionMoves    = "version-is-stable-and-moves-with-a-write"
	CaseSnapCtxEnded        = "an-ended-context-fails-both-calls"
	CaseSnapNoRepull        = "an-unchanged-version-is-not-pulled-again"
	CaseSnapLastKnownGood   = "a-failed-pull-keeps-the-last-known-good"
	CaseSnapVersionFails    = "a-failed-version-keeps-the-last-known-good"
	CaseSnapCancelMidStream = "a-cancel-during-the-stream-keeps-the-last-known-good"
	CaseSnapMemoryGuard     = "the-memory-guard-refuses-and-keeps-the-last-known-good"
	CaseSnapGone            = "a-segment-gone-from-the-store-is-unknown-not-last-known-good"
	CaseSnapNeverLoaded     = "a-holder-that-never-loaded-answers-unknown"
	CaseSnapLargeStreamed   = "a-large-set-is-built-without-a-second-copy"
)

The names of the cases of RunSnapshotSource.

View Source
const (
	CaseVerStable      = "an-unchanged-segment-has-a-stable-version"
	CaseVerMoves       = "a-write-moves-the-version"
	CaseVerPerSegment  = "versions-are-per-segment"
	CaseVerAbsent      = "an-absent-segment-has-a-stable-token-or-is-missing"
	CaseVerCtxEnded    = "an-ended-context-fails"
	CaseVerConcurrency = "concurrent-reads-agree"
)

The names of the cases of RunVersioner.

Variables

This section is empty.

Functions

func AwaitClusterReady

func AwaitClusterReady(tb testing.TB, ports []string, run func(cmd []string) (string, error))

AwaitClusterReady waits until every node of a cluster that was just created agrees that it is usable: CLUSTER INFO says cluster_state:ok, all 16384 slots assigned, and as many known nodes as there are ports. A command that is sent before that, to any node, can be answered CLUSTERDOWN. run executes a command in the container of the nodes and returns its output. The failure message holds the last CLUSTER INFO of each node.

func CaseNames

func CaseNames() []string

CaseNames lists the cases of the suite.

func CheckSnapshotSource

func CheckSnapshotSource(factory SnapshotFactory) map[string][]string

CheckSnapshotSource runs the same cases without a testing.T and returns the failures of each case that failed, by case name. It is how the suite is tested against deliberately broken sources.

func CheckVersioner

func CheckVersioner(factory VersionerFactory) map[string][]string

CheckVersioner runs the same cases without a testing.T and returns the failures of each case that failed, by case name. It is how the suite is tested against deliberately broken versioners.

func RunSnapshotSource

func RunSnapshotSource(t *testing.T, factory SnapshotFactory)

RunSnapshotSource runs the conformance suite of segment.SnapshotSource against the implementation that the factory builds, once per case, and drives a snapshot.Holder over it for the cases of the holder contract (exact call counts, last known good, cancellation, the memory guard, one copy of a large set). Call it from a test that is not parallel: one case measures allocations of the whole process.

func RunVersioner

func RunVersioner(t *testing.T, factory VersionerFactory)

RunVersioner runs the conformance suite of segment.Versioner against the implementation that the factory builds, once per case. Tokens are opaque: the suite only compares them for equality.

func SnapshotSegments

func SnapshotSegments() []string

SnapshotSegments returns the names of the segments that the suite uses, for a factory whose store must be told its segments before it starts.

Types

type Backend

type Backend interface {
	// Fail makes every following call fail with an error, with no lookup answered, until Heal.
	Fail()
	// FailSegment makes the lookups of the segment unanswerable, and the call fail, until Heal.
	FailSegment(name string)
	// Hang makes the following calls wait until their context ends or Release. The channel gets one value for
	// every call that started to wait.
	Hang() <-chan struct{}
	// Release lets the calls that hang go on, and stops hanging.
	Release()
	// Heal undoes Fail and FailSegment.
	Heal()
}

Backend is the handle through which the suite makes the store behind a provider misbehave. Every method that a fixture waives is never called.

type Capability

type Capability string

Capability is something a backend may not be able to arrange.

const (
	// CapFail is [Backend.Fail]: every call fails with an error until Heal. It cannot be waived.
	CapFail Capability = "fail"
	// CapPartial is [Backend.FailSegment]: the lookups of one segment cannot be answered.
	CapPartial Capability = "partial-failure"
	// CapHang is [Backend.Hang]: a call does not answer until its context ends or Release.
	CapHang Capability = "hang"
)

type Data

type Data map[string][]string

Data is the members of the segments the provider must hold.

type Factory

type Factory func(data Data) (Fixture, error)

Factory builds a fixture for a case. It is called once for each case, with the data the suite needs.

type Fixture

type Fixture struct {
	// Provider is the provider under test, holding the members of the Data.
	Provider segment.Provider
	// Backend arranges faults in the store behind the provider. A provider that has no store to fail (an in-memory
	// one) leaves it nil: the suite then wraps the provider in a decorator of its own that fails and hangs on
	// request, so the cases that need a fault still run. That is a limit: the fault comes from the decorator, so
	// those cases check the answers of the provider when it is healthy and the expectations of the suite, not the
	// error paths of the provider itself.
	Backend Backend
	// Unsupported lists the faults that the backend cannot arrange, with a reason for each: only CapPartial and
	// CapHang can be waived, and only with a Backend. The run must declare the same waivers (see [Run]).
	Unsupported Waivers
	// Racer (optional) arranges that a segment is created while a lookup runs. The case of
	// [segment.AtomicExistencer] runs only for a fixture that has one, whose Missing is segment.MissingUnknown and
	// whose provider reports [segment.HasAtomicExistence]; a provider that does not report it keeps the non-atomic
	// behaviour that it documents, and the case is skipped for it.
	Racer Racer
	// Close (optional) releases the provider after the case.
	Close func()
	// Missing is what the provider answers for a segment that does not exist: the default (empty) is
	// segment.MissingNotMember, and segment.MissingUnknown is the explicit opt-in.
	Missing segment.MissingKey
	// Bound is how long a case waits for a call that is known to fail by never answering. Zero means ten seconds.
	Bound time.Duration
}

Fixture is a provider under test and the handle behind it.

type Racer

type Racer interface {
	// LoadDuringLookup unloads the segment seg now, and arranges that its members appear (the segment is loaded with
	// exactly those members) while the next lookup of seg runs: after the store has received the first command of
	// the lookup that names the segment, and before the last one runs. A store with transactions makes the set
	// visible to the whole transaction, as it would be if the set had been created just before it. The members
	// appear once.
	LoadDuringLookup(seg string, members []string)
}

Racer is the handle through which the suite makes a segment appear while a lookup is in progress. A fixture gives one when its store can do that; the case of segment.AtomicExistencer needs it.

type Report

type Report struct {
	Failed      map[string][]string
	Unsupported map[string]string
	// Invalid lists the problems of the waivers: an unknown capability, an empty reason, a waiver that cannot be
	// given, or a fixture whose waivers differ from the ones the run declares. No case runs while there is one.
	Invalid []string
	Ran     []string
}

Report is what a run did.

func Check

func Check(factory Factory, declared Waivers) Report

Check runs every case against the providers of the factory and returns the report. It never fails a test. declared is the set of waivers that the caller states for the factory: it must be the one of every fixture.

func Run

func Run(t *testing.T, factory Factory, waivers Waivers) Report

Run runs every case as a subtest of "cases", in parallel, and fails the ones that failed: a filter of `go test -run` selects the cases that run, and only those are built and run. waivers is the set of faults that the fixtures cannot arrange: it is compared with the Unsupported of every fixture, and a difference fails the run. It logs the cases that a waiver skipped, and returns what the cases did.

type Seed

type Seed func(name string, ids []string) error

Seed creates or replaces the members of a segment in the store behind a source, and must change the version that the source reports for it. It is how the suites write to a store.

type SnapshotFactory

type SnapshotFactory func() (SnapshotFixture, error)

SnapshotFactory builds a fixture of an empty store for a case.

type SnapshotFixture

type SnapshotFixture struct {
	// Source is the implementation under test.
	Source segment.SnapshotSource
	// Seed writes members to the store.
	Seed Seed
	// Close (optional) releases the store after the case.
	Close func()
	// Remove (optional) deletes a segment from the store and moves its version.
	Remove func(name string) error
	// Missing is what a segment that does not exist means for the holders of the suite: the default (empty) is
	// segment.MissingNotMember, segment.MissingUnknown is the opt-in.
	Missing segment.MissingKey
	// NoEmptySet is true for a store that cannot hold an empty segment (a Redis set does not exist without a
	// member): seeding no member then removes the segment, and the suite expects it to be missing.
	NoEmptySet bool
}

SnapshotFixture is a store under test: the source, and the way to write to the store behind it.

type VersionerFactory

type VersionerFactory func() (VersionerFixture, error)

VersionerFactory builds a fixture of an empty store for a case.

type VersionerFixture

type VersionerFixture struct {
	// Versioner is the implementation under test.
	Versioner segment.Versioner
	// Seed writes members to the store and must change the version of the segment.
	Seed Seed
	// Close (optional) releases the store after the case.
	Close func()
}

VersionerFixture is a store under test: the versioner, and the way to write to the store behind it.

type Waivers

type Waivers map[Capability]string

Waivers lists the capabilities that a fixture cannot arrange, each with the reason.

func NewWaivers

func NewWaivers() Waivers

NewWaivers returns an empty set of waivers.

func (Waivers) With

func (w Waivers) With(capability Capability, reason string) Waivers

With adds a waiver and returns the set.

Jump to

Keyboard shortcuts

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