-
Notifications
You must be signed in to change notification settings - Fork 655
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
Conversation
…eueMockDispatcher). Note: Apple implementation works with a trick: avoiding a freeze() by using the new memory model (experimental).
3874543
to
3fd55fb
Compare
apollo-api/api/apollo-api.api
Outdated
@@ -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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
apollo-mockserver/src/appleMain/kotlin/com/apollographql/apollo3/mockserver/MockServer.kt
Outdated
Show resolved
Hide resolved
apollo-mockserver/src/commonMain/kotlin/com/apollographql/apollo3/mockserver/MockServer.kt
Outdated
Show resolved
Hide resolved
apollo-mockserver/src/commonMain/kotlin/com/apollographql/apollo3/mockserver/MockServer.kt
Outdated
Show resolved
Hide resolved
...rver/src/commonTest/kotlin/com/apollographql/apollo3/mockserver/test/MockServerCommonTest.kt
Outdated
Show resolved
Hide resolved
...rver/src/commonTest/kotlin/com/apollographql/apollo3/mockserver/test/MockServerCommonTest.kt
Outdated
Show resolved
Hide resolved
…pollo's, which was breaking a few tests
…ver back. Freezing the MockDispatcher requires to implement a copy() method.
…lo3/mockserver/MockServer.kt Co-authored-by: Martin Bonnin <martin@mbonnin.net>
...ting-support/src/commonMain/kotlin/com/apollographql/apollo3/testing/ApolloMockDispatcher.kt
Outdated
Show resolved
Hide resolved
...work-transport/src/test/kotlin/testnetworktransport/MapTestNetworkTransportDispatcherTest.kt
Outdated
Show resolved
Hide resolved
...lo-mockserver/src/commonMain/kotlin/com/apollographql/apollo3/mockserver/MockServerCommon.kt
Outdated
Show resolved
Hide resolved
...lo-mockserver/src/commonMain/kotlin/com/apollographql/apollo3/mockserver/MockServerCommon.kt
Outdated
Show resolved
Hide resolved
...kserver/src/commonMain/kotlin/com/apollographql/apollo3/mockserver/QueueMockServerHandler.kt
Outdated
Show resolved
Hide resolved
…ueueMockServerHandler for Apple instead
private val queue = ArrayDeque<ApolloResponse<out Operation.Data>>() | ||
|
||
fun <D : Operation.Data> enqueue(response: ApolloResponse<D>) { | ||
queue.add(response) |
There was a problem hiding this comment.
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.
There was a problem hiding this 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!
Added some documentation here |
…nstead have QueueTestNetworkTransport and MapTestNetworkTransport
Step 1
MockDispatcher
interface which has afun dispatch(request: MockRecordedRequest): MockResponse
and modifyMockServer
to use itQueueMockDispatcher
, make it the default one inMockServer
MockServer.enqueue
delegates to this queue dispatcherCaveat: the Apple implementation of
MockServer
needs tofreeze()
theMockDispatcher
, which prompted me to declare acopy
method on theMockDispatcher
interface - this allows to mutate the copy and re-freeze. Could there be a better way?Step 2
apollo-testing-support
, addApolloMockDispatcher
interface which is analogous toMockDispatcher
but returningApolloResponse
ApolloMockDispatcherBridge
class and afun MockServer(apolloMockDispatcher: ApolloMockDispatcher)
function, it can be used as a regularMockDispatcher
QueueApolloMockDispatcher
: analogous toQueueMockDispatcher
MapApolloMockDispatcher
: has methods to associateOperation
orOperation.Data
toApolloResponse
Step 3
apollo-testing-support
, addTestNetworkTransport
and aTestNetworkTransportDispatcher
interfaceTestNetworkTransportDispatcher
has 2 implementations:QueueTestNetworkTransportDispatcher
MapTestNetworkTransportDispatcher
(the default one)Thoughts
After looking at this, I am not convinced we should keep what was done in Step 2 (
ApolloMockDispatcher
), because:TestNetworkTransport
to be a better fitting (less low-level) API for users wishing to mock ApolloClient responsesMockServer.enqueue(Operation, Data)
extension inapollo-testing-support
which addresses pretty much the same usecase (I saw it later 🙈)MockServer