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

MockServer improvements and TestNetworkTransport #3757

Merged
merged 25 commits into from
Jan 10, 2022
Merged

Conversation

BoD
Copy link
Contributor

@BoD BoD commented Dec 23, 2021

Step 1

  • 0f83009
  • Add MockDispatcher interface which has a fun dispatch(request: MockRecordedRequest): MockResponse and modify MockServer to use it
  • Add an implementation QueueMockDispatcher, make it the default one in MockServer
  • Now MockServer.enqueue delegates to this queue dispatcher

Caveat: the Apple implementation of MockServer needs to freeze() the MockDispatcher, which prompted me to declare a copy method on the MockDispatcher interface - this allows to mutate the copy and re-freeze. Could there be a better way?

Step 2

  • 28a676f
  • In apollo-testing-support, add ApolloMockDispatcher interface which is analogous to MockDispatcher but returning ApolloResponse
  • Thanks to a ApolloMockDispatcherBridge class and a fun MockServer(apolloMockDispatcher: ApolloMockDispatcher) function, it can be used as a regular MockDispatcher
  • Add 2 implementations:
    • QueueApolloMockDispatcher : analogous to QueueMockDispatcher
    • MapApolloMockDispatcher : has methods to associate Operation or Operation.Data to ApolloResponse

Step 3

  • 3874543
  • In apollo-testing-support, add TestNetworkTransport and a TestNetworkTransportDispatcher interface
  • TestNetworkTransportDispatcher has 2 implementations:
    • QueueTestNetworkTransportDispatcher
    • MapTestNetworkTransportDispatcher (the default one)
  • This is basically what was described in TestNetworkTransport #3737, minus the “magic” to have the ability to pass a test builder lambda

Thoughts

After looking at this, I am not convinced we should keep what was done in Step 2 (ApolloMockDispatcher), because:

  • I find the TestNetworkTransport to be a better fitting (less low-level) API for users wishing to mock ApolloClient responses
  • For our own internal tests, there was already a MockServer.enqueue(Operation, Data) extension in apollo-testing-support which addresses pretty much the same usecase (I saw it later 🙈)
  • With the dispatcher approach you can’t both enqueue a string and an Operation on the same MockServer

…eueMockDispatcher).

Note: Apple implementation works with a trick: avoiding a freeze() by using the new memory model (experimental).
@@ -101,6 +101,12 @@ public final class com/apollographql/apollo3/api/ApolloResponse$Builder {
public final fun requestUuid (Ljava/util/UUID;)Lcom/apollographql/apollo3/api/ApolloResponse$Builder;
}

public final class com/apollographql/apollo3/api/ApolloResponsesKt {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use @JvmName("ApolloResponses") to make Java interop nicer?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also what about merging with ResponseParser and have something like ApolloResponseAdapter that can adapt to//from json like we do for other models?

BoD and others added 4 commits January 4, 2022 14:43
…ver back.

Freezing the MockDispatcher requires to implement a copy() method.
…lo3/mockserver/MockServer.kt

Co-authored-by: Martin Bonnin <martin@mbonnin.net>
private val queue = ArrayDeque<ApolloResponse<out Operation.Data>>()

fun <D : Operation.Data> enqueue(response: ApolloResponse<D>) {
queue.add(response)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this needs to be locked. On native it doesn't throw because we run the flow on the main thread always but it might not always be the case. And on the JVM we cannot make any assumption about what dispatcher is going to be used.

Copy link
Contributor

@martinbonnin martinbonnin left a comment

Choose a reason for hiding this comment

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

One last concurrency comment but looks great 🤩 . That'll be a nice addition!

@BoD
Copy link
Contributor Author

BoD commented Jan 5, 2022

Added some documentation here

@BoD BoD marked this pull request as ready for review January 10, 2022 10:14
…nstead have QueueTestNetworkTransport and MapTestNetworkTransport
@BoD BoD merged commit f476e02 into main Jan 10, 2022
@BoD BoD deleted the mockserver-improvements branch January 10, 2022 17:11
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.

2 participants