Skip to content

[plugin] change tasks HttpClient engine from CIO to Apache #1162

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

Merged
merged 1 commit into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ description = "GraphQL Kotlin common utilities to generate a client."

val compileTestingVersion: String by project
val graphQLJavaVersion: String by project
val jacksonVersion: String by project
val junitVersion: String by project
val kotlinPoetVersion: String by project
val kotlinxSerializationVersion: String by project
Expand All @@ -15,8 +16,12 @@ dependencies {
}
api("com.squareup:kotlinpoet:$kotlinPoetVersion")
api("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxSerializationVersion")
implementation("io.ktor:ktor-client-cio:$ktorVersion")
implementation("io.ktor:ktor-client-jackson:$ktorVersion")
implementation("io.ktor:ktor-client-apache:$ktorVersion")
implementation("io.ktor:ktor-client-jackson:$ktorVersion") {
exclude("com.fasterxml.jackson.core", "jackson-databind")
exclude("com.fasterxml.jackson.module", "jackson-module-kotlin")
}
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
testImplementation(project(path = ":graphql-kotlin-client-jackson"))
testImplementation("com.github.tomakehurst:wiremock-jre8:$wireMockVersion")
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:$compileTestingVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package com.expediagroup.graphql.plugin.client

import graphql.schema.idl.SchemaParser
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.engine.cio.endpoint
import io.ktor.client.engine.apache.Apache
import io.ktor.client.features.ClientRequestException
import io.ktor.client.features.HttpRequestTimeoutException
import io.ktor.client.features.HttpTimeout
import io.ktor.client.request.get
import io.ktor.client.request.header
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.runBlocking
import java.net.UnknownHostException

/**
* Downloads GraphQL SDL from the specified endpoint and verifies whether the result is a valid GraphQL schema.
Expand All @@ -34,12 +35,10 @@ fun downloadSchema(
httpHeaders: Map<String, Any> = emptyMap(),
connectTimeout: Long = 5_000,
readTimeout: Long = 15_000
): String = HttpClient(engineFactory = CIO) {
engine {
this.requestTimeout = readTimeout
endpoint {
this.connectTimeout = connectTimeout
}
): String = HttpClient(engineFactory = Apache) {
install(HttpTimeout) {
connectTimeoutMillis = connectTimeout
requestTimeoutMillis = readTimeout
}
}.use { client ->
runBlocking {
Expand All @@ -51,7 +50,7 @@ fun downloadSchema(
}
} catch (e: Throwable) {
when (e) {
is ClientRequestException, is TimeoutCancellationException -> throw e
is ClientRequestException, is HttpRequestTimeoutException, is UnknownHostException -> throw e
else -> throw RuntimeException("Unable to download SDL from specified endpoint=$endpoint", e)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ package com.expediagroup.graphql.plugin.client
import graphql.introspection.IntrospectionResultToSchema
import graphql.schema.idl.SchemaPrinter
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.engine.cio.endpoint
import io.ktor.client.engine.apache.Apache
import io.ktor.client.features.ClientRequestException
import io.ktor.client.features.HttpRequestTimeoutException
import io.ktor.client.features.HttpTimeout
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.request.header
import io.ktor.client.request.post
import io.ktor.client.request.url
import io.ktor.http.ContentType
import io.ktor.http.contentType
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.runBlocking
import java.net.UnknownHostException

private const val INTROSPECTION_QUERY =
"""
Expand Down Expand Up @@ -131,12 +132,10 @@ fun introspectSchema(
httpHeaders: Map<String, Any> = emptyMap(),
connectTimeout: Long = 5_000,
readTimeout: Long = 15_000
): String = HttpClient(engineFactory = CIO) {
engine {
requestTimeout = readTimeout
endpoint {
this.connectTimeout = connectTimeout
}
): String = HttpClient(engineFactory = Apache) {
install(HttpTimeout) {
connectTimeoutMillis = connectTimeout
requestTimeoutMillis = readTimeout
}
install(feature = JsonFeature)
}.use { client ->
Expand All @@ -155,7 +154,7 @@ fun introspectSchema(
}
} catch (e: Throwable) {
when (e) {
is ClientRequestException, is TimeoutCancellationException -> throw e
is ClientRequestException, is HttpRequestTimeoutException, is UnknownHostException -> throw e
else -> throw RuntimeException("Unable to run introspection query against the specified endpoint=$endpoint", e)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import com.github.tomakehurst.wiremock.client.WireMock.stubFor
import com.github.tomakehurst.wiremock.core.WireMockConfiguration
import graphql.schema.idl.errors.SchemaProblem
import io.ktor.client.features.ClientRequestException
import io.ktor.client.features.HttpRequestTimeoutException
import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.nio.channels.UnresolvedAddressException
import java.net.UnknownHostException
import kotlin.test.assertEquals
import kotlin.test.assertTrue

Expand Down Expand Up @@ -97,12 +97,11 @@ class DownloadSchemaTest {
@Test
@KtorExperimentalAPI
fun `verify downloadSchema will throw exception if URL is not valid`() {
val exception = assertThrows<RuntimeException> {
assertThrows<UnknownHostException> {
runBlocking {
downloadSchema("https://non-existent-graphql-url.com/should_404")
}
}
assertTrue(exception.cause is UnresolvedAddressException)
}

@Test
Expand Down Expand Up @@ -147,7 +146,7 @@ class DownloadSchemaTest {
.withFixedDelay(1_000)
)
)
assertThrows<TimeoutCancellationException> {
assertThrows<HttpRequestTimeoutException> {
runBlocking {
downloadSchema(endpoint = "${wireMockServer.baseUrl()}/sdl", connectTimeout = 100, readTimeout = 100)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.core.WireMockConfiguration
import io.ktor.client.features.ClientRequestException
import io.ktor.client.features.HttpRequestTimeoutException
import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.io.BufferedReader
import java.net.UnknownHostException
import kotlin.test.assertEquals

class IntrospectSchemaTest {
Expand Down Expand Up @@ -129,13 +130,23 @@ class IntrospectSchemaTest {
.withFixedDelay(1_000)
)
)
assertThrows<TimeoutCancellationException> {
assertThrows<HttpRequestTimeoutException> {
runBlocking {
introspectSchema(endpoint = "${wireMockServer.baseUrl()}/graphql", connectTimeout = 100, readTimeout = 100)
}
}
}

@Test
@KtorExperimentalAPI
fun `verify introspectSchema will throw exception if URL is not valid`() {
assertThrows<UnknownHostException> {
runBlocking {
downloadSchema("https://non-existent-graphql-url.com/should_404")
}
}
}

companion object {
private val wireMockServer: WireMockServer = WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ class GraphQLDownloadSDLTaskIT : GraphQLGradlePluginAbstractIT() {
.withArguments(DOWNLOAD_SDL_TASK_NAME, "--stacktrace")
.buildAndFail()

println(result.output)
assertEquals(TaskOutcome.FAILED, result.task(":$DOWNLOAD_SDL_TASK_NAME")?.outcome)
assertTrue(result.output.contains("Timed out waiting for 100 ms", ignoreCase = true))
assertTrue(result.output.contains("Request timeout has been expired", ignoreCase = true))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ class GraphQLIntrospectSchemaTaskIT : GraphQLGradlePluginAbstractIT() {
.buildAndFail()

assertEquals(TaskOutcome.FAILED, result.task(":$INTROSPECT_SCHEMA_TASK_NAME")?.outcome)
assertTrue(result.output.contains("Timed out waiting for 100 ms", ignoreCase = true))
assertTrue(result.output.contains("Request timeout has been expired", ignoreCase = true))
}
}