itertest

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package itertest provides facilities for testing internal iterators.

Index

Constants

This section is empty.

Variables

View Source
var ErrInjected = Error("ErrInjected", errorfs.ErrInjected)

ErrInjected is an error artificially injected for testing.

Functions

func Attach

func Attach(
	iter base.InternalIterator, initialState ProbeState, probes ...Probe,
) base.InternalIterator

Attach takes an iterator, an initial state and a probe, returning an iterator that will invoke the provided Probe on all internal iterator operations.

func Condensed

func Condensed(opts *iterCmdOpts)

Condensed configures RunInternalIterCmd to output condensed results without values, collapsed onto a single line.

func NewParser

func NewParser() *dsl.Parser[Probe]

NewParser constructs a Probe parser.

func RunInternalIterCmd

func RunInternalIterCmd(
	t *testing.T, d *datadriven.TestData, iter base.InternalIterator, opts ...IterOpt,
) string

RunInternalIterCmd evaluates a datadriven command controlling an internal iterator, returning a string with the results of the iterator operations.

func RunInternalIterCmdWriter

func RunInternalIterCmdWriter(
	t *testing.T, w io.Writer, d *datadriven.TestData, iter base.InternalIterator, opts ...IterOpt,
)

RunInternalIterCmdWriter evaluates a datadriven command controlling an internal iterator, writing the results of the iterator operations to the provided Writer.

func ShowCommands

func ShowCommands(opts *iterCmdOpts)

ShowCommands configures RunInternalIterCmd to show the command in each output line (so you don't have to visually match the line to the command).

func Verbose

func Verbose(opts *iterCmdOpts)

Verbose configures RunInternalIterCmd to output verbose results.

Types

type ErrorProbe

type ErrorProbe struct {
	// contains filtered or unexported fields
}

ErrorProbe is a Probe that injects an error.

func Error

func Error(name string, err error) *ErrorProbe

Error returns a Probe that returns the provided error. The name is Name returned by String().

func (*ErrorProbe) Error

func (p *ErrorProbe) Error() error

Error implements error, so that injected error values may be used as probes that inject themselves.

func (*ErrorProbe) Probe

func (p *ErrorProbe) Probe(pctx *ProbeContext)

Probe implements the Probe interface, replacing the iterator return value with an error.

func (*ErrorProbe) String

func (p *ErrorProbe) String() string

String implements fmt.Stringer.

type IterOpt

type IterOpt func(*iterCmdOpts)

An IterOpt configures the behavior of RunInternalIterCmd.

func WithSpan

func WithSpan(spanFunc func() *keyspan.Span) IterOpt

WithSpan configures RunInternalIterCmd to print the span returned by spanFunc after each iteration operation.

func WithStats

func WithStats(stats *base.InternalIteratorStats) IterOpt

WithStats configures RunInternalIterCmd to collect iterator stats in the struct pointed to by stats.

type Op

type Op struct {
	Kind    OpKind
	SeekKey []byte
	// Return is initialized with the return result of the underlying iterator.
	// Probes may mutate them.
	Return struct {
		KV  *base.InternalKV
		Err error
	}
}

Op describes an individual iterator operation being performed.

type OpKind

type OpKind int8

OpKind indicates the type of iterator operation being performed.

const (
	// OpSeekGE indicates a SeekGE internal iterator operation.
	OpSeekGE OpKind = iota
	// OpSeekPrefixGE indicates a SeekPrefixGE internal iterator operation.
	OpSeekPrefixGE
	// OpSeekLT indicates a SeekLT internal iterator operation.
	OpSeekLT
	// OpFirst indicates a First internal iterator operation.
	OpFirst
	// OpLast indicates a Last internal iterator operation.
	OpLast
	// OpNext indicates a Next internal iterator operation.
	OpNext
	// OpNextPrefix indicates a NextPrefix internal iterator operation.
	OpNextPrefix
	// OpPrev indicates a Prev internal iterator operation.
	OpPrev
	// OpClose indicates a Close internal iterator operation.
	OpClose
)

func (OpKind) Evaluate

func (o OpKind) Evaluate(pctx *ProbeContext) bool

Evaluate implements Predicate.

func (OpKind) String

func (o OpKind) String() string

String imlements fmt.Stringer.

type Predicate

type Predicate = dsl.Predicate[*ProbeContext]

Predicate encodes conditional logic that yields a boolean.

type Probe

type Probe interface {
	// Probe inspects, and possibly manipulates, iterator operations' results.
	Probe(*ProbeContext)
}

Probe defines an interface for probes that may inspect or mutate internal iterator behavior.

func If

func If(pred Predicate, thenProbe, elseProbe Probe) Probe

If a conditional Probe. If its predicate evaluates to true, it probes using its Then probe. If its predicate evalutes to false, it probes using its Else probe.

func MustParseProbes

func MustParseProbes(parser *dsl.Parser[Probe], probeDSLs ...string) []Probe

MustParseProbes parses each DSL string as a separate probe, returning a slice of parsed probes. Panics if any of the probes fail to parse.

func Nil

func Nil() Probe

Nil returns a Probe that always returns nil.

func Noop

func Noop() Probe

Noop returns a Probe that does nothing.

func ReturnKV

func ReturnKV(kv *base.InternalKV) Probe

ReturnKV returns a Probe that modifies an operation's return value to the provided KV pair.

type ProbeContext

type ProbeContext struct {
	Op
	ProbeState
}

ProbeContext provides the context within which a Probe is run. It includes information about the iterator operation in progress.

type ProbeState

type ProbeState struct {
	*base.Comparer
	Log io.Writer
}

ProbeState holds state additional to the context of the operation that's accessible to probes.

type UserKey

type UserKey []byte

UserKey implements a predicate that evaluates to true if the returned InternalKey holds a specific user key.

func (UserKey) Evaluate

func (p UserKey) Evaluate(pctx *ProbeContext) bool

Evaluate implements Predicate.

func (UserKey) String

func (p UserKey) String() string

String implements fmt.Stringer.

Jump to

Keyboard shortcuts

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