Skip to content

Commit

Permalink
Revert duplicating tests in websockets project
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Aug 16, 2023
1 parent fddadf6 commit 19f61e0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 118 deletions.
1 change: 0 additions & 1 deletion tests/websockets/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ kotlin {
findByName("commonMain")?.apply {
dependencies {
implementation(libs.apollo.runtime)
implementation(libs.apollo.engine.ktor)
}
}

Expand Down
12 changes: 3 additions & 9 deletions tests/websockets/src/jvmTest/kotlin/CachedSubscriptionTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import com.apollographql.apollo.sample.server.SampleServer
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.cache.normalized.ApolloStore
import com.apollographql.apollo3.cache.normalized.api.MemoryCacheFactory
import com.apollographql.apollo3.cache.normalized.store
import com.apollographql.apollo3.cache.normalized.watch
import com.apollographql.apollo3.network.ws.KtorWebSocketEngine
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
Expand Down Expand Up @@ -38,15 +38,15 @@ class CachedSubscriptionTest {
}
}

private fun subscriptionsCanUpdateTheCache(customizeApolloClientBuilder: ApolloClient.Builder.() -> ApolloClient.Builder) {
@Test
fun subscriptionsCanUpdateTheCache() {
val store = ApolloStore(
MemoryCacheFactory(Int.MAX_VALUE),
)

val apolloClient = ApolloClient.Builder()
.httpServerUrl(sampleServer.graphqlUrl())
.webSocketServerUrl(sampleServer.subscriptionsUrl())
.customizeApolloClientBuilder()
.store(store)
.build()

Expand Down Expand Up @@ -81,10 +81,4 @@ class CachedSubscriptionTest {
job.cancel()
}
}

@Test
fun subscriptionsCanUpdateTheCacheDefault() = subscriptionsCanUpdateTheCache { this }

@Test
fun subscriptionsCanUpdateTheCacheKtor() = subscriptionsCanUpdateTheCache { webSocketEngine(KtorWebSocketEngine()) }
}
11 changes: 2 additions & 9 deletions tests/websockets/src/jvmTest/kotlin/SampleServerCustomTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import com.apollographql.apollo.sample.server.SampleServer
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.network.ws.KtorWebSocketEngine
import com.apollographql.apollo3.network.ws.WebSocketNetworkTransport
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.delay
Expand All @@ -14,7 +13,8 @@ import org.junit.Test
import sample.server.CountSubscription

class SampleServerCustomTest {
private fun websocketReopenWhenDoesNotPile(customizeTransport: WebSocketNetworkTransport.Builder.() -> WebSocketNetworkTransport.Builder) {
@Test
fun websocketReopenWhenDoesNotPile() {

val port = 56678
val url = "http://localhost:$port/subscriptions"
Expand All @@ -34,7 +34,6 @@ class SampleServerCustomTest {
reopenCount++
true
}
.customizeTransport()
.build()
)
.serverUrl("https://unused.com/")
Expand Down Expand Up @@ -71,10 +70,4 @@ class SampleServerCustomTest {
}
sampleServer?.close()
}

@Test
fun websocketReopenWhenDoesNotPileDefault() = websocketReopenWhenDoesNotPile { this }

@Test
fun websocketReopenWhenDoesNotPileKtor() = websocketReopenWhenDoesNotPile { webSocketEngine(KtorWebSocketEngine()) }
}
66 changes: 12 additions & 54 deletions tests/websockets/src/jvmTest/kotlin/SampleServerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import com.apollographql.apollo.sample.server.SampleServer
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.exception.SubscriptionOperationException
import com.apollographql.apollo3.network.ws.KtorWebSocketEngine
import com.apollographql.apollo3.network.ws.SubscriptionWsProtocolAdapter
import com.apollographql.apollo3.network.ws.WebSocketConnection
import com.apollographql.apollo3.network.ws.WebSocketNetworkTransport
Expand Down Expand Up @@ -45,10 +44,10 @@ class SampleServerTest {
}
}

private fun simple(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) {
@Test
fun simple() {
val apolloClient = ApolloClient.Builder()
.serverUrl(sampleServer.subscriptionsUrl())
.customizeClient()
.build()

runBlocking {
Expand All @@ -61,16 +60,9 @@ class SampleServerTest {
}

@Test
fun simpleDefault()= simple { this }

@Test
fun simpleKtor() = simple { webSocketEngine(KtorWebSocketEngine()) }


private fun interleavedSubscriptions(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) {
fun interleavedSubscriptions() {
val apolloClient = ApolloClient.Builder()
.serverUrl(sampleServer.subscriptionsUrl())
.customizeClient()
.build()

runBlocking {
Expand All @@ -93,17 +85,12 @@ class SampleServerTest {
}

@Test
fun interleavedSubscriptionsDefault() = interleavedSubscriptions { this }

@Test
fun interleavedSubscriptionsKtor() = interleavedSubscriptions { webSocketEngine(KtorWebSocketEngine()) }

private fun idleTimeout(customizeTransport: WebSocketNetworkTransport.Builder.() -> WebSocketNetworkTransport.Builder) {
fun idleTimeout() {
val transport = WebSocketNetworkTransport.Builder().serverUrl(
serverUrl = sampleServer.subscriptionsUrl(),
).idleTimeoutMillis(
idleTimeoutMillis = 1000
).customizeTransport().build()
).build()

val apolloClient = ApolloClient.Builder()
.networkTransport(transport)
Expand All @@ -123,13 +110,8 @@ class SampleServerTest {
}

@Test
fun idleTimeoutDefault() = idleTimeout { this }

@Test
fun idleTimeoutKtor() = idleTimeout { webSocketEngine(KtorWebSocketEngine()) }

private fun slowConsumer(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) {
val apolloClient = ApolloClient.Builder().serverUrl(serverUrl = sampleServer.subscriptionsUrl()).customizeClient().build()
fun slowConsumer() {
val apolloClient = ApolloClient.Builder().serverUrl(serverUrl = sampleServer.subscriptionsUrl()).build()

runBlocking {
/**
Expand All @@ -153,17 +135,12 @@ class SampleServerTest {
}

@Test
fun slowConsumerDefault() = slowConsumer { this }

@Test
fun slowConsumerKtor() = slowConsumer { webSocketEngine(KtorWebSocketEngine()) }

private fun serverTermination(customizeTransport: WebSocketNetworkTransport.Builder.() -> WebSocketNetworkTransport.Builder) {
fun serverTermination() {
val transport = WebSocketNetworkTransport.Builder().serverUrl(
serverUrl = sampleServer.subscriptionsUrl(),
).idleTimeoutMillis(
idleTimeoutMillis = 1000
).customizeTransport().build()
).build()

val apolloClient = ApolloClient.Builder()
.networkTransport(transport)
Expand All @@ -184,15 +161,9 @@ class SampleServerTest {
}

@Test
fun serverTerminationDefault() = serverTermination { this }

@Test
fun serverTerminationKtor() = serverTermination { webSocketEngine(KtorWebSocketEngine()) }

private fun operationError(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) {
fun operationError() {
val apolloClient = ApolloClient.Builder()
.serverUrl(sampleServer.subscriptionsUrl())
.customizeClient()
.build()

runBlocking {
Expand All @@ -211,12 +182,6 @@ class SampleServerTest {
}
}

@Test
fun operationErrorDefault() = operationError { this }

@Test
fun operationErrorKtor() = operationError { webSocketEngine(KtorWebSocketEngine()) }

private inline fun <reified T> Any?.cast() = this as T

private object AuthorizationException : Exception()
Expand Down Expand Up @@ -265,16 +230,15 @@ class SampleServerTest {
}
}

private fun canResumeAfterGraphQLError(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) {
@Test
fun canResumeAfterGraphQLError() {
val wsFactory = AuthorizationAwareWsProtocolFactory()
val apolloClient = ApolloClient.Builder()
.serverUrl(sampleServer.subscriptionsUrl())
.wsProtocol(wsFactory)
.webSocketReopenWhen { e, _ ->
println("XXX " + e)
e is AuthorizationException
}
.customizeClient()
.build()

runBlocking {
Expand All @@ -286,10 +250,4 @@ class SampleServerTest {
assertEquals(listOf(0, 0), list)
}
}

@Test
fun canResumeAfterGraphQLErrorDefault() = canResumeAfterGraphQLError { this }

@Test
fun canResumeAfterGraphQLErrorKtor() = canResumeAfterGraphQLError { webSocketEngine(KtorWebSocketEngine()) }
}
52 changes: 7 additions & 45 deletions tests/websockets/src/jvmTest/kotlin/WebSocketErrorsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.apollographql.apollo.sample.server.SampleServer
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.exception.ApolloNetworkException
import com.apollographql.apollo3.exception.ApolloWebSocketClosedException
import com.apollographql.apollo3.network.ws.KtorWebSocketEngine
import com.apollographql.apollo3.network.ws.SubscriptionWsProtocol
import com.apollographql.apollo3.network.ws.closeConnection
import kotlinx.coroutines.async
Expand Down Expand Up @@ -40,15 +39,15 @@ class WebSocketErrorsTest {
}
}

private fun connectionErrorEmitsException(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) = runBlocking {
@Test
fun connectionErrorEmitsException() = runBlocking {
val apolloClient = ApolloClient.Builder()
.serverUrl(sampleServer.subscriptionsUrl())
.wsProtocol(
SubscriptionWsProtocol.Factory(
connectionPayload = { mapOf("return" to "error") }
)
)
.customizeClient()
.build()

apolloClient.subscription(TimeSubscription())
Expand All @@ -63,14 +62,8 @@ class WebSocketErrorsTest {
apolloClient.close()
}


@Test
fun connectionErrorEmitsExceptionDefault() = connectionErrorEmitsException { this }

@Test
fun connectionErrorEmitsExceptionKtor() = connectionErrorEmitsException { webSocketEngine(KtorWebSocketEngine()) }

private fun socketClosedEmitsException(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) = runBlocking {
fun socketClosedEmitsException() = runBlocking {
val apolloClient = ApolloClient.Builder()
.serverUrl(sampleServer.subscriptionsUrl())
.wsProtocol(
Expand All @@ -80,7 +73,6 @@ class WebSocketErrorsTest {
connectionPayload = { mapOf("return" to "close(3666)") }
)
)
.customizeClient()
.build()

apolloClient.subscription(TimeSubscription())
Expand All @@ -96,12 +88,7 @@ class WebSocketErrorsTest {
}

@Test
fun socketClosedEmitsExceptionDefault() = socketClosedEmitsException { this }

@Test
fun socketClosedEmitsExceptionKtor() = socketClosedEmitsException { webSocketEngine(KtorWebSocketEngine()) }

private fun socketReopensAfterAnError(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) = runBlocking {
fun socketReopensAfterAnError() = runBlocking {
var connectionInitCount = 0
var exception: Throwable? = null

Expand All @@ -121,7 +108,6 @@ class WebSocketErrorsTest {
// Only retry once
connectionInitCount == 1
}
.customizeClient()
.build()

val items = async {
Expand Down Expand Up @@ -168,16 +154,10 @@ class WebSocketErrorsTest {
}

@Test
fun socketReopensAfterAnErrorDefault() = socketReopensAfterAnError { this }

@Test
fun socketReopensAfterAnErrorKtor() = socketReopensAfterAnError { webSocketEngine(KtorWebSocketEngine()) }

private fun disposingTheClientClosesTheWebSocket(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) = runBlocking {
fun disposingTheClientClosesTheWebSocket() = runBlocking {
var apolloClient = ApolloClient.Builder()
.httpServerUrl(sampleServer.graphqlUrl())
.webSocketServerUrl(sampleServer.subscriptionsUrl())
.customizeClient()
.build()


Expand All @@ -204,12 +184,7 @@ class WebSocketErrorsTest {
}

@Test
fun disposingTheClientClosesTheWebSocketDefault() = disposingTheClientClosesTheWebSocket { this }

@Test
fun disposingTheClientClosesTheWebSocketKtor() = disposingTheClientClosesTheWebSocket { webSocketEngine(KtorWebSocketEngine()) }

private fun flowThrowsIfNoReconnect(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) = runBlocking {
fun flowThrowsIfNoReconnect() = runBlocking {
val apolloClient = ApolloClient.Builder()
.httpServerUrl(sampleServer.graphqlUrl())
.webSocketServerUrl(sampleServer.subscriptionsUrl())
Expand All @@ -220,7 +195,6 @@ class WebSocketErrorsTest {
}
)
)
.customizeClient()
.build()

launch {
Expand All @@ -246,12 +220,7 @@ class WebSocketErrorsTest {
}

@Test
fun flowThrowsIfNoReconnectDefault() = flowThrowsIfNoReconnect { this }

@Test
fun flowThrowsIfNoReconnectKtor() = flowThrowsIfNoReconnect { webSocketEngine(KtorWebSocketEngine()) }

private fun closeConnectionReconnectsTheWebSocket(customizeClient: ApolloClient.Builder.() -> ApolloClient.Builder) = runBlocking {
fun closeConnectionReconnectsTheWebSocket() = runBlocking {
class MyWebSocketReconnectException : Exception()

var connectionInitCount = 0
Expand All @@ -270,7 +239,6 @@ class WebSocketErrorsTest {
assertIs<MyWebSocketReconnectException>(e)
true
}
.customizeClient()
.build()

apolloClient.subscription(CountSubscription(2, 100))
Expand All @@ -290,10 +258,4 @@ class WebSocketErrorsTest {
// connectionInitCount is 2 since we returned true in webSocketReopenWhen
assertEquals(2, connectionInitCount)
}

@Test
fun closeConnectionReconnectsTheWebSocketDefault() = closeConnectionReconnectsTheWebSocket { this }

@Test
fun closeConnectionReconnectsTheWebSocketKtor() = closeConnectionReconnectsTheWebSocket { webSocketEngine(KtorWebSocketEngine()) }
}

0 comments on commit 19f61e0

Please sign in to comment.