mocks

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package mocks provides mock implementations of all supervisor interfaces for testing. These mocks implement the Runnable, Reloadable, Stateable, and ReloadSender interfaces with configurable delays to simulate real service behavior in tests.

Example: ```go import (

"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/robbyt/go-supervisor"
"github.com/robbyt/go-supervisor/runnables/mocks"

)

func TestMyComponent(t *testing.T) {
	// Create a mock service
	mockRunnable := mocks.NewMockRunnable()

	// Set expectations
	mockRunnable.On("Run", mock.Anything).Return(nil)
	mockRunnable.On("Stop").Once()

	// For state-based tests
	stateCh := make(chan string)
	mockRunnable.On("GetStateChan", mock.Anything).Return(stateCh)

	// Create supervisor with mock
	super := supervisor.New([]supervisor.Runnable{mockRunnable})

	// Run test...

	// Verify expectations
	mockRunnable.AssertExpectations(t)
}

```

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockRunnableWithReloadSender

type MockRunnableWithReloadSender struct {
	*Runnable
}

MockRunnableWithReloadSender extends Runnable to also implement the ReloadSender interface.

func NewMockRunnableWithReloadSender

func NewMockRunnableWithReloadSender() *MockRunnableWithReloadSender

NewMockRunnableWithReloadSender creates a new MockRunnableWithReload with default delays.

func (*MockRunnableWithReloadSender) GetReloadTrigger

func (m *MockRunnableWithReloadSender) GetReloadTrigger() <-chan struct{}

GetReloadTrigger implements the ReloadSender interface. It returns a receive-only channel that emits signals when a reload is requested.

type MockRunnableWithShutdownSender

type MockRunnableWithShutdownSender struct {
	*Runnable
}

MockRunnableWithShutdownSender extends Runnable to also implement the ShutdownSender interface.

func NewMockRunnableWithShutdownSender

func NewMockRunnableWithShutdownSender() *MockRunnableWithShutdownSender

NewMockRunnableWithShutdown creates a new MockRunnableWithReload with default delays.

func (*MockRunnableWithShutdownSender) GetShutdownTrigger

func (m *MockRunnableWithShutdownSender) GetShutdownTrigger() <-chan struct{}

ShutdownSender mocks implementation of the ShutdownSender interface. It returns a receive-only channel that emits signals when a shutdown is requested.

type MockRunnableWithStateable added in v0.0.4

type MockRunnableWithStateable struct {
	*Runnable
	DelayGetState time.Duration // Delay before GetState and GetStateChan return
}

MockRunnableWithStateable extends Runnable to also implement the Stateable interface.

func NewMockRunnableWithStateable added in v0.0.4

func NewMockRunnableWithStateable() *MockRunnableWithStateable

NewMockRunnableWithStateable creates a new MockRunnableWithStateable with default delays.

func (*MockRunnableWithStateable) GetState added in v0.0.4

func (m *MockRunnableWithStateable) GetState() string

GetState mocks the GetState method of the Stateable interface. It returns the current state of the service as configured in test expectations.

func (*MockRunnableWithStateable) GetStateChan added in v0.0.4

func (m *MockRunnableWithStateable) GetStateChan(ctx context.Context) <-chan string

GetStateChan mocks the GetStateChan method of the Stateable interface. It returns a receive-only channel that will emit state updates as configured in test expectations.

func (*MockRunnableWithStateable) IsRunning added in v0.0.4

func (m *MockRunnableWithStateable) IsRunning() bool

IsRunning mocks the IsRunning method of the Stateable interface. It returns true if the service is currently running, as configured in test expectations.

type Runnable

type Runnable struct {
	mock.Mock
	DelayRun    time.Duration // Delay before Run returns
	DelayStop   time.Duration // Delay before Stop returns
	DelayReload time.Duration // Delay before Reload returns
}

Runnable is a mock implementation of the Runnable, Reloadable, and Stateable interfaces using testify/mock. It allows for configurable delays in method responses to simulate service behavior.

func NewMockRunnable

func NewMockRunnable() *Runnable

NewMockRunnable creates a new Runnable mock with default delays.

func (*Runnable) Reload

func (m *Runnable) Reload()

Reload mocks the Reload method of the Reloadable interface. It sleeps for DelayReload duration before recording the call.

func (*Runnable) Run

func (m *Runnable) Run(ctx context.Context) error

Run mocks the Run method of the Runnable interface. It sleeps for DelayRun duration before returning the mocked error result.

func (*Runnable) Stop

func (m *Runnable) Stop()

Stop mocks the Stop method of the Runnable interface. It sleeps for DelayStop duration before recording the call.

func (*Runnable) String

func (m *Runnable) String() string

String returns a string representation of the mock service. It can be mocked by doing mock.On("String").Return("customValue") in tests.

Jump to

Keyboard shortcuts

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