Introducing limit of times a mock is available#33
Conversation
f59623d to
3e196f0
Compare
|
Thanks for the contribution @ferranjr. I see the new tonic release has some breaking changes causing this build to fail (I have the version loosely set to |
|
@declark1 PR is now rebased with your changes |
|
I was thinking about this further and I realized that it would probably be better to implement this specific feature at the mock-level (as an option, similar to priority), rather than at the matcher-level, so that the mock has a limit rather than one of it's specific matchers. This wasn't initially possible as we didn't have a counter implemented yet to track the number of matches. I'm merging a batch of changes now, including adding For this, I was thinking we can:
The usage would then look like this: // With Mock::new()
let mock = Mock::new(|when, then| {
when.post().path(..).json(..);
then.json(..);
})
.with_limit(2);
// With shorthand MockSet::mock_with_options()
let mut mocks = MockSet::new();
mocks.mock_with_options(priority, limit, |when, then|) {
when.post().path(..).json(..);
then.json(..);
limit = Some(2);
}What are your thoughts on this? |
|
Yes, makes sense to move it to mock level... I noticed the changes on your other PR, and already was thinking how this could be change. Definitely like the approach: Although, probably from working using other mocking libraries before, I am kind of bias towards Maybe limit gives me an idea of no more calls are allowed... where times, if follow by another mock, gives an idea of the amount of times it behaves that way? In any case, move it Mock level makes more sense, and we can implement it with the counter you have in that PR |
|
BTW, happy to work on it later, I see you merged the changes already |
|
Thanks @ferranjr, sure it would be great if you can add this. Apologies for the re-work. I suppose |
d493d3c to
32717c2
Compare
Signed-off-by: Ferran Puig-Calvache <ferran.puig@coralogix.com>
times matcher to enable mocker server basic state changeslimit of times a mock is available
|
@declark1 done the rework, and while doing it the |
Signed-off-by: Ferran Puig-Calvache <ferran.puig@coralogix.com>
declark1
left a comment
There was a problem hiding this comment.
A couple nits to update names to limit, otherwise looks good, thanks!
| pub fn with_limit(mut self, times: usize) -> Self { | ||
| self.limit = Some(times); |
There was a problem hiding this comment.
nit: can you update times -> limit here and below in matches()
| } | ||
|
|
||
| #[test] | ||
| fn test_times() { |
There was a problem hiding this comment.
nit: rename to test_limit()
| pub priority: u8, | ||
| /// Match counter. | ||
| pub match_count: AtomicUsize, | ||
| /// Limit the times the mock will work. |
There was a problem hiding this comment.
nit: reword docstring consistent with above:
/// Limit on how many times this mock can be matched.
Signed-off-by: Ferran Puig-Calvache <ferran.puig@coralogix.com>
|
sorted, clearly I was debating times vs limit till last second :) |
Signed-off-by: Ferran Puig-Calvache <ferran.puig@coralogix.com>
As proposed in #32 this introduces a little matcher helper to limit the amount of times a matcher successfully matches.
This enables testing of retries mechanisms on a client and hopefully help other cases too.