Skip to content

Feature: Support for multiple instances of HTTPMock#4

Merged
bstien merged 12 commits intomainfrom
feature/multiple-instances
Aug 31, 2025
Merged

Feature: Support for multiple instances of HTTPMock#4
bstien merged 12 commits intomainfrom
feature/multiple-instances

Conversation

@bstien
Copy link
Owner

@bstien bstien commented Aug 31, 2025

Why?

One painpoint in the current setup is that there can only exist one instance of HTTPMock at 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 using HTTPMock in sequence.

How

With this new setup the user can create several instances of HTTPMock, each with their own queue and URLSession. The way this works is to assign a known identifier (UUID) to both HTTPMock and its URLSession. The singleton HTTPMock.shared still exists for convenience.

HTTPMockURLProtocol's queue looks like this:

Before After
[Key: [MockResponse]] [UUID: [Key: [MockResponse]]]

When a request comes in HTTPMockURLProtocol will look at the identifier on the URLSessionTask's URLSession and 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?

  • Allow HTTPMock to accept an identifier on instantiation.
  • Assign this identifier to the instance's URLSession.
  • Improve HTTPMockURLProtocol to identify which queue to use, based on this identifier.
    • Coming as a parameter when inserting/removing.
    • Reading from URLSession on incoming request.
  • Update/add tests and let them run in parallel.

@bstien bstien merged commit f6cd70a into main Aug 31, 2025
1 check passed
@bstien bstien deleted the feature/multiple-instances branch August 31, 2025 10:04
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.

1 participant