1
+ package io.grpc.kotlin
2
+
3
+ import com.google.common.truth.Truth.assertThat
4
+ import io.grpc.ServerCall
5
+ import io.grpc.ServerInterceptors
6
+ import io.grpc.examples.helloworld.GreeterGrpcKt.GreeterCoroutineImplBase
7
+ import io.grpc.examples.helloworld.GreeterGrpcKt.GreeterCoroutineStub
8
+ import io.grpc.examples.helloworld.HelloReply
9
+ import io.grpc.examples.helloworld.HelloRequest
10
+ import org.junit.Test
11
+ import org.junit.runner.RunWith
12
+ import org.junit.runners.JUnit4
13
+ import kotlin.coroutines.CoroutineContext
14
+ import kotlin.coroutines.EmptyCoroutineContext
15
+ import kotlin.coroutines.coroutineContext
16
+ import io.grpc.Metadata as GrpcMetadata
17
+
18
+ /* * Tests for [CoroutineContextServerInterceptor]. */
19
+ @RunWith(JUnit4 ::class ) /* inserted by Copybara: */ @com.google.testing.testsize.MediumTest
20
+ class CoroutineContextServerInterceptorTest : AbstractCallsTest () {
21
+ class ArbitraryContextElement (val message : String = " " ) : CoroutineContext.Element {
22
+ companion object Key : CoroutineContext.Key<ArbitraryContextElement>
23
+ override val key: CoroutineContext .Key <* >
24
+ get() = Key
25
+ }
26
+
27
+ class HelloReplyWithContextMessage (
28
+ message : String? = null
29
+ ) : GreeterCoroutineImplBase(
30
+ message?.let { ArbitraryContextElement (it) } ? : EmptyCoroutineContext
31
+ ) {
32
+ override suspend fun sayHello (request : HelloRequest ): HelloReply =
33
+ helloReply(coroutineContext[ArbitraryContextElement ]!! .message)
34
+ }
35
+
36
+ @Test
37
+ fun injectContext () {
38
+ val interceptor = object : CoroutineContextServerInterceptor () {
39
+ override fun coroutineContext (
40
+ call : ServerCall <* , * >,
41
+ headers : GrpcMetadata
42
+ ): CoroutineContext = ArbitraryContextElement (" success" )
43
+ }
44
+
45
+ val channel = makeChannel(HelloReplyWithContextMessage (), interceptor)
46
+ val client = GreeterCoroutineStub (channel)
47
+
48
+ runBlocking {
49
+ assertThat(client.sayHello(helloRequest(" " )).message).isEqualTo(" success" )
50
+ }
51
+ }
52
+
53
+ @Test
54
+ fun conflictingInterceptorsInnermostWins () {
55
+ val interceptor1 = object : CoroutineContextServerInterceptor () {
56
+ override fun coroutineContext (
57
+ call : ServerCall <* , * >,
58
+ headers : GrpcMetadata
59
+ ): CoroutineContext = ArbitraryContextElement (" first" )
60
+ }
61
+ val interceptor2 = object : CoroutineContextServerInterceptor () {
62
+ override fun coroutineContext (
63
+ call : ServerCall <* , * >,
64
+ headers : GrpcMetadata
65
+ ): CoroutineContext = ArbitraryContextElement (" second" )
66
+ }
67
+
68
+ val channel = makeChannel(
69
+ ServerInterceptors .intercept(
70
+ ServerInterceptors .intercept(
71
+ HelloReplyWithContextMessage (),
72
+ interceptor2
73
+ ),
74
+ interceptor1
75
+ )
76
+ )
77
+ val client = GreeterCoroutineStub (channel)
78
+
79
+ runBlocking {
80
+ assertThat(client.sayHello(helloRequest(" " )).message).isEqualTo(" second" )
81
+ }
82
+ }
83
+
84
+ @Test
85
+ fun interceptorContextTakesPriority () {
86
+ val interceptor = object : CoroutineContextServerInterceptor () {
87
+ override fun coroutineContext (
88
+ call : ServerCall <* , * >,
89
+ headers : GrpcMetadata
90
+ ): CoroutineContext = ArbitraryContextElement (" interceptor" )
91
+ }
92
+
93
+ val channel = makeChannel(HelloReplyWithContextMessage (" server" ), interceptor)
94
+ val client = GreeterCoroutineStub (channel)
95
+
96
+ runBlocking {
97
+ assertThat(client.sayHello(helloRequest(" " )).message).isEqualTo(" interceptor" )
98
+ }
99
+ }
100
+ }
0 commit comments