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 ¶
- type MockEndpointCandidates
- type MockManagedQueue
- func (m *MockManagedQueue) Add(item flowcontrol.QueueItemAccessor) error
- func (m *MockManagedQueue) ByteSize() uint64
- func (m *MockManagedQueue) Capabilities() []flowcontrol.QueueCapability
- func (m *MockManagedQueue) Cleanup(predicate contracts.PredicateFunc) []flowcontrol.QueueItemAccessor
- func (m *MockManagedQueue) Drain() []flowcontrol.QueueItemAccessor
- func (m *MockManagedQueue) FlowKey() flowcontrol.FlowKey
- func (m *MockManagedQueue) FlowQueueAccessor() flowcontrol.FlowQueueAccessor
- func (m *MockManagedQueue) Len() int
- func (m *MockManagedQueue) Name() string
- func (m *MockManagedQueue) OrderingPolicy() flowcontrol.OrderingPolicy
- func (m *MockManagedQueue) PeekHead() flowcontrol.QueueItemAccessor
- func (m *MockManagedQueue) PeekTail() flowcontrol.QueueItemAccessor
- func (m *MockManagedQueue) Remove(handle flowcontrol.QueueItemHandle) (flowcontrol.QueueItemAccessor, error)
- type MockRegistryDataPlane
- func (m *MockRegistryDataPlane) AllOrderedPriorityLevels() []int
- func (m *MockRegistryDataPlane) ApplyDesiredPriorities(_ map[int]struct{})
- func (m *MockRegistryDataPlane) ExecuteGCCycle()
- func (m *MockRegistryDataPlane) FairnessPolicy(priority int) (flowcontrol.FairnessPolicy, error)
- func (m *MockRegistryDataPlane) FlowGCTimeout() time.Duration
- func (m *MockRegistryDataPlane) ManagedQueue(key flowcontrol.FlowKey) (contracts.ManagedQueue, error)
- func (m *MockRegistryDataPlane) PriorityBandAccessor(priority int) (flowcontrol.PriorityBandAccessor, error)
- func (m *MockRegistryDataPlane) PriorityBandUpdateChannel() <-chan map[int]struct{}
- func (m *MockRegistryDataPlane) Stats() contracts.AggregateStats
- func (m *MockRegistryDataPlane) SubmitDesiredPriorities(_ map[int]struct{})
- func (m *MockRegistryDataPlane) WithConnection(key flowcontrol.FlowKey, fn func(conn contracts.ActiveFlowConnection) error) error
- type MockSafeQueue
- func (m *MockSafeQueue) Add(item flowcontrol.QueueItemAccessor)
- func (m *MockSafeQueue) ByteSize() uint64
- func (m *MockSafeQueue) Capabilities() []flowcontrol.QueueCapability
- func (m *MockSafeQueue) Cleanup(predicate contracts.PredicateFunc) []flowcontrol.QueueItemAccessor
- func (m *MockSafeQueue) Drain() []flowcontrol.QueueItemAccessor
- func (m *MockSafeQueue) Len() int
- func (m *MockSafeQueue) Name() string
- func (m *MockSafeQueue) PeekHead() flowcontrol.QueueItemAccessor
- func (m *MockSafeQueue) PeekTail() flowcontrol.QueueItemAccessor
- func (m *MockSafeQueue) Remove(handle flowcontrol.QueueItemHandle) (flowcontrol.QueueItemAccessor, error)
- type MockSaturationDetector
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.
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
- **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.
- **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.
- **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 ¶
func (m *MockManagedQueue) Add(item flowcontrol.QueueItemAccessor) error
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 ¶
func (m *MockManagedQueue) Cleanup(predicate contracts.PredicateFunc) []flowcontrol.QueueItemAccessor
Cleanup removes items matching a predicate. It checks for a test override before locking.
func (*MockManagedQueue) Drain ¶
func (m *MockManagedQueue) Drain() []flowcontrol.QueueItemAccessor
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 ¶
func (m *MockManagedQueue) PeekHead() flowcontrol.QueueItemAccessor
PeekHead returns the first item found in the mock queue. Note: map iteration order is not guaranteed.
func (*MockManagedQueue) PeekTail ¶
func (m *MockManagedQueue) PeekTail() flowcontrol.QueueItemAccessor
PeekTail is not implemented for this mock.
func (*MockManagedQueue) Remove ¶
func (m *MockManagedQueue) Remove(handle flowcontrol.QueueItemHandle) (flowcontrol.QueueItemAccessor, error)
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 (m *MockRegistryDataPlane) ManagedQueue(key flowcontrol.FlowKey) (contracts.ManagedQueue, error)
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 (m *MockRegistryDataPlane) Stats() contracts.AggregateStats
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 (m *MockSafeQueue) Add(item flowcontrol.QueueItemAccessor)
func (*MockSafeQueue) ByteSize ¶
func (m *MockSafeQueue) ByteSize() uint64
func (*MockSafeQueue) Capabilities ¶
func (m *MockSafeQueue) Capabilities() []flowcontrol.QueueCapability
func (*MockSafeQueue) Cleanup ¶
func (m *MockSafeQueue) Cleanup(predicate contracts.PredicateFunc) []flowcontrol.QueueItemAccessor
func (*MockSafeQueue) Drain ¶
func (m *MockSafeQueue) Drain() []flowcontrol.QueueItemAccessor
func (*MockSafeQueue) Len ¶
func (m *MockSafeQueue) Len() int
func (*MockSafeQueue) Name ¶
func (m *MockSafeQueue) Name() string
func (*MockSafeQueue) PeekHead ¶
func (m *MockSafeQueue) PeekHead() flowcontrol.QueueItemAccessor
func (*MockSafeQueue) PeekTail ¶
func (m *MockSafeQueue) PeekTail() flowcontrol.QueueItemAccessor
func (*MockSafeQueue) Remove ¶
func (m *MockSafeQueue) Remove(handle flowcontrol.QueueItemHandle) (flowcontrol.QueueItemAccessor, error)
type MockSaturationDetector ¶
type MockSaturationDetector struct {
SaturationFunc func(ctx context.Context, candidatePods []fwkdl.Endpoint) float64
}
MockSaturationDetector is a simple "stub-style" mock for testing.