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

Allow setting different responses per request #31 #102

Closed
wants to merge 17 commits into from
Closed

Allow setting different responses per request #31 #102

wants to merge 17 commits into from

Conversation

infinite-spectrum
Copy link

@infinite-spectrum infinite-spectrum commented Jun 22, 2021

issue: #31

Following is the summary of the changes done in the PR so far:

  • As part of this PR, I've introduced a new way to handle Random/Repeatable responses.
  • I've used burst terminology for repeatable responses.

Changed Items:

  • Changed response field in an imposter to responses (array of response)

Added Items:

  • Added burst field in response schema under imposter schema

    • this new field has a +integer value, which represents no of times a response should be repeated.
    • valid only in BURST mode, ignored in RANDOM mode
    • in BURST mode if ignored/missing, the default value is 1
  • Added responseMode field in request schema under imposter schema

    • this new field can have two string values RANDOM or BURST
    • default value is RANDOM
    • any other values will be ignored and default values will be selected instead
    • if the field is missing/ignored, the default value will be considered

Input required:

  • Migration strategy for existing imposter schema to new schema (response -> array of response)
  • Determining default value for responseMode which is currently RANDOM

* Adding logic to have multiple responses for similar request
* Changed imposters json and yaml schema to have array of response instead just response
* Fixing tests according to changes
* Adding TODOs
* Modularizig code
* Added logic for random responses if burst field is absent
* Changing implementation of random mode and burst mode selection
* Updating exisiting and adding new documentation portion as per the feature.
* Ignoring negative or 0 value in the burst mode. Using 1 value in that case.
* Fixing and maintaining name consistencey in the response_handler.go file
@infinite-spectrum infinite-spectrum changed the title Allow setting different responses per request #68 Allow setting different responses per request #31 Jun 24, 2021
* Adding unit tests for repeatable responses
* Removing TODO file
* Changing name of a function
@infinite-spectrum infinite-spectrum marked this pull request as ready for review June 24, 2021 13:53
* Fixing comments
* Fixing comments
* Fixing linting errors
* Fixing edge case of having no responses available
* Added test case for no response available, fixing tests
* Adding test for no response and minor fixes
- `RANDOM` and `BURST` are valid values
- `RANDOM` is default if field is not provided or any other value is provided.
2. `response` -> `burst` (applicable only in `BURST` mode, ignored in)
- +ve integer are valid values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by +ve? 🤔

Copy link
Author

@infinite-spectrum infinite-spectrum Jul 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, +ve means positive. I'll change it to be positive instead of +ve.

Copy link
Member

@joanlopez joanlopez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @infinite-spectrum,

Thanks a lot for your ideas & code contributions. I'll need more time to test the new features and to do an exhaustive code review. However, after a quick review, I have two things to begin the discussion with:

  • What do you think about moving the responseMode to the imposter's level or to make the response field an object with mode and payloads fields within?
  • Have you tested your implementation with concurrent requests? I might be wrong (as I said I had no time to do some tests neither look at it in-detail), but looks like you're increasing the requests counter with no protection against concurrent requests (ergo potential data races).

Any thoughts about both topics? Thanks!! 😊

@joanlopez joanlopez requested a review from aperezg July 1, 2021 07:55
@infinite-spectrum
Copy link
Author

infinite-spectrum commented Jul 5, 2021

@joanlopez

  • What do you think about moving the responseMode to the imposter's level or to make the response field an object with mode and payloads fields within?

>> I'd go with 2nd option rather than the 1st one.

  • Have you tested your implementation with concurrent requests? I might be wrong (as I said I had no time to do some tests neither look at it in-detail), but looks like you're increasing the requests counter with no protection against concurrent requests (ergo potential data races).

>> Nope, this is a miss from my end. I'll incorporate this one.

@joanlopez
Copy link
Member

joanlopez commented Jul 5, 2021

  • What do you think about moving the responseMode to the imposter's level or to make the response field an object with mode and payloads fields within?

I'd go with 2nd option rather than the 1st one.

I'm still wondering about how big that change is. In comparison with tools like Mountebank, in Killgrave is much simpler to define your imposters, so with this kind of change (forcing the user to define an array, etc) we're losing the added value that Killgrave brings into the ecosystem.

That being said, do you think it could work to either:

  • Make the imposters parser work with both schemas (older and newer).
  • Base this new feature in a completely new field (response vs responses or similar).

What do you think? cc/ @aperezg

  • Have you tested your implementation with concurrent requests? I might be wrong (as I said I had no time to do some tests neither look at it in-detail), but looks like you're increasing the requests counter with no protection against concurrent requests (ergo potential data races).

Nope, this is a miss from my end. I'll incorporate this one.

No worries at all, that's why code reviews are for. Feel free to ping me if you have any question related with that.

Again, thanks a lot for your engagement with the project and your valuable contributions 🙏🏻

Using mutex lock for response handler's state change
@@ -54,14 +59,20 @@ func (rh *ResponseHandler) fillDefaults(imposter *Imposter) {
rh.counter = 1
rh.currentInd = 0
}

rh.mutex = new(sync.Mutex)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, there's no need to make the rh.mutex a pointer neither to initialize it since rh is already a pointer to a ResponseHandler. See this example from the Go Tour. Thanks!

@aperezg
Copy link
Member

aperezg commented Oct 12, 2021

hello @infinite-spectrum will you continue with this PR? seems very good contibution so I hope to see this finish :D

apurvasaraiya and others added 2 commits December 7, 2021 18:11
@infinite-spectrum
Copy link
Author

@aperezg, sorry for the late submission. I've fixed the code for concurrent requests. Please review and let me know any suggestions/issues.

@infinite-spectrum
Copy link
Author

@aperezg, we have to discuss one question - How to deal with the schema change? (responses instead of response in the imposter file.)

Here are few ideas:

  • Make current code to work with both imposter schemas. (@joanlopez's idea)
  • Write a one-time utility to convert imposter from previous version to the current one

Any other ideas would be appreciated.
Thank you

@aperezg
Copy link
Member

aperezg commented Feb 22, 2022

Hello @infinite-spectrum, sorry for the late response 🙏 so for continue with the PR, I think that the best idea would be that joan said, support the responses and response :)

@infinite-spectrum infinite-spectrum closed this by deleting the head repository Jul 5, 2023
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 this pull request may close these issues.

4 participants