4
4
5
5
package kotlinx.rpc.grpc.test.proto
6
6
7
- import kotlinx.coroutines.CompletableDeferred
8
- import kotlinx.coroutines.coroutineScope
9
- import kotlinx.coroutines.delay
10
7
import kotlinx.coroutines.flow.Flow
11
8
import kotlinx.coroutines.flow.channelFlow
12
- import kotlinx.coroutines.flow.collect
13
- import kotlinx.coroutines.flow.conflate
14
9
import kotlinx.coroutines.flow.flow
15
10
import kotlinx.coroutines.flow.map
16
11
import kotlinx.coroutines.flow.toList
17
- import kotlinx.coroutines.launch
18
12
import kotlinx.rpc.RpcServer
19
13
import kotlinx.rpc.grpc.ClientCallScope
20
14
import kotlinx.rpc.grpc.ClientInterceptor
21
15
import kotlinx.rpc.grpc.GrpcClient
22
16
import kotlinx.rpc.grpc.StatusCode
23
17
import kotlinx.rpc.grpc.StatusException
24
- import kotlinx.rpc.grpc.internal.bidirectionalStreamingRpc
25
18
import kotlinx.rpc.grpc.statusCode
26
19
import kotlinx.rpc.grpc.test.EchoRequest
27
20
import kotlinx.rpc.grpc.test.EchoResponse
@@ -37,7 +30,7 @@ import kotlin.test.assertFailsWith
37
30
import kotlin.test.assertIs
38
31
import kotlin.test.assertTrue
39
32
40
- class ClientInterceptorTest : GrpcProtoTest () {
33
+ class ClientInterceptorTest : GrpcProtoTest () {
41
34
42
35
override fun RpcServer.registerServices () {
43
36
registerService<EchoService > { EchoServiceImpl () }
@@ -46,7 +39,7 @@ class ClientInterceptorTest: GrpcProtoTest() {
46
39
@Test
47
40
fun `throw during intercept - should fail with thrown exception` () {
48
41
val error = assertFailsWith<IllegalStateException > {
49
- val interceptor = interceptor { _, _ ->
42
+ val interceptor = interceptor {
50
43
throw IllegalStateException (" Failing in interceptor" )
51
44
}
52
45
runGrpcTest(clientInterceptors = interceptor, test = ::unaryCall)
@@ -58,11 +51,11 @@ class ClientInterceptorTest: GrpcProtoTest() {
58
51
@Test
59
52
fun `throw during onHeader - should fail with status exception containing the thrown exception` () {
60
53
val error = assertFailsWith<StatusException > {
61
- val interceptor = interceptor { scope, req ->
62
- scope. onHeaders {
54
+ val interceptor = interceptor {
55
+ onHeaders {
63
56
throw IllegalStateException (" Failing in onHeader" )
64
57
}
65
- scope. proceed(req )
58
+ proceed(it )
66
59
}
67
60
runGrpcTest(clientInterceptors = interceptor, test = ::unaryCall)
68
61
}
@@ -75,11 +68,11 @@ class ClientInterceptorTest: GrpcProtoTest() {
75
68
@Test
76
69
fun `throw during onClose - should fail with status exception containing the thrown exception` () {
77
70
val error = assertFailsWith<StatusException > {
78
- val interceptor = interceptor { scope, req ->
79
- scope. onClose { _, _ ->
71
+ val interceptor = interceptor {
72
+ onClose { _, _ ->
80
73
throw IllegalStateException (" Failing in onClose" )
81
74
}
82
- scope. proceed(req )
75
+ proceed(it )
83
76
}
84
77
runGrpcTest(clientInterceptors = interceptor, test = ::unaryCall)
85
78
}
@@ -92,9 +85,9 @@ class ClientInterceptorTest: GrpcProtoTest() {
92
85
@Test
93
86
fun `cancel in intercept - should fail with cancellation` () {
94
87
val error = assertFailsWith<StatusException > {
95
- val interceptor = interceptor { scope, req ->
96
- scope. cancel(" Canceling in interceptor" , IllegalStateException (" Cancellation cause" ))
97
- scope. proceed(req )
88
+ val interceptor = interceptor {
89
+ cancel(" Canceling in interceptor" , IllegalStateException (" Cancellation cause" ))
90
+ proceed(it )
98
91
}
99
92
runGrpcTest(clientInterceptors = interceptor, test = ::unaryCall)
100
93
}
@@ -107,9 +100,9 @@ class ClientInterceptorTest: GrpcProtoTest() {
107
100
108
101
@Test
109
102
fun `modify request message - should return modified message` () {
110
- val interceptor = interceptor { scope, req ->
111
- val modified = req .map { EchoRequest { message = " Modified" } }
112
- scope. proceed(modified)
103
+ val interceptor = interceptor {
104
+ val modified = it .map { EchoRequest { message = " Modified" } }
105
+ proceed(modified)
113
106
}
114
107
runGrpcTest(clientInterceptors = interceptor) {
115
108
val service = it.withService<EchoService >()
@@ -120,8 +113,8 @@ class ClientInterceptorTest: GrpcProtoTest() {
120
113
121
114
@Test
122
115
fun `modify response message - should return modified message` () {
123
- val interceptor = interceptor { scope, req ->
124
- scope. proceed(req ).map { EchoResponse { message = " Modified" } }
116
+ val interceptor = interceptor {
117
+ proceed(it ).map { EchoResponse { message = " Modified" } }
125
118
}
126
119
runGrpcTest(clientInterceptors = interceptor) {
127
120
val service = it.withService<EchoService >()
@@ -132,24 +125,26 @@ class ClientInterceptorTest: GrpcProtoTest() {
132
125
133
126
@Test
134
127
fun `append a response message once closed` () {
135
- val interceptor = interceptor { scope, req -> channelFlow {
136
- scope.proceed(req).collect {
137
- trySend(it)
138
- }
139
- scope.onClose { status, _ ->
140
- trySend(EchoResponse { message = " Appended-after-close-with-${status.statusCode} " })
128
+ val interceptor = interceptor {
129
+ channelFlow {
130
+ proceed(it).collect {
131
+ trySend(it)
132
+ }
133
+ onClose { status, _ ->
134
+ trySend(EchoResponse { message = " Appended-after-close-with-${status.statusCode} " })
135
+ }
141
136
}
142
- } }
137
+ }
143
138
144
139
runGrpcTest(
145
140
clientInterceptors = interceptor
146
141
) { client ->
147
142
val svc = client.withService<EchoService >()
148
143
val responses = svc.BidirectionalStreamingEcho (flow {
149
- repeat(5 ) {
150
- emit(EchoRequest { message = " Eccchhooo" })
151
- }
152
- }).toList()
144
+ repeat(5 ) {
145
+ emit(EchoRequest { message = " Eccchhooo" })
146
+ }
147
+ }).toList()
153
148
assertEquals(6 , responses.size)
154
149
assertTrue(responses.any { it.message == " Appended-after-close-with-OK" })
155
150
}
@@ -164,15 +159,16 @@ class ClientInterceptorTest: GrpcProtoTest() {
164
159
}
165
160
166
161
private fun interceptor (
167
- block : ( ClientCallScope <Any , Any > , Flow <Any >) -> Flow <Any >
162
+ block : ClientCallScope <Any , Any >.( Flow <Any >) -> Flow <Any >,
168
163
): List <ClientInterceptor > {
169
164
return listOf (object : ClientInterceptor {
170
165
@Suppress(" UNCHECKED_CAST" )
171
- override fun <Req , Resp > intercept (
172
- scope : ClientCallScope <Req , Resp >,
166
+ override fun <Req , Resp > ClientCallScope <Req , Resp >.intercept (
173
167
request : Flow <Req >,
174
168
): Flow <Resp > {
175
- return block(scope as ClientCallScope <Any , Any >, request as Flow <Any >) as Flow <Resp >
169
+ with (this as ClientCallScope <Any , Any >) {
170
+ return block(request as Flow <Any >) as Flow <Resp >
171
+ }
176
172
}
177
173
})
178
174
}
0 commit comments