Feature: Support for multiple instances of HTTPMock#4
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
One painpoint in the current setup is that there can only exist one instance of
HTTPMockat a time, because of the way the response queues are handled. This is a pretty big limit, especially when it forces us to run tests usingHTTPMockin sequence.How
With this new setup the user can create several instances of
HTTPMock, each with their own queue andURLSession. The way this works is to assign a known identifier (UUID) to bothHTTPMockand itsURLSession. The singletonHTTPMock.sharedstill exists for convenience.HTTPMockURLProtocol's queue looks like this:[Key: [MockResponse]][UUID: [Key: [MockResponse]]]When a request comes in
HTTPMockURLProtocolwill look at the identifier on theURLSessionTask'sURLSessionand use this to look up the queue in question.When registering new responses the identifier has to be provided, so the responses are placed in the correct queue.
To avoid race conditions all reads/writes to/from the queue now happens within a lock.
What?
HTTPMockto accept an identifier on instantiation.URLSession.HTTPMockURLProtocolto identify which queue to use, based on this identifier.URLSessionon incoming request.