Class RequestMatchers
- Author:
- jamesdbloom
-
Nested Class Summary
Nested classes/interfaces inherited from class org.mockserver.mock.listeners.MockServerMatcherNotifier
MockServerMatcherNotifier.Cause -
Constructor Summary
ConstructorsConstructorDescriptionRequestMatchers(Configuration configuration, MockServerLogger mockServerLogger, Scheduler scheduler, WebSocketClientRegistry webSocketClientRegistry) -
Method Summary
Modifier and TypeMethodDescriptionadd(Expectation expectation, MockServerMatcherNotifier.Cause cause) voidclear(ExpectationId expectationId, String logCorrelationId) voidclear(RequestDefinition requestDefinition) findClosestMatchDiff(HttpRequest httpRequest) firstMatchingEarlyExpectation(HttpRequest headersOnlyRequest) firstMatchingExpectation(RequestDefinition requestDefinition) Returns the state backend, ornullif none has been set.booleanisEmpty()protected voidnotifyListeners(RequestMatchers notifier, MockServerMatcherNotifier.Cause cause) peekFirstMatchingExpectation(RequestDefinition requestDefinition) Side-effect-free probe: returns the first active expectation whose matcher matches the given request, WITHOUT consuming the match.postProcess(Expectation expectation) voidReconciles the node-local HttpRequestMatcher cache against the backend KeyValueStore.voidreset()voidretrieveActiveExpectations(RequestDefinition requestDefinition) retrieveExpectationsMatchingRequest(RequestDefinition requestDefinition) retrieveRequestDefinitions(List<ExpectationId> expectationIds) retrieveRequestMatchers(RequestDefinition requestDefinition) voidsetStateBackend(StateBackend stateBackend) Sets the state backend reference and wires the expectation KV store as the source of truth.intsize()voidupdate(Expectation[] expectations, MockServerMatcherNotifier.Cause cause) Methods inherited from class org.mockserver.mock.listeners.MockServerMatcherNotifier
registerListener, unregisterListenerMethods inherited from class org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
equals, fieldsExcludedFromEqualsAndHashCode, hashCode, toString
-
Constructor Details
-
RequestMatchers
public RequestMatchers(Configuration configuration, MockServerLogger mockServerLogger, Scheduler scheduler, WebSocketClientRegistry webSocketClientRegistry)
-
-
Method Details
-
setStateBackend
Sets the state backend reference and wires the expectation KV store as the source of truth. Called byHttpStateafter construction. The node-local httpRequestMatchers CPQ becomes a derived cache; all mutations route through the backend first.When a backend is wired, the node-local CPQ's maxSize is raised to
Integer.MAX_VALUEso that eviction is controlled exclusively by the backend (avoiding insertion-order divergence between two CPQs on update-in-place vs re-insert).reconcileEvictions()trims the node-local cache after each backend mutation.Threading contract: all control-plane mutations (add, update, remove, clear, reset, setStateBackend) are assumed to be externally serialized — i.e. a single writer at a time. This is satisfied today because
HttpStateserializes control-plane calls on the Netty event loop or holds the action lock.TODO(jamesdbloom): phase 2c — when remote invalidation events arrive concurrently from a clustered backend, this single-writer assumption must be revisited — likely by introducing an internal lock or event-queue around the node-local cache reconciliation.
-
getStateBackend
Returns the state backend, ornullif none has been set. -
add
-
update
-
size
public int size() -
reset
-
reset
public void reset() -
firstMatchingExpectation
-
firstMatchingEarlyExpectation
-
clear
-
clear
-
reconcileFromBackend
public void reconcileFromBackend()Reconciles the node-local HttpRequestMatcher cache against the backend KeyValueStore. This handles three cases:- Eviction: cached matchers whose id is no longer in the backend are removed (mirrors maxExpectations eviction).
- Remote add: backend entries with no local matcher get a new compiled HttpRequestMatcher (enables cross-node visibility under clustering).
- Remote update: backend entries whose version is newer than the locally cached version get their matcher rebuilt.
In single-node / no-backend mode this method is a no-op. When the backend is LOCAL (non-clustered), only eviction applies because all mutations originate locally and the CPQ is already in sync.
Threading contract: serialized via
synchronizedso that concurrent remote invalidation events (from a clustered backend) do not corrupt the node-local CPQ. Local mutations are still single-writer (Netty event loop / action lock); the lock is reentrant-safe for local callers because Java'ssynchronizedis reentrant.Concurrent matching (data-plane) note: this method applies incremental per-entry mutations (add/update/remove) to the CPQ — the same granularity as normal control-plane add/remove. The CPQ's
toSortedList()provides an eventually-consistent sorted snapshot viaConcurrentSkipListSet + volatile sortedCache + filter(nonNull). A matching thread callingtoSortedList()during a reconcile may see a snapshot that lags by one mutation, but will never see a torn/empty view. This matches the pre-existing control-plane / data-plane concurrency contract. -
postProcess
-
retrieveRequestDefinitions
-
retrieveActiveExpectations
-
peekFirstMatchingExpectation
Side-effect-free probe: returns the first active expectation whose matcher matches the given request, WITHOUT consuming the match. Specifically, this method avoids:- Times decrement (
consumeMatch()) - Scenario state transition
responseInProgressflag- Metrics increment
Note on logging: the underlying
HttpRequestMatcher.matches()call may still emitINFO-levelEXPECTATION_MATCHEDorEXPECTATION_NOT_MATCHEDlog entries as a side-effect of the match evaluation. This method does not suppress those match-diagnostic logs. It is the Times/scenario/responseInProgress/metrics side-effects that are avoided.Used by the gRPC bidi router to decide the routing path before committing to a handler. Callers that need to actually consume the match (decrement Times, transition scenarios, emit logs) must still call
firstMatchingExpectation(RequestDefinition)separately on the committed path. - Times decrement (
-
retrieveExpectationsMatchingRequest
-
retrieveRequestMatchers
-
findClosestMatchDiff
-
isEmpty
public boolean isEmpty() -
getScenarioManager
-
notifyListeners
- Overrides:
notifyListenersin classMockServerMatcherNotifier
-