Skip to content

Commit

Permalink
Speed up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Aug 8, 2023
1 parent f54e587 commit 7c775f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import com.apollographql.apollo3.mpp.currentTimeMillis
import com.apollographql.apollo3.network.http.DefaultHttpEngine
import com.apollographql.apollo3.network.http.get
import com.apollographql.apollo3.testing.internal.runTest
import platform.CFNetwork.kCFErrorDomainCFNetwork
import platform.CFNetwork.kCFErrorHTTPSProxyConnectionFailure
import platform.Foundation.CFBridgingRelease
import platform.Foundation.NSError
import platform.Foundation.NSURLErrorCannotFindHost
import platform.Foundation.NSURLErrorCannotConnectToHost
import platform.Foundation.NSURLErrorDomain
import platform.Foundation.NSURLErrorTimedOut
import kotlin.test.Test
Expand All @@ -26,7 +23,11 @@ import kotlin.test.fail
class HttpEngineTest {
@Test
fun canReadNSError() = runTest {
val apolloClient = ApolloClient.Builder().serverUrl("https://inexistent.host/graphql").build()
/**
* Try to trigger an error wihtout having to wait 60s on a timeout.
* Hopefully Port 1 is closed on most machines. If that's not enough, we'd need to find an instrumented server
*/
val apolloClient = ApolloClient.Builder().serverUrl("https://127.0.0.1:1/graphql").build()

val response = apolloClient.query(HeroNameQuery()).execute()
val apolloNetworkException = response.exception
Expand All @@ -38,15 +39,8 @@ class HttpEngineTest {
// assertIs<NSError>(cause)
check(cause is NSError)

assertTrue(
when {
// Happens locally if a proxy is running
cause.domain == (CFBridgingRelease(kCFErrorDomainCFNetwork) as String) && cause.code == kCFErrorHTTPSProxyConnectionFailure.toLong() -> true
// Default case
cause.domain == NSURLErrorDomain && cause.code == NSURLErrorCannotFindHost -> true
else -> false
}
)
assertEquals(cause.domain, NSURLErrorDomain)
assertEquals(cause.code, NSURLErrorCannotConnectToHost)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class BenchmarksTest {
repeat(MEASUREMENT_COUNT) {
durations.add(
measureTime {
repeat(EXECUTION_PER_MEASUREMENT) { test(it) }
repeat(EXECUTION_PER_MEASUREMENT) {
test(it)
}
}
)
}
Expand Down Expand Up @@ -83,8 +85,8 @@ class BenchmarksTest {
)

companion object {
private const val EXECUTION_PER_MEASUREMENT = 500
private const val MEASUREMENT_COUNT = 10
private const val EXECUTION_PER_MEASUREMENT = 100
private const val MEASUREMENT_COUNT = 4

val measurements = mutableListOf<Measurement>()

Expand Down

0 comments on commit 7c775f8

Please sign in to comment.