|
| 1 | +package io.grpc.kotlin |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import com.google.common.truth.extensions.proto.ProtoTruth |
| 5 | +import io.grpc.CallOptions |
| 6 | +import io.grpc.Channel |
| 7 | +import io.grpc.ClientCall |
| 8 | +import io.grpc.ClientInterceptor |
| 9 | +import io.grpc.ClientInterceptors |
| 10 | +import io.grpc.MethodDescriptor |
| 11 | +import io.grpc.examples.helloworld.GreeterGrpcKt |
| 12 | +import io.grpc.examples.helloworld.HelloRequest |
| 13 | +import io.grpc.examples.helloworld.MultiHelloRequest |
| 14 | +import kotlinx.coroutines.flow.Flow |
| 15 | +import kotlinx.coroutines.flow.first |
| 16 | +import kotlinx.coroutines.flow.flowOf |
| 17 | +import kotlinx.coroutines.flow.map |
| 18 | +import kotlinx.coroutines.withContext |
| 19 | +import org.junit.Test |
| 20 | +import org.junit.runner.RunWith |
| 21 | +import org.junit.runners.JUnit4 |
| 22 | +import java.util.UUID |
| 23 | +import kotlin.coroutines.CoroutineContext |
| 24 | + |
| 25 | +@RunWith(JUnit4::class) |
| 26 | +class ClientCallOptionsCoroutineContextPropagationTest : AbstractCallsTest() { |
| 27 | + |
| 28 | + @Test |
| 29 | + fun `should capture coroutine context with unary call`() { |
| 30 | + val server = object : GreeterGrpcKt.GreeterCoroutineImplBase() { |
| 31 | + override suspend fun sayHello(request: HelloRequest) = helloReply("Hello, ${request.name}!") |
| 32 | + } |
| 33 | + val interceptor = CoroutineContextCapturingInterceptor() |
| 34 | + val contextElement = DummyCoroutineContextElement() |
| 35 | + val channel = ClientInterceptors.intercept(makeChannel(server), interceptor) |
| 36 | + val stub = GreeterGrpcKt.GreeterCoroutineStub(channel) |
| 37 | + |
| 38 | + runBlocking { |
| 39 | + withContext(contextElement) { |
| 40 | + ProtoTruth.assertThat(stub.sayHello(helloRequest("Steven"))) |
| 41 | + .isEqualTo(helloReply("Hello, Steven!")) |
| 42 | + } |
| 43 | + } |
| 44 | + assertThat(interceptor.coroutineContext).isNotNull() |
| 45 | + assertThat(interceptor.coroutineContext!![DummyCoroutineContextElement]).isEqualTo(contextElement) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `should capture coroutine context with client streaming`() { |
| 50 | + val server = object : GreeterGrpcKt.GreeterCoroutineImplBase() { |
| 51 | + override suspend fun clientStreamSayHello(requests: Flow<HelloRequest>) = requests.map { request -> |
| 52 | + helloReply("Hello, ${request.name}!") |
| 53 | + }.first() |
| 54 | + } |
| 55 | + val interceptor = CoroutineContextCapturingInterceptor() |
| 56 | + val contextElement = DummyCoroutineContextElement() |
| 57 | + val channel = ClientInterceptors.intercept(makeChannel(server), interceptor) |
| 58 | + val stub = GreeterGrpcKt.GreeterCoroutineStub(channel) |
| 59 | + |
| 60 | + runBlocking { |
| 61 | + withContext(contextElement) { |
| 62 | + ProtoTruth.assertThat(stub.clientStreamSayHello(flowOf(helloRequest("Steven")))) |
| 63 | + .isEqualTo(helloReply("Hello, Steven!")) |
| 64 | + } |
| 65 | + } |
| 66 | + assertThat(interceptor.coroutineContext).isNotNull() |
| 67 | + assertThat(interceptor.coroutineContext!![DummyCoroutineContextElement]).isEqualTo(contextElement) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `should capture coroutine context with server streaming`() { |
| 72 | + val server = object : GreeterGrpcKt.GreeterCoroutineImplBase() { |
| 73 | + override fun serverStreamSayHello(request: MultiHelloRequest) = flowOf( |
| 74 | + helloReply("Hello, ${request.nameList.joinToString()}!") |
| 75 | + ) |
| 76 | + } |
| 77 | + val interceptor = CoroutineContextCapturingInterceptor() |
| 78 | + val contextElement = DummyCoroutineContextElement() |
| 79 | + val channel = ClientInterceptors.intercept(makeChannel(server), interceptor) |
| 80 | + val stub = GreeterGrpcKt.GreeterCoroutineStub(channel) |
| 81 | + |
| 82 | + runBlocking { |
| 83 | + withContext(contextElement) { |
| 84 | + ProtoTruth.assertThat(stub.serverStreamSayHello(multiHelloRequest("Steven", "Andrew")).first()) |
| 85 | + .isEqualTo(helloReply("Hello, Steven, Andrew!")) |
| 86 | + } |
| 87 | + } |
| 88 | + assertThat(interceptor.coroutineContext).isNotNull() |
| 89 | + assertThat(interceptor.coroutineContext!![DummyCoroutineContextElement]).isEqualTo(contextElement) |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun `should capture coroutine context with bidi streaming`() { |
| 94 | + val server = object : GreeterGrpcKt.GreeterCoroutineImplBase() { |
| 95 | + override fun bidiStreamSayHello(requests: Flow<HelloRequest>) = requests.map { request -> |
| 96 | + helloReply("Hello, ${request.name}!") |
| 97 | + } |
| 98 | + } |
| 99 | + val interceptor = CoroutineContextCapturingInterceptor() |
| 100 | + val contextElement = DummyCoroutineContextElement() |
| 101 | + val channel = ClientInterceptors.intercept(makeChannel(server), interceptor) |
| 102 | + val stub = GreeterGrpcKt.GreeterCoroutineStub(channel) |
| 103 | + |
| 104 | + runBlocking { |
| 105 | + withContext(contextElement) { |
| 106 | + ProtoTruth.assertThat(stub.bidiStreamSayHello(flowOf(helloRequest("Steven"))).first()) |
| 107 | + .isEqualTo(helloReply("Hello, Steven!")) |
| 108 | + } |
| 109 | + } |
| 110 | + assertThat(interceptor.coroutineContext).isNotNull() |
| 111 | + assertThat(interceptor.coroutineContext!![DummyCoroutineContextElement]).isEqualTo(contextElement) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +private data class DummyCoroutineContextElement(val value: UUID = UUID.randomUUID()) : CoroutineContext.Element { |
| 116 | + override val key: CoroutineContext.Key<*> = Key |
| 117 | + |
| 118 | + companion object Key : CoroutineContext.Key<DummyCoroutineContextElement> |
| 119 | +} |
| 120 | + |
| 121 | +private class CoroutineContextCapturingInterceptor : ClientInterceptor { |
| 122 | + |
| 123 | + var coroutineContext: CoroutineContext? = null |
| 124 | + |
| 125 | + override fun <ReqT : Any?, RespT : Any?> interceptCall( |
| 126 | + method: MethodDescriptor<ReqT, RespT>, |
| 127 | + callOptions: CallOptions, |
| 128 | + next: Channel, |
| 129 | + ): ClientCall<ReqT, RespT> { |
| 130 | + coroutineContext = callOptions.coroutineContext |
| 131 | + |
| 132 | + return next.newCall(method, callOptions) |
| 133 | + } |
| 134 | +} |
0 commit comments