mocks

package
v0.9.2 Latest Latest
Warning

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

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

Documentation

Overview

Package mocks provides mocks for the interfaces defined in the `contracts` package.

Testing Philosophy: High-Fidelity Mocks

The components that consume these contracts, particularly the `controller.Processor`, are complex, concurrent orchestrators. Testing them reliably requires more than simple stubs. It requires high-fidelity mocks that allow for the deterministic simulation of race conditions and specific failure modes.

For this reason, mocks like `MockManagedQueue` are deliberately stateful and thread-safe. They provide a reliable, in-memory simulation of the real component's behavior, while also providing function-based overrides (e.g., `AddFunc`) that allow tests to inject specific errors or pause execution at critical moments. This strategy is essential for creating the robust, non-flaky tests needed to verify the correctness of the system's concurrent logic. For a more detailed defense of this strategy, see the comment at the top of `controller/internal/processor_test.go`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockEndpointCandidates

type MockEndpointCandidates struct {
	// LocateFunc allows injecting custom logic.
	LocateFunc func(ctx context.Context, requestMetadata map[string]any) []fwkdl.Endpoint
	// Candidates is a static return value used if LocateFunc is nil.
	Candidates []fwkdl.Endpoint
}

MockEndpointCandidates provides a mock implementation of the contracts.EndpointCandidates interface. It allows tests to control the exact set of endpoint candidates returned for a given request.

func (*MockEndpointCandidates) Locate

func (m *MockEndpointCandidates) Locate(ctx context.Context, requestMetadata map[string]any) []fwkdl.Endpoint

type MockManagedQueue

type MockManagedQueue struct {
	// FlowKeyV defines the flow specification for this mock queue. It should be set by the test.
	FlowKeyV flowcontrol.FlowKey

	// AddFunc allows a test to completely override the default Add behavior.
	AddFunc func(item flowcontrol.QueueItemAccessor) error
	// RemoveFunc allows a test to completely override the default Remove behavior.
	RemoveFunc func(handle flowcontrol.QueueItemHandle) (flowcontrol.QueueItemAccessor, error)
	// CleanupFunc allows a test to completely override the default Cleanup behavior.
	CleanupFunc func(predicate contracts.PredicateFunc) []flowcontrol.QueueItemAccessor
	// DrainFunc allows a test to completely override the default Drain behavior.
	DrainFunc func() []flowcontrol.QueueItemAccessor
	// OrderingPolicyFunc allows a test to override OrderingPolicy.
	OrderingPolicyFunc func() flowcontrol.OrderingPolicy
	// contains filtered or unexported fields
}

MockManagedQueue is a high-fidelity, thread-safe mock of the `contracts.ManagedQueue` interface, designed specifically for testing the concurrent `controller/internal.Processor`.

This mock is essential for creating deterministic and focused unit tests. It allows for precise control over queue behavior and enables the testing of critical edge cases (e.g., empty queues, dispatch failures) in complete isolation, which would be difficult and unreliable to achieve with the concrete `registry.managedQueue` implementation.

### Design Philosophy

  1. **Stateful**: The mock maintains an internal map of items to accurately reflect a real queue's state. Its `Len()` and `ByteSize()` methods are derived directly from this state.
  2. **Deadlock-Safe Overrides**: Test-specific logic (e.g., `AddFunc`) is executed instead of the default implementation. The override function is fully responsible for its own logic and synchronization, as the mock's internal mutex will *not* be held during its execution.
  3. **Self-Wiring**: The `FlowQueueAccessor()` method returns the mock itself, ensuring the accessor is always correctly connected to the queue's state without manual wiring in tests.

func (*MockManagedQueue) Add

Add adds an item to the queue. It checks for a test override before locking. If no override is present, it executes the default stateful logic, which includes fulfilling the `SafeQueue.Add` contract.

func (*MockManagedQueue) ByteSize

func (m *MockManagedQueue) ByteSize() uint64

ByteSize returns the actual total byte size of all items in the mock queue.

func (*MockManagedQueue) Capabilities

func (m *MockManagedQueue) Capabilities() []flowcontrol.QueueCapability

func (*MockManagedQueue) Cleanup

Cleanup removes items matching a predicate. It checks for a test override before locking.

func (*MockManagedQueue) Drain

Drain removes all items from the queue. It checks for a test override before locking.

func (*MockManagedQueue) FlowKey

func (m *MockManagedQueue) FlowKey() flowcontrol.FlowKey

func (*MockManagedQueue) FlowQueueAccessor

func (m *MockManagedQueue) FlowQueueAccessor() flowcontrol.FlowQueueAccessor

FlowQueueAccessor returns the mock itself, as it fully implements the `flowcontrol.FlowQueueAccessor` interface.

func (*MockManagedQueue) Len

func (m *MockManagedQueue) Len() int

Len returns the actual number of items currently in the mock queue.

func (*MockManagedQueue) Name

func (m *MockManagedQueue) Name() string

func (*MockManagedQueue) OrderingPolicy

func (m *MockManagedQueue) OrderingPolicy() flowcontrol.OrderingPolicy

func (*MockManagedQueue) PeekHead

PeekHead returns the first item found in the mock queue. Note: map iteration order is not guaranteed.

func (*MockManagedQueue) PeekTail

PeekTail is not implemented for this mock.

func (*MockManagedQueue) Remove

Remove removes an item from the queue. It checks for a test override before locking.

type MockRegistryDataPlane

type MockRegistryDataPlane struct {
	ManagedQueueFunc             func(key flowcontrol.FlowKey) (contracts.ManagedQueue, error)
	FairnessPolicyFunc           func(priority int) (flowcontrol.FairnessPolicy, error)
	PriorityBandAccessorFunc     func(priority int) (flowcontrol.PriorityBandAccessor, error)
	AllOrderedPriorityLevelsFunc func() []int
	StatsFunc                    func() contracts.AggregateStats
	WithConnectionFunc           func(key flowcontrol.FlowKey, fn func(conn contracts.ActiveFlowConnection) error) error
}

MockRegistryDataPlane is a simple "stub-style" mock for testing. Its methods are implemented as function fields (e.g., `IDFunc`). A test can inject behavior by setting the desired function field in the test setup. If a func is nil, the method will return a zero value.

func (*MockRegistryDataPlane) AllOrderedPriorityLevels

func (m *MockRegistryDataPlane) AllOrderedPriorityLevels() []int

func (*MockRegistryDataPlane) ApplyDesiredPriorities

func (m *MockRegistryDataPlane) ApplyDesiredPriorities(_ map[int]struct{})

func (*MockRegistryDataPlane) ExecuteGCCycle

func (m *MockRegistryDataPlane) ExecuteGCCycle()

func (*MockRegistryDataPlane) FairnessPolicy

func (m *MockRegistryDataPlane) FairnessPolicy(priority int) (flowcontrol.FairnessPolicy, error)

func (*MockRegistryDataPlane) FlowGCTimeout

func (m *MockRegistryDataPlane) FlowGCTimeout() time.Duration

func (*MockRegistryDataPlane) ManagedQueue

func (*MockRegistryDataPlane) PriorityBandAccessor

func (m *MockRegistryDataPlane) PriorityBandAccessor(priority int) (flowcontrol.PriorityBandAccessor, error)

func (*MockRegistryDataPlane) PriorityBandUpdateChannel

func (m *MockRegistryDataPlane) PriorityBandUpdateChannel() <-chan map[int]struct{}

func (*MockRegistryDataPlane) Stats

func (*MockRegistryDataPlane) SubmitDesiredPriorities

func (m *MockRegistryDataPlane) SubmitDesiredPriorities(_ map[int]struct{})

func (*MockRegistryDataPlane) WithConnection

func (m *MockRegistryDataPlane) WithConnection(key flowcontrol.FlowKey, fn func(conn contracts.ActiveFlowConnection) error) error

type MockSafeQueue

type MockSafeQueue struct {
	NameV         string
	CapabilitiesV []flowcontrol.QueueCapability
	LenV          int
	ByteSizeV     uint64
	PeekHeadV     flowcontrol.QueueItemAccessor
	PeekTailV     flowcontrol.QueueItemAccessor
	AddFunc       func(item flowcontrol.QueueItemAccessor)
	RemoveFunc    func(handle flowcontrol.QueueItemHandle) (flowcontrol.QueueItemAccessor, error)
	CleanupFunc   func(predicate contracts.PredicateFunc) []flowcontrol.QueueItemAccessor
	DrainFunc     func() []flowcontrol.QueueItemAccessor
}

MockSafeQueue is a simple stub mock for the SafeQueue interface. It is used for tests that need to control the exact return values of a queue's methods without simulating the queue's internal logic or state.

func (*MockSafeQueue) Add

func (*MockSafeQueue) ByteSize

func (m *MockSafeQueue) ByteSize() uint64

func (*MockSafeQueue) Capabilities

func (m *MockSafeQueue) Capabilities() []flowcontrol.QueueCapability

func (*MockSafeQueue) Cleanup

func (*MockSafeQueue) Drain

func (*MockSafeQueue) Len

func (m *MockSafeQueue) Len() int

func (*MockSafeQueue) Name

func (m *MockSafeQueue) Name() string

func (*MockSafeQueue) PeekHead

func (*MockSafeQueue) PeekTail

func (*MockSafeQueue) Remove

type MockSaturationDetector

type MockSaturationDetector struct {
	SaturationFunc func(ctx context.Context, candidatePods []fwkdl.Endpoint) float64
}

MockSaturationDetector is a simple "stub-style" mock for testing.

func (*MockSaturationDetector) Saturation

func (m *MockSaturationDetector) Saturation(ctx context.Context, candidatePods []fwkdl.Endpoint) float64

Jump to

Keyboard shortcuts

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