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