Help us shape the future of Go! Take the 2026 Go Developer Survey

testutil

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package testutil provides common testing utilities for the goflow library.

Index

Constants

View Source
const TestTimeout = 5 * time.Second

TestTimeout is the default timeout for tests

Variables

This section is empty.

Functions

func AssertEqual

func AssertEqual[T comparable](t *testing.T, got, want T)

AssertEqual fails the test if got != want

func AssertError

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

AssertError fails the test if err is nil

func AssertEventually

func AssertEventually(t *testing.T, condition func() bool)

AssertEventually is like Eventually but with a default timeout of 1 second.

func AssertNoError

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

AssertNoError fails the test if err is not nil

func AssertNotEqual

func AssertNotEqual[T comparable](t *testing.T, got, want T)

AssertNotEqual fails the test if got == want

func Eventually

func Eventually(t *testing.T, condition func() bool, timeout, interval time.Duration)

Eventually repeatedly checks a condition until it's true or timeout is reached. This is useful for testing asynchronous operations without using fixed time.Sleep().

Example:

Eventually(t, func() bool {
    return atomic.LoadInt32(&counter) == expected
}, 500*time.Millisecond, 10*time.Millisecond)

func EventuallyWithContext

func EventuallyWithContext(ctx context.Context, t *testing.T, condition func() bool, interval time.Duration)

EventuallyWithContext is like Eventually but respects context cancellation.

func WaitForInt32

func WaitForInt32(t *testing.T, value *int32, expected int32, timeout time.Duration)

WaitForInt32 waits for an atomic int32 to reach the expected value. This is a common pattern for testing concurrent operations.

func WaitForInt64

func WaitForInt64(t *testing.T, value *int64, expected int64, timeout time.Duration)

WaitForInt64 waits for an atomic int64 to reach the expected value.

func WithTimeout

func WithTimeout(t *testing.T) (context.Context, context.CancelFunc)

WithTimeout creates a context with the default test timeout

Types

type CallbackTracker

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

CallbackTracker tracks whether a callback was called and how many times. Useful for testing callback functionality.

func NewCallbackTracker

func NewCallbackTracker() *CallbackTracker

NewCallbackTracker creates a new callback tracker.

func (*CallbackTracker) AssertCallCount

func (ct *CallbackTracker) AssertCallCount(t *testing.T, expected int)

AssertCallCount fails if the callback was not called exactly n times.

func (*CallbackTracker) AssertCalled

func (ct *CallbackTracker) AssertCalled(t *testing.T)

AssertCalled fails if the callback was not called.

func (*CallbackTracker) AssertNotCalled

func (ct *CallbackTracker) AssertNotCalled(t *testing.T)

AssertNotCalled fails if the callback was called.

func (*CallbackTracker) CallCount

func (ct *CallbackTracker) CallCount() int

CallCount returns the number of times the callback was called.

func (*CallbackTracker) Called

func (ct *CallbackTracker) Called() bool

Called returns true if the callback was called at least once.

func (*CallbackTracker) Mark

func (ct *CallbackTracker) Mark(value ...interface{})

Mark marks the callback as called and optionally stores a value.

func (*CallbackTracker) Reset

func (ct *CallbackTracker) Reset()

Reset resets the tracker.

func (*CallbackTracker) Value

func (ct *CallbackTracker) Value() interface{}

Value returns the last value passed to Mark.

type MockClock

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

MockClock implements Clock interface for testing with controllable time. This is used across multiple rate limiter tests to avoid actual time delays.

func NewMockClock

func NewMockClock(start time.Time) *MockClock

NewMockClock creates a new MockClock starting at the given time. If zero time is provided, uses current time.

func (*MockClock) Advance

func (m *MockClock) Advance(d time.Duration)

Advance moves the mock clock forward by the given duration.

func (*MockClock) Now

func (m *MockClock) Now() time.Time

Now returns the current mock time.

func (*MockClock) Set

func (m *MockClock) Set(t time.Time)

Set sets the mock clock to a specific time.

type MockWriter

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

MockWriter is a test writer that can simulate various write conditions including delays, errors, and write counting.

func NewMockWriter

func NewMockWriter() *MockWriter

NewMockWriter creates a new MockWriter.

func (*MockWriter) Len

func (mw *MockWriter) Len() int

Len returns the current buffer length.

func (*MockWriter) Reset

func (mw *MockWriter) Reset()

Reset clears the buffer and resets counters.

func (*MockWriter) SetAlwaysError

func (mw *MockWriter) SetAlwaysError(err error)

SetAlwaysError configures the writer to always return the given error.

func (*MockWriter) SetErrorOnNth

func (mw *MockWriter) SetErrorOnNth(n int)

SetErrorOnNth configures the writer to error on the nth write.

func (*MockWriter) SetWriteDelay

func (mw *MockWriter) SetWriteDelay(delay time.Duration)

SetWriteDelay configures a delay for each write operation.

func (*MockWriter) String

func (mw *MockWriter) String() string

String returns the current buffer contents.

func (*MockWriter) Write

func (mw *MockWriter) Write(p []byte) (int, error)

Write implements io.Writer interface with configurable behavior.

func (*MockWriter) WriteCount

func (mw *MockWriter) WriteCount() int

WriteCount returns the number of Write calls.

Jump to

Keyboard shortcuts

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