Skip to content

Commit ebd7a64

Browse files
committed
grpc: Add multi interceptor tests
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 63c0b5e commit ebd7a64

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

grpc/grpc-core/src/commonTest/kotlin/kotlinx/rpc/grpc/test/proto/ClientInterceptorTest.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,57 @@ class ClientInterceptorTest : GrpcProtoTest() {
176176
assertEquals("Cancellation cause", error.cause?.message)
177177
}
178178

179+
@Test
180+
fun `cancel in two interceptors - should fail with cancellation`() {
181+
val error = assertFailsWith<StatusException> {
182+
val interceptor1 = interceptor {
183+
onClose { _, _ -> cancel("[1] Canceling in onClose", IllegalStateException("Cancellation cause")) }
184+
proceed(it)
185+
}
186+
val interceptor2 = interceptor {
187+
onClose { _, _ -> cancel("[2] Canceling in onClose", IllegalStateException("Cancellation cause")) }
188+
proceed(it)
189+
}
190+
runGrpcTest(clientInterceptors = interceptor1 + interceptor2, test = ::unaryCall)
191+
}
192+
193+
assertEquals(StatusCode.CANCELLED, error.getStatus().statusCode)
194+
assertContains(error.message!!, "[1] Canceling in onClose")
195+
assertIs<IllegalStateException>(error.cause)
196+
assertEquals("Cancellation cause", error.cause?.message)
197+
}
198+
199+
@Test
200+
fun `cancel in two interceptors withing response stream - should fail with cancellation`() {
201+
val error = assertFailsWith<StatusException> {
202+
val interceptor1 = interceptor {
203+
proceed(it).map {
204+
val msg = it as EchoResponse
205+
if (msg.message == "Echo-3") {
206+
cancel("[1] Canceling in response flow", IllegalStateException("Cancellation cause"))
207+
}
208+
it
209+
}
210+
}
211+
val interceptor2 = interceptor {
212+
proceed(it).map {
213+
val msg = it as EchoResponse
214+
// this is cancelled before the first one
215+
if (msg.message == "Echo-2") {
216+
cancel("[2] Canceling in response flow", IllegalStateException("Cancellation cause"))
217+
}
218+
it
219+
}
220+
}
221+
runGrpcTest(clientInterceptors = interceptor1 + interceptor2, test = ::bidiStream)
222+
}
223+
224+
assertEquals(StatusCode.CANCELLED, error.getStatus().statusCode)
225+
assertContains(error.message!!, "[2] Canceling in response flow")
226+
assertIs<IllegalStateException>(error.cause)
227+
assertEquals("Cancellation cause", error.cause?.message)
228+
}
229+
179230
@Test
180231
fun `modify request message - should return modified message`() {
181232
val interceptor = interceptor {

grpc/grpc-core/src/commonTest/kotlin/kotlinx/rpc/grpc/test/proto/ServerInterceptorTest.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ class ServerInterceptorTest : GrpcProtoTest() {
3535
registerService<EchoService> { EchoServiceImpl() }
3636
}
3737

38+
@Test
39+
fun `throw during onClosing - should fail propagate the exception to the server root`() {
40+
val error = assertFailsWith<IllegalStateException> {
41+
val interceptor = interceptor {
42+
onClose { _, _ -> throw IllegalStateException("Illegal failing in onClose") }
43+
proceed(it)
44+
}
45+
runGrpcTest(serverInterceptors = interceptor, test = ::unaryCall)
46+
}
47+
48+
assertContains(error.message!!, "Illegal failing in onClose")
49+
// check that the error is indeed causing a server crash
50+
assertContains(error.stackTraceToString(), "suspendServerCall")
51+
}
52+
3853
@Test
3954
fun `throw during intercept - should fail with unknown status on client`() {
4055
var cause: Throwable? = null
@@ -112,6 +127,24 @@ class ServerInterceptorTest : GrpcProtoTest() {
112127
assertContains(error.message!!, "Close in onClose")
113128
}
114129

130+
@Test
131+
fun `close in two interceptors - should fail with correct status on client`() {
132+
val error = assertFailsWith<StatusException> {
133+
val interceptor1 = interceptor {
134+
onClose { _, _ -> close(Status(StatusCode.UNAUTHENTICATED, "[1] Close in onClose"), GrpcMetadata()) }
135+
proceed(it)
136+
}
137+
val interceptor2 = interceptor {
138+
onClose { _, _ -> close(Status(StatusCode.UNAUTHENTICATED, "[2] Close in onClose"), GrpcMetadata()) }
139+
proceed(it)
140+
}
141+
runGrpcTest(serverInterceptors = interceptor1 + interceptor2, test = ::unaryCall)
142+
}
143+
144+
assertEquals(StatusCode.UNAUTHENTICATED, error.getStatus().statusCode)
145+
assertContains(error.message!!, "[1] Close in onClose")
146+
}
147+
115148
@Test
116149
fun `dont proceed and return custom message - should succeed on client`() {
117150
val interceptor = interceptor {

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/NativeClientCall.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ internal class NativeClientCall<Request, Response>(
154154
*/
155155
private fun turnReady() {
156156
if (ready.compareAndSet(expect = false, update = true)) {
157-
safeUserCode("Failed to call onClose.") {
157+
safeUserCode("Failed to call onReady.") {
158158
listener?.onReady()
159159
}
160160
}
@@ -200,6 +200,7 @@ internal class NativeClientCall<Request, Response>(
200200
callResult.future.onComplete { success ->
201201
try {
202202
if (success) {
203+
// if the batch doesn't succeed, this is reflected in the recv status op batch.
203204
onSuccess()
204205
}
205206
} finally {

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/NativeServerCall.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ internal class NativeServerCall<Request, Response>(
291291

292292
override fun close(status: Status, trailers: GrpcMetadata) {
293293
check(initialized) { internalError("Call not initialized") }
294-
closed = true
295294

296295
val arena = Arena()
297296

@@ -320,6 +319,7 @@ internal class NativeServerCall<Request, Response>(
320319
if (details != null) grpc_slice_unref(details)
321320
arena.clear()
322321
}) {
322+
closed = true
323323
// nothing to do here
324324
}
325325
}

0 commit comments

Comments
 (0)