Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to disable the RegEx pattern cache? #170

Closed
harrysun2006 opened this issue Mar 3, 2021 · 16 comments · Fixed by #176
Closed

How to disable the RegEx pattern cache? #170

harrysun2006 opened this issue Mar 3, 2021 · 16 comments · Fixed by #176

Comments

@harrysun2006
Copy link

Hey Alex,

I'm just wondering is there a way to disable the RegEx pattern cache from command line? In my project, I need to response different payloads based on the request payload (url and headers are same). So I defined a few rules (request/response) with different RegEx pattern in post field and I noticed that the matching is tried on rules in natural order before any such rules are cached. However, after one rule is cached, the matching will be tried on potential matched rules first, this cause some inconsistency issue.
For example, I defined four rules below (pseudo code):

- description: rule1
   request:
      method: PUT
      url: service_endpoint
      post: regex1
   response:
      file: response1.json

- description: rule2
   request:
      method: PUT
      url: service_endpoint
      post: regex2
   response:
      file: response2.json

- description: rule3
   request:
      method: PUT
      url: service_endpoint
      post: regex3
   response:
      file: response3.json

And then I make a few requests with different payload (matching to the regular expressions). If the payload in first request only matches to rule3, then regex3 will be cached. And in a following request, when the payload matches to both rule1 and rule3, I was expecting rule1 gets matched and response1.json is picked.

It would be great if there is a way to turn off such RegEx pattern cache. Please let me know your thoughts on it.

Thanks,
Harry

@azagniotov
Copy link
Owner

azagniotov commented Mar 3, 2021

Hi Harry,

For context:

There are two caches in stubby4j:

  1. RegexPatternCache
  2. StubHttpLifecycleCache

The RegexPatternCache is used by RegexParser class only to cache Java's compiled Pattern objects, not the regex matches. This is to avoid recompiling Pattern object for the same regex string when doing incoming HTTP request matching to stubs. For example, if there are repeated requests to the same stub within a small timeframe, there is no need to compile the same Pattern object over and over again

The StubHttpLifecycleCache is the one that maybe causing you headache. This cache is used in StubRepository (see StubRepository#matchStub). This cache caches matched stubs by the URL of the incoming request AFTER the incoming request was successfully matched to a stub.

If there is a repeated request for the same URL and the above cache contains a stub for this URL, then this cached stub (in the code the variable's name is cachedMatchCandidate) is evaluated first of all before all other stubs (this stub's stubbed properties evaluated for the equality against this new incoming request).

If the cached stub was not matched to the incoming request (even though that their URLs are equal), the incoming request is matched to all other stubs in their natural defined order.

@azagniotov azagniotov linked a pull request Mar 3, 2021 that will close this issue
@azagniotov
Copy link
Owner

azagniotov commented Mar 3, 2021

I tried to reproduce the issue you are experiencing in the following PR #176 (see the stubs.yaml and the stubby4jIssue170 test case.

I made sure that the behavior of stubby4j is as expected and the test stubby4jIssue170 passes when the cache is disabled. I will disable the stub cache until I revisit this so that it would not be causing any issues.

azagniotov added a commit that referenced this issue Mar 3, 2021
@azagniotov azagniotov linked a pull request Mar 3, 2021 that will close this issue
azagniotov added a commit that referenced this issue Mar 3, 2021
azagniotov added a commit that referenced this issue Mar 3, 2021
@azagniotov
Copy link
Owner

Harry, I got to this sooner than I anticipated: version v7.1.3 has been pushed to Maven Central. So, you can skip the v7.1.2

@azagniotov azagniotov reopened this Mar 3, 2021
@harrysun2006
Copy link
Author

Hi Alex,
Thanks for the detailed explanation. Now I understand it's the StubHttpLifecycleCache which causes some issues in my test because I have stubs sharing same URL and method.
I will do some test with v7.1.3 and let you know the results.
Cheers,
Harry

@azagniotov
Copy link
Owner

Great, Harry. Looking forward to hear about the results and if there are any other issues

@harrysun2006
Copy link
Author

It's tested and working well in v7.1.3. I can see now the first matched rule is always picked up even when some other rules (with same URL and method) were cached from earlier matches.
Great job. Thanks Alex 👍

@azagniotov
Copy link
Owner

Awesome!

@azagniotov
Copy link
Owner

Harry, just for the sake of clarity I wanted to mention that in v7.1.3 the StubHttpLifecycleCache is completely disabled.

I do have a PR outstanding to bring it back #179 where I am caching stubs not just by a URI, but by the hashCode of the incoming request, but I am not going to rush to merge it in as I want to perform more testing

@harrysun2006
Copy link
Author

harrysun2006 commented Mar 4, 2021

Hey Alex, I noticed that there was no stub matched from caches any more during my test with v7.1.3. I totally agree with you that StubHttpLifecycleCache based on URL + method + hash of request as cache key would be good enough.

@azagniotov
Copy link
Owner

Hi Harry, just FYI I release v7.2.0 to Maven just now. The changes are https://github.com/azagniotov/stubby4j/blob/master/CHANGELOG.md#720 I did not observe any issues when doing automated and manual testing, so I hope things will be as smooth for you as now.

@harrysun2006
Copy link
Author

Hi Alex, I will do some text when v7.2.0 is available on mvnrepository and get back to you with my test results. Have a good weekend.

@azagniotov
Copy link
Owner

It is on Maven Central now: https://search.maven.org/search?q=g:io.github.azagniotov%20AND%20a:stubby4j

Have a great weekend too!

@harrysun2006
Copy link
Author

Tested with version v7.2.0 jar, it's all good now. Thanks Alex! 😄

@azagniotov
Copy link
Owner

Nice! Thanks for letting me know!

@azagniotov
Copy link
Owner

@harrysun2006 apologies for reusing this ticket, but just FYI, new feature: https://github.com/azagniotov/stubby4j#request-proxying

@harrysun2006
Copy link
Author

Hey Alex, no need to apologise and thank you for letting me know. It's a nice new feature that will be helpful in many scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants