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

Denormalize "Code", slight simplification of HTTP interface #224

Merged
merged 1 commit into from
Feb 23, 2024
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 @@ -386,9 +386,6 @@ class Client(
private fun unaryResult(numUnsent: Int, result: ResponseMessage<out MessageLite>): ClientResponseResult {
return when (result) {
is ResponseMessage.Success -> {
if (result.code != Code.OK) {
throw RuntimeException("RPC was successful but ended with non-OK code ${result.code}")
}
ClientResponseResult(
headers = result.headers,
payloads = listOf(payloadExtractor(result.message)),
Expand All @@ -397,9 +394,6 @@ class Client(
)
}
is ResponseMessage.Failure -> {
if (result.code != result.cause.code) {
throw RuntimeException("RPC result has mismatching codes: ${result.code} != ${result.cause.code}")
}
ClientResponseResult(
headers = result.headers,
error = result.cause,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.connectrpc.http.HTTPRequest
import com.connectrpc.http.HTTPResponse
import com.connectrpc.http.Stream
import com.connectrpc.http.UnaryHTTPRequest
import com.connectrpc.http.clone
import okio.Buffer

internal class TracingHTTPClient(
Expand All @@ -36,18 +37,9 @@ internal class TracingHTTPClient(
printer.println("Failed to receive HTTP response (${buffer.size} bytes): ${response.cause!!.message.orEmpty()}")
printer.indent().println(response.cause!!.stackTraceToString())
} else {
printer.println("Received HTTP response (${buffer.size} bytes): ${response.tracingInfo?.httpStatus ?: "???"}")
printer.println("Received HTTP response (${buffer.size} bytes): ${response.status ?: "???"}")
}
onResult(
HTTPResponse(
code = response.code,
headers = response.headers,
message = buffer,
trailers = response.trailers,
tracingInfo = response.tracingInfo,
cause = response.cause,
),
)
onResult(response.clone(message = buffer))
}
return {
printer.println("Canceling HTTP request...")
Expand All @@ -71,9 +63,9 @@ internal class TracingHTTPClient(
}
is StreamResult.Complete -> {
if (result.cause != null) {
printer.printlnWithStackTrace("Failed to complete HTTP response (code=${result.code}): ${result.cause!!.message.orEmpty()}")
printer.printlnWithStackTrace("Failed to complete HTTP response (code=${result.cause!!.code}): ${result.cause!!.message.orEmpty()}")
} else {
printer.printlnWithStackTrace("Received HTTP response completion: code=${result.code}")
printer.printlnWithStackTrace("Received successful HTTP response completion")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ abstract class ClientStreamClient<Req : MessageLite, Resp : MessageLite>(
val resp = underlying.receiveAndClose()
return ResponseMessage.Success(
message = resp,
code = Code.OK,
headers = underlying.responseHeaders().await(),
trailers = underlying.responseTrailers().await(),
)
Expand All @@ -108,7 +107,6 @@ abstract class ClientStreamClient<Req : MessageLite, Resp : MessageLite>(
}
return ResponseMessage.Failure(
cause = connectException,
code = connectException.code,
headers = underlying.responseHeaders().await(),
trailers = connectException.metadata,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package com.connectrpc.conformance.java

import com.connectrpc.Code
import com.connectrpc.ConnectException
import com.connectrpc.ResponseMessage
import com.connectrpc.conformance.BaseConformanceTest
import com.connectrpc.conformance.ServerType
import com.connectrpc.conformance.v1.ErrorDetail
Expand Down Expand Up @@ -199,7 +200,6 @@ class ConformanceTest(
}
val countDownLatch = CountDownLatch(1)
testServiceConnectClient.unaryCall(message, headers) { response ->
assertThat(response.code).isEqualTo(Code.OK)
assertThat(response.headers[leadingKey]).containsExactly(leadingValue)
assertThat(response.trailers[trailingKey]).containsExactly(b64Encode(trailingValue))
response.failure {
Expand All @@ -224,10 +224,9 @@ class ConformanceTest(
}
val countDownLatch = CountDownLatch(1)
testServiceConnectClient.unaryCall(message) { response ->
assertThat(response.code).isEqualTo(Code.UNKNOWN)
response.failure { errorResponse ->
assertThat(errorResponse.cause).isNotNull()
assertThat(errorResponse.code).isEqualTo(Code.UNKNOWN)
assertThat(errorResponse.cause.code).isEqualTo(Code.UNKNOWN)
assertThat(errorResponse.cause.message).isEqualTo("test status message")
countDownLatch.countDown()
}
Expand Down Expand Up @@ -292,7 +291,6 @@ class ConformanceTest(
response.failure { errorResponse ->
val error = errorResponse.cause
assertThat(error.code).isEqualTo(Code.UNKNOWN)
assertThat(response.code).isEqualTo(Code.UNKNOWN)
assertThat(error.message).isEqualTo(statusMessage)
countDownLatch.countDown()
}
Expand All @@ -308,7 +306,8 @@ class ConformanceTest(
fun unimplementedMethod(): Unit = runBlocking {
val countDownLatch = CountDownLatch(1)
testServiceConnectClient.unimplementedCall(empty {}) { response ->
assertThat(response.code).isEqualTo(Code.UNIMPLEMENTED)
assertThat(response).isInstanceOf(ResponseMessage.Failure::class.java)
response.failure { assertThat(it.cause.code).isEqualTo(Code.UNIMPLEMENTED) }
countDownLatch.countDown()
}
countDownLatch.await(500, TimeUnit.MILLISECONDS)
Expand All @@ -319,7 +318,8 @@ class ConformanceTest(
fun unimplementedService(): Unit = runBlocking {
val countDownLatch = CountDownLatch(1)
unimplementedServiceClient.unimplementedCall(empty {}) { response ->
assertThat(response.code).isEqualTo(Code.UNIMPLEMENTED)
assertThat(response).isInstanceOf(ResponseMessage.Failure::class.java)
response.failure { assertThat(it.cause.code).isEqualTo(Code.UNIMPLEMENTED) }
countDownLatch.countDown()
}
countDownLatch.await(500, TimeUnit.MILLISECONDS)
Expand Down Expand Up @@ -356,7 +356,6 @@ class ConformanceTest(
}
val countDownLatch = CountDownLatch(1)
testServiceConnectClient.failUnaryCall(simpleRequest {}) { response ->
assertThat(response.code).isEqualTo(Code.RESOURCE_EXHAUSTED)
response.failure { errorResponse ->
val error = errorResponse.cause
assertThat(error.code).isEqualTo(Code.RESOURCE_EXHAUSTED)
Expand Down Expand Up @@ -419,7 +418,6 @@ class ConformanceTest(
payload = payload { body = ByteString.copyFrom(ByteArray(size)) }
}
val response = testServiceConnectClient.unaryCallBlocking(message, headers).execute()
assertThat(response.code).isEqualTo(Code.OK)
assertThat(response.headers[leadingKey]).containsExactly(leadingValue)
assertThat(response.trailers[trailingKey]).containsExactly(b64Encode(trailingValue))
response.failure {
Expand All @@ -439,10 +437,9 @@ class ConformanceTest(
}
}
val response = testServiceConnectClient.unaryCallBlocking(message).execute()
assertThat(response.code).isEqualTo(Code.UNKNOWN)
response.failure { errorResponse ->
assertThat(errorResponse.cause).isNotNull()
assertThat(errorResponse.code).isEqualTo(Code.UNKNOWN)
assertThat(errorResponse.cause.code).isEqualTo(Code.UNKNOWN)
assertThat(errorResponse.cause.message).isEqualTo("test status message")
}
response.success {
Expand All @@ -465,7 +462,6 @@ class ConformanceTest(
response.failure { errorResponse ->
val error = errorResponse.cause
assertThat(error.code).isEqualTo(Code.UNKNOWN)
assertThat(response.code).isEqualTo(Code.UNKNOWN)
assertThat(error.message).isEqualTo(statusMessage)
}
response.success {
Expand All @@ -476,13 +472,15 @@ class ConformanceTest(
@Test
fun unimplementedMethodBlocking(): Unit = runBlocking {
val response = testServiceConnectClient.unimplementedCallBlocking(empty {}).execute()
assertThat(response.code).isEqualTo(Code.UNIMPLEMENTED)
assertThat(response).isInstanceOf(ResponseMessage.Failure::class.java)
response.failure { assertThat(it.cause.code).isEqualTo(Code.UNIMPLEMENTED) }
}

@Test
fun unimplementedServiceBlocking(): Unit = runBlocking {
val response = unimplementedServiceClient.unimplementedCallBlocking(empty {}).execute()
assertThat(response.code).isEqualTo(Code.UNIMPLEMENTED)
assertThat(response).isInstanceOf(ResponseMessage.Failure::class.java)
response.failure { assertThat(it.cause.code).isEqualTo(Code.UNIMPLEMENTED) }
}

@Test
Expand All @@ -492,7 +490,6 @@ class ConformanceTest(
domain = "connect-conformance"
}
val response = testServiceConnectClient.failUnaryCallBlocking(simpleRequest {}).execute()
assertThat(response.code).isEqualTo(Code.RESOURCE_EXHAUSTED)
response.failure { errorResponse ->
val error = errorResponse.cause
assertThat(error.code).isEqualTo(Code.RESOURCE_EXHAUSTED)
Expand Down Expand Up @@ -562,7 +559,6 @@ class ConformanceTest(
}
val countDownLatch = CountDownLatch(1)
testServiceConnectClient.unaryCall(message, headers) { response ->
assertThat(response.code).isEqualTo(Code.OK)
assertThat(response.headers[leadingKey]).containsExactly(leadingValue)
assertThat(response.trailers[trailingKey]).containsExactly(b64Encode(trailingValue))
response.failure {
Expand All @@ -587,10 +583,9 @@ class ConformanceTest(
}
val countDownLatch = CountDownLatch(1)
testServiceConnectClient.unaryCall(message) { response ->
assertThat(response.code).isEqualTo(Code.UNKNOWN)
response.failure { errorResponse ->
assertThat(errorResponse.cause).isNotNull()
assertThat(errorResponse.code).isEqualTo(Code.UNKNOWN)
assertThat(errorResponse.cause.code).isEqualTo(Code.UNKNOWN)
assertThat(errorResponse.cause.message).isEqualTo("test status message")
countDownLatch.countDown()
}
Expand Down Expand Up @@ -619,7 +614,6 @@ class ConformanceTest(
response.failure { errorResponse ->
val error = errorResponse.cause
assertThat(error.code).isEqualTo(Code.UNKNOWN)
assertThat(response.code).isEqualTo(Code.UNKNOWN)
assertThat(error.message).isEqualTo(statusMessage)
countDownLatch.countDown()
}
Expand All @@ -635,7 +629,8 @@ class ConformanceTest(
fun unimplementedMethodCallback(): Unit = runBlocking {
val countDownLatch = CountDownLatch(1)
testServiceConnectClient.unimplementedCall(empty {}) { response ->
assertThat(response.code).isEqualTo(Code.UNIMPLEMENTED)
assertThat(response).isInstanceOf(ResponseMessage.Failure::class.java)
response.failure { assertThat(it.cause.code).isEqualTo(Code.UNIMPLEMENTED) }
countDownLatch.countDown()
}
countDownLatch.await(500, TimeUnit.MILLISECONDS)
Expand All @@ -646,7 +641,8 @@ class ConformanceTest(
fun unimplementedServiceCallback(): Unit = runBlocking {
val countDownLatch = CountDownLatch(1)
unimplementedServiceClient.unimplementedCall(empty {}) { response ->
assertThat(response.code).isEqualTo(Code.UNIMPLEMENTED)
assertThat(response).isInstanceOf(ResponseMessage.Failure::class.java)
response.failure { assertThat(it.cause.code).isEqualTo(Code.UNIMPLEMENTED) }
countDownLatch.countDown()
}
countDownLatch.await(500, TimeUnit.MILLISECONDS)
Expand All @@ -661,7 +657,6 @@ class ConformanceTest(
}
val countDownLatch = CountDownLatch(1)
testServiceConnectClient.failUnaryCall(simpleRequest {}) { response ->
assertThat(response.code).isEqualTo(Code.RESOURCE_EXHAUSTED)
response.failure { errorResponse ->
val error = errorResponse.cause
assertThat(error.code).isEqualTo(Code.RESOURCE_EXHAUSTED)
Expand Down
Loading
Loading