Documentation
¶
Overview ¶
Package th provides basic test helpers.
Index ¶
- func DelayEach[A any](in <-chan A, delay time.Duration) <-chan A
- func DontClose[A any](in <-chan A) <-chan A
- func ExpectActiveContext(t *testing.T, ctx context.Context)
- func ExpectBetween[A cmp.Ordered](t *testing.T, actual A, min A, max A)
- func ExpectBlock(t *testing.T, f func(t *testing.T))
- func ExpectCanceledContext(t *testing.T, ctx context.Context)
- func ExpectDrainedChan[A any](t *testing.T, ch <-chan A)
- func ExpectElementsMatch[A comparable](t *testing.T, actual, expected []A)
- func ExpectError(t *testing.T, err error, expectedMessage string)
- func ExpectLTE[A cmp.Ordered](t *testing.T, actual A, max A)
- func ExpectLeak(t *testing.T, f func(t *testing.T))
- func ExpectMap[K, V comparable](t *testing.T, actual map[K]V, expected map[K]V)
- func ExpectNoError(t *testing.T, err error)
- func ExpectNoRace[T any](value T)
- func ExpectNotHang(t *testing.T, waitFor time.Duration, f func())
- func ExpectOpenChan[A any](t *testing.T, ch <-chan A)
- func ExpectSlice[A comparable](t *testing.T, actual []A, expected []A)
- func ExpectSorted[T cmp.Ordered](t *testing.T, arr []T)
- func ExpectUnsorted[T cmp.Ordered](t *testing.T, arr []T)
- func ExpectValue[A comparable](t *testing.T, actual A, expected A)
- func FromRange(start, end int) <-chan int
- func FromSlice[A any](slice []A) <-chan A
- func RunSynctest(t *testing.T, name string, f func(t *testing.T))
- func SimulateWork(min, max time.Duration)
- func TestBothOrderings(t *testing.T, f func(t *testing.T, ord bool))
- func TestLevels(t *testing.T, levels []int, f func(t *testing.T, n int))
- func TestVariants[V any](t *testing.T, name string, variants []V, f func(t *testing.T, v V))
- func ToSlice[A any](in <-chan A) []A
- type InFlightGauge
- type Stopwatch
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DelayEach ¶ added in v0.9.0
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 ExpectActiveContext ¶ added in v0.9.0
func ExpectBetween ¶ added in v0.9.0
func ExpectBlock ¶ added in v0.9.0
ExpectBlock runs f in a synctest bubble and asserts that the main goroutine is durably blocked.
func ExpectCanceledContext ¶ added in v0.9.0
func ExpectDrainedChan ¶
func ExpectElementsMatch ¶ added in v0.9.0
func ExpectElementsMatch[A comparable](t *testing.T, actual, expected []A)
func ExpectLeak ¶ added in v0.9.0
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 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 ExpectOpenChan ¶ added in v0.9.0
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 ExpectValue ¶
func ExpectValue[A comparable](t *testing.T, actual A, expected A)
func RunSynctest ¶ added in v0.9.0
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
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 TestLevels ¶ added in v0.9.0
func TestVariants ¶ added in v0.9.0
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.