Merge pull request #6 from TouK/max.uses.history
Do not remove mock from history after max uses
This commit is contained in:
commit
c0188689f2
4 changed files with 9 additions and 12 deletions
|
@ -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
|
- 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
|
- 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
|
- 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)
|
- cyclic - should mock be added after `maxUses` uses at the end of the mock list (by default false)
|
||||||
|
|
||||||
#### HTTPS configuration
|
#### HTTPS configuration
|
||||||
|
|
|
@ -96,6 +96,8 @@ class MockServerMaxUsesTest extends Specification {
|
||||||
CloseableHttpResponse response3 = client.execute(restPost)
|
CloseableHttpResponse response3 = client.execute(restPost)
|
||||||
then:'no mock should be found'
|
then:'no mock should be found'
|
||||||
response3.statusLine.statusCode == 404
|
response3.statusLine.statusCode == 404
|
||||||
|
and:'mock should exist'
|
||||||
|
remoteMockServer.listMocks().find { it.name == 'mock1' } != null
|
||||||
}
|
}
|
||||||
|
|
||||||
def 'should return two mocks in cyclic order'() {
|
def 'should return two mocks in cyclic order'() {
|
||||||
|
|
|
@ -97,17 +97,15 @@ class ContextExecutor {
|
||||||
private synchronized void handleMaxUses(Mock mock) {
|
private synchronized void handleMaxUses(Mock mock) {
|
||||||
if (mock.hasLimitedUses()) {
|
if (mock.hasLimitedUses()) {
|
||||||
mock.decrementUses()
|
mock.decrementUses()
|
||||||
removeAndResetIfNeeded(mock)
|
resetIfNeeded(mock)
|
||||||
log.debug("Uses left ${mock.usesLeft} of ${mock.maxUses} (is cyclic: ${mock.cyclic})")
|
log.debug("Uses left ${mock.usesLeft} of ${mock.maxUses} (is cyclic: ${mock.cyclic})")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeAndResetIfNeeded(Mock mock) {
|
private void resetIfNeeded(Mock mock) {
|
||||||
if (mock.shouldBeRemoved()) {
|
|
||||||
mocks.remove(mock)
|
|
||||||
}
|
|
||||||
if (mock.shouldUsesBeReset()) {
|
if (mock.shouldUsesBeReset()) {
|
||||||
mock.resetUses()
|
mock.resetUses()
|
||||||
|
mocks.remove(mock)
|
||||||
mocks.add(mock)
|
mocks.add(mock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,8 @@ class Mock implements Comparable<Mock> {
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean match(Method method, MockRequest request) {
|
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) {
|
MockResponse apply(MockRequest request) {
|
||||||
|
@ -188,12 +189,8 @@ class Mock implements Comparable<Mock> {
|
||||||
usesLeft--
|
usesLeft--
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean shouldBeRemoved() {
|
|
||||||
return hasLimitedUses() && usesLeft <= 0
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean shouldUsesBeReset() {
|
boolean shouldUsesBeReset() {
|
||||||
return shouldBeRemoved() && cyclic
|
return hasLimitedUses() && usesLeft <= 0 && cyclic
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetUses() {
|
void resetUses() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue