Documentation
¶
Overview ¶
Package testutil provides common testing utilities for the goflow library.
Index ¶
- Constants
- func AssertEqual[T comparable](t *testing.T, got, want T)
- func AssertError(t *testing.T, err error)
- func AssertEventually(t *testing.T, condition func() bool)
- func AssertNoError(t *testing.T, err error)
- func AssertNotEqual[T comparable](t *testing.T, got, want T)
- func Eventually(t *testing.T, condition func() bool, timeout, interval time.Duration)
- func EventuallyWithContext(ctx context.Context, t *testing.T, condition func() bool, ...)
- func WaitForInt32(t *testing.T, value *int32, expected int32, timeout time.Duration)
- func WaitForInt64(t *testing.T, value *int64, expected int64, timeout time.Duration)
- func WithTimeout(t *testing.T) (context.Context, context.CancelFunc)
- type CallbackTracker
- func (ct *CallbackTracker) AssertCallCount(t *testing.T, expected int)
- func (ct *CallbackTracker) AssertCalled(t *testing.T)
- func (ct *CallbackTracker) AssertNotCalled(t *testing.T)
- func (ct *CallbackTracker) CallCount() int
- func (ct *CallbackTracker) Called() bool
- func (ct *CallbackTracker) Mark(value ...interface{})
- func (ct *CallbackTracker) Reset()
- func (ct *CallbackTracker) Value() interface{}
- type MockClock
- type MockWriter
- func (mw *MockWriter) Len() int
- func (mw *MockWriter) Reset()
- func (mw *MockWriter) SetAlwaysError(err error)
- func (mw *MockWriter) SetErrorOnNth(n int)
- func (mw *MockWriter) SetWriteDelay(delay time.Duration)
- func (mw *MockWriter) String() string
- func (mw *MockWriter) Write(p []byte) (int, error)
- func (mw *MockWriter) WriteCount() int
Constants ¶
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 ¶
AssertError fails the test if err is nil
func AssertEventually ¶
AssertEventually is like Eventually but with a default timeout of 1 second.
func AssertNoError ¶
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 ¶
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 ¶
WaitForInt32 waits for an atomic int32 to reach the expected value. This is a common pattern for testing concurrent operations.
func WaitForInt64 ¶
WaitForInt64 waits for an atomic int64 to reach the expected value.
func WithTimeout ¶
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) 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 ¶
NewMockClock creates a new MockClock starting at the given time. If zero time is provided, uses current 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 (*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.