th

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 10 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 th provides basic test helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DelayEach added in v0.9.0

func DelayEach[A any](in <-chan A, delay time.Duration) <-chan A

DelayEach forwards items, sleeping for the given duration before each one. Under synctest, this makes it impossible to consume the channel in zero fake time, so one goroutine (main) can observe the intermediate state of another goroutine (drain) consuming the stream. This function is usually paired with ExpectOpenChan.

A sleep of 1ns - written as a bare 1 in the tests - is the minimum that gives observability: the sleep completes only when every goroutine in the bubble is durably blocked, so the stream cannot advance while any goroutine is runnable.

func DontClose added in v0.9.0

func DontClose[A any](in <-chan A) <-chan A

func ExpectActiveContext added in v0.9.0

func ExpectActiveContext(t *testing.T, ctx context.Context)

func ExpectBetween added in v0.9.0

func ExpectBetween[A cmp.Ordered](t *testing.T, actual A, min A, max A)

func ExpectBlock added in v0.9.0

func ExpectBlock(t *testing.T, f func(t *testing.T))

ExpectBlock runs f in a synctest bubble and asserts that the main goroutine is durably blocked.

func ExpectCanceledContext added in v0.9.0

func ExpectCanceledContext(t *testing.T, ctx context.Context)

func ExpectDrainedChan

func ExpectDrainedChan[A any](t *testing.T, ch <-chan A)

func ExpectElementsMatch added in v0.9.0

func ExpectElementsMatch[A comparable](t *testing.T, actual, expected []A)

func ExpectError

func ExpectError(t *testing.T, err error, expectedMessage string)

func ExpectLTE added in v0.9.0

func ExpectLTE[A cmp.Ordered](t *testing.T, actual A, max A)

func ExpectLeak added in v0.9.0

func ExpectLeak(t *testing.T, f func(t *testing.T))

ExpectLeak runs f in a synctest bubble and asserts that at least one background goroutine is durably blocked, while the main goroutine completes cleanly.

func ExpectMap added in v0.2.0

func ExpectMap[K, V comparable](t *testing.T, actual map[K]V, expected map[K]V)

func ExpectNoError

func ExpectNoError(t *testing.T, err error)

func ExpectNoRace added in v0.9.0

func ExpectNoRace[T any](value T)

ExpectNoRace is a semantic name for a bare unsynchronized read. Tests sometimes need to perform an unsynchronized read of a variable to have the race detector confirm that all writes in other goroutines happen before this read. It's enough to do a no-op read:

_ = myVariable

This works but requires an explanatory comment at every site. ExpectNoRace is also a no-op but makes the call site clearer:

th.ExpectNoRace(myVariable)

func ExpectNotHang

func ExpectNotHang(t *testing.T, waitFor time.Duration, f func())

func ExpectOpenChan added in v0.9.0

func ExpectOpenChan[A any](t *testing.T, ch <-chan A)

ExpectOpenChan checks that the channel emits another item rather than closing. The operation is destructive and blocks until either an item arrives or the channel is closed.

func ExpectSlice

func ExpectSlice[A comparable](t *testing.T, actual []A, expected []A)

func ExpectSorted

func ExpectSorted[T cmp.Ordered](t *testing.T, arr []T)

func ExpectUnsorted

func ExpectUnsorted[T cmp.Ordered](t *testing.T, arr []T)

func ExpectValue

func ExpectValue[A comparable](t *testing.T, actual A, expected A)

func FromRange

func FromRange(start, end int) <-chan int

func FromSlice

func FromSlice[A any](slice []A) <-chan A

func RunSynctest added in v0.9.0

func RunSynctest(t *testing.T, name string, f func(t *testing.T))

RunSynctest runs a subtest in a synctest bubble. It panics unless all goroutines started from f exit cleanly.

func SimulateWork added in v0.9.0

func SimulateWork(min, max time.Duration)

SimulateWork sleeps for a random time in [min, max]. Call it inside a worker function (in a synctest bubble) to enforce concurrent execution instead of relying on scheduler luck.

A random sleep imitates real IO-bound work, where each worker spends most of its time blocked on a network call, and every call takes a similar but not identical time.

Since each worker sleeps at least min per iteration, no single worker can race ahead and grab most of the items. Workers move through the input at different, variable speeds, but they're guaranteed to stay within (n-1)*maxSleep/minSleep items of each other. Tests rely on this bound to check how many extra items were processed after an early return.

func TestBothOrderings

func TestBothOrderings(t *testing.T, f func(t *testing.T, ord bool))

func TestLevels added in v0.9.0

func TestLevels(t *testing.T, levels []int, f func(t *testing.T, n int))

func TestVariants added in v0.9.0

func TestVariants[V any](t *testing.T, name string, variants []V, f func(t *testing.T, v V))

func ToSlice

func ToSlice[A any](in <-chan A) []A

Types

type InFlightGauge added in v0.9.0

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

InFlightGauge measures the peak number of goroutines in flight inside an instrumented region: each goroutine calls Enter when it arrives and Exit when it leaves, and Max reports the high-water mark of how many were in flight at once.

func (*InFlightGauge) Enter added in v0.9.0

func (g *InFlightGauge) Enter()

func (*InFlightGauge) Exit added in v0.9.0

func (g *InFlightGauge) Exit()

func (*InFlightGauge) Max added in v0.9.0

func (g *InFlightGauge) Max() int

type Stopwatch added in v0.9.0

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

Stopwatch measures the time elapsed from the first call to Start to the first call to Stop.

func StartStopwatch added in v0.9.0

func StartStopwatch() *Stopwatch

StartStopwatch returns a new Stopwatch that is already started.

func (*Stopwatch) Elapsed added in v0.9.0

func (sw *Stopwatch) Elapsed() time.Duration

func (*Stopwatch) Start added in v0.9.0

func (sw *Stopwatch) Start()

func (*Stopwatch) Stop added in v0.9.0

func (sw *Stopwatch) Stop()

Jump to

Keyboard shortcuts

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