Skip to content

Commit

Permalink
Disabled internal StubHttpLifecycleCache due to issue #170 until the …
Browse files Browse the repository at this point in the history
…caching solution will be revisited (#176)
  • Loading branch information
azagniotov authored Mar 3, 2021
1 parent 26b85e0 commit 444f864
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ public void shouldMatchDistinctRequestBodiesForTheSameUrlWhenPutRequestMade() th
}

@Test
public void should_SuccessfullyMatchPostRegex_WhenPostRequestMade() throws Exception {
public void stubby4jIssue171() throws Exception {

final String requestUrl = String.format("%s%s", STUBS_URL, "/azagniotov/stubby4j/issues/171");

Expand All @@ -1130,7 +1130,7 @@ public void should_SuccessfullyMatchPostRegex_WhenPostRequestMade() throws Excep
}

@Test
public void should_NotMatchPostRegex_WhenPostRequestMade_WithWrongPayload() throws Exception {
public void stubby4jIssue171_WithWrongPayload() throws Exception {

final String requestUrl = String.format("%s%s", STUBS_URL, "/azagniotov/stubby4j/issues/171");

Expand All @@ -1146,4 +1146,39 @@ public void should_NotMatchPostRegex_WhenPostRequestMade_WithWrongPayload() thro
final HttpResponse response = request.execute();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND_404);
}

@Test
public void stubby4jIssue170() throws Exception {

final String requestUrl = String.format("%s%s", STUBS_URL, "/azagniotov/stubby4j/issues/170");
final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(HEADER_APPLICATION_JSON);

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Making request#1 which matches rule_3 ONLY, this will cache stub for rule_3 by the above requestUrl
////////////////////////////////////////////////////////////////////////////////////////////////////////////
final String contentOne = "{\"rule\":\"rule_3\",\"request_id\":\"rule_3_request_id\"}";
final HttpRequest requestOne = HttpUtils.constructHttpRequest(HttpMethods.POST, requestUrl, contentOne);

requestOne.setHeaders(httpHeaders);
final HttpResponse responseOne = requestOne.execute();
final String responseOneContentAsString = responseOne.parseAsString().trim();

assertThat(responseOne.getStatusCode()).isEqualTo(HttpStatus.CREATED_201);
assertThat("rule_3").isEqualTo(responseOneContentAsString);

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Making request#2 which matches rule_1 AND rule_3. But, in this case,
// we are expecting rule_1 as a response, because the rule_1 is defined earlier than rule_3
////////////////////////////////////////////////////////////////////////////////////////////////////////////
final String contentTwo = "{\"rule\":\"rule_1\",\"request_id\":\"rule_1_request_id\"}";
final HttpRequest requestTwo = HttpUtils.constructHttpRequest(HttpMethods.POST, requestUrl, contentTwo);

requestTwo.setHeaders(httpHeaders);
final HttpResponse responseTwo = requestTwo.execute();
final String responseTwoContentAsString = responseTwo.parseAsString().trim();

assertThat(responseTwo.getStatusCode()).isEqualTo(HttpStatus.CREATED_201);
assertThat("rule_1").isEqualTo(responseTwoContentAsString);
}
}
42 changes: 42 additions & 0 deletions src/functional-test/resources/yaml/stubs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,45 @@
headers:
content-type: application/json
status: 201


- description: rule_1
request:
url: /azagniotov/stubby4j/issues/170
method: POST
headers:
content-type: application/json
post: >
{"rule":"rule_1","request_id":"rule_1_request_id"}
response:
body: rule_1
status: 201


- description: rule_2
request:
url: /azagniotov/stubby4j/issues/170
method: POST
headers:
content-type: application/json
post: >
{"rule":"rule_2","request_id":"rule_2_request_id"}
response:
body: rule_2
status: 201


- description: rule_3
request:
url: /azagniotov/stubby4j/issues/170
method: POST
headers:
content-type: application/json
post: >
{"rule":"(.*)","request_id":"(.*)"}
response:
body: rule_3
status: 201
Original file line number Diff line number Diff line change
Expand Up @@ -182,32 +182,10 @@ private synchronized Optional<StubHttpLifecycle> matchStub(final StubHttpLifecyc
final long initialStart = System.currentTimeMillis();
final String incomingRequestUrl = incomingStub.getUrl();

final Optional<StubHttpLifecycle> cachedMatchCandidateOptional = stubMatchesCache.get(incomingRequestUrl);

return cachedMatchCandidateOptional.map(cachedMatchCandidate -> {

ANSITerminal.loaded(String.format("Local cache contains potential match for the URL [%s]", incomingRequestUrl));
LOGGER.debug("Local cache contains potential match for the URL [{}].", incomingRequestUrl);

// The order(?) in which equality is determined is important here (what object is "equal to" the other one)
if (incomingStub.equals(cachedMatchCandidate)) {
final long elapsed = System.currentTimeMillis() - initialStart;
logMatch(elapsed, cachedMatchCandidate);
ANSITerminal.loaded(String.format("Potential match for the URL [%s] was deemed as a full match", incomingRequestUrl));
LOGGER.debug("Potential match for the URL [{}] was deemed as a full match.", incomingRequestUrl);

return Optional.of(cachedMatchCandidate);
}

ANSITerminal.warn(String.format("Cached match for the URL [%s] failed to match fully, invalidating match cache..", incomingRequestUrl));
LOGGER.warn("Cached match for the URL [{}] failed to match fully, invalidating match cache.", incomingRequestUrl);

stubMatchesCache.clearByKey(incomingRequestUrl);

// Since we did not find match with cached match candidate, then:
return matchAll(incomingStub, initialStart, incomingRequestUrl);

}).orElseGet(() -> matchAll(incomingStub, initialStart, incomingRequestUrl));
// TODO Caching related behavior is disabled by https://github.com/azagniotov/stubby4j/pull/176
// due to https://github.com/azagniotov/stubby4j/issues/170 until a more viable way to use
// the cache for matching optimization is identified
return matchAll(incomingStub, initialStart, incomingRequestUrl);
}

private Optional<StubHttpLifecycle> matchAll(final StubHttpLifecycle incomingStub, final long initialStart, final String incomingRequestUrl) {
Expand All @@ -218,7 +196,11 @@ private Optional<StubHttpLifecycle> matchAll(final StubHttpLifecycle incomingStu

ANSITerminal.status(String.format("Caching the found match for URL [%s]", incomingRequestUrl));
LOGGER.debug("Caching the found match for URL [{}].", incomingRequestUrl);
stubMatchesCache.putIfAbsent(incomingRequestUrl, stubbed);

// TODO Caching related behavior is disabled by https://github.com/azagniotov/stubby4j/pull/176
// due to https://github.com/azagniotov/stubby4j/issues/170 until a more viable way to use
// the cache for matching optimization is identified
// stubMatchesCache.putIfAbsent(incomingRequestUrl, stubbed);

return Optional.of(stubbed);
}
Expand Down

0 comments on commit 444f864

Please sign in to comment.