Do not remove mock from history after max uses

This commit is contained in:
Piotr Fus 2020-08-11 12:53:35 +02:00
parent 769199f5d4
commit fe9ef89970
4 changed files with 9 additions and 12 deletions

View file

@ -171,7 +171,7 @@ Send POST request to localhost:<PORT>/serverControl
- schema - path to xsd schema file on mockserver classpath; default empty, so no vallidation of request is performed; if validation fails then response has got status 400 and response is raw message from validator
- imports - list of imports for closures (each import is separate tag); `alias` is the name of `fullClassName` available in closure; `fullClassName` must be available on classpath of mock server
- https - HTTPS configuration
- maxUses - limit uses of mock to the specific number, after that mock is removed (any negative number means unlimited - default, cannot set value to 0)
- maxUses - limit uses of mock to the specific number, after that mock is marked as ignored (any negative number means unlimited - default, cannot set value to 0), after this number of invocation mock history is still available, but mock does not apply to any request
- cyclic - should mock be added after `maxUses` uses at the end of the mock list (by default false)
#### HTTPS configuration

View file

@ -96,6 +96,8 @@ class MockServerMaxUsesTest extends Specification {
CloseableHttpResponse response3 = client.execute(restPost)
then:'no mock should be found'
response3.statusLine.statusCode == 404
and:'mock should exist'
remoteMockServer.listMocks().find { it.name == 'mock1' } != null
}
def 'should return two mocks in cyclic order'() {

View file

@ -97,17 +97,15 @@ class ContextExecutor {
private synchronized void handleMaxUses(Mock mock) {
if (mock.hasLimitedUses()) {
mock.decrementUses()
removeAndResetIfNeeded(mock)
resetIfNeeded(mock)
log.debug("Uses left ${mock.usesLeft} of ${mock.maxUses} (is cyclic: ${mock.cyclic})")
}
}
private void removeAndResetIfNeeded(Mock mock) {
if (mock.shouldBeRemoved()) {
mocks.remove(mock)
}
private void resetIfNeeded(Mock mock) {
if (mock.shouldUsesBeReset()) {
mock.resetUses()
mocks.remove(mock)
mocks.add(mock)
}
}

View file

@ -59,7 +59,8 @@ class Mock implements Comparable<Mock> {
}
boolean match(Method method, MockRequest request) {
return this.method == method && predicate(request)
boolean usesCondition = hasLimitedUses() ? usesLeft > 0 : true
return usesCondition && this.method == method && predicate(request)
}
MockResponse apply(MockRequest request) {
@ -188,12 +189,8 @@ class Mock implements Comparable<Mock> {
usesLeft--
}
boolean shouldBeRemoved() {
return hasLimitedUses() && usesLeft <= 0
}
boolean shouldUsesBeReset() {
return shouldBeRemoved() && cyclic
return hasLimitedUses() && usesLeft <= 0 && cyclic
}
void resetUses() {