13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+ import Atomics
16
17
import EchoImplementation
17
18
import EchoModel
18
19
import GRPC
@@ -28,14 +29,15 @@ class InterceptorsTests: GRPCTestCase {
28
29
private var server : Server !
29
30
private var connection : ClientConnection !
30
31
private var echo : Echo_EchoNIOClient !
32
+ private let onCloseCounter = ManagedAtomic < Int > ( 0 )
31
33
32
34
override func setUp( ) {
33
35
super. setUp ( )
34
36
self . group = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
35
37
36
38
self . server = try ! Server . insecure ( group: self . group)
37
39
. withServiceProviders ( [
38
- EchoProvider ( ) ,
40
+ EchoProvider ( interceptors : CountOnCloseInterceptors ( counter : self . onCloseCounter ) ) ,
39
41
HelloWorldProvider ( interceptors: HelloWorldServerInterceptorFactory ( ) ) ,
40
42
] )
41
43
. withLogger ( self . serverLogger)
@@ -64,6 +66,8 @@ class InterceptorsTests: GRPCTestCase {
64
66
let get = self . echo. get ( . with { $0. text = " hello " } )
65
67
assertThat ( try get. response. wait ( ) , . is( . with { $0. text = " hello :teg ohce tfiwS " } ) )
66
68
assertThat ( try get. status. wait ( ) , . hasCode( . ok) )
69
+
70
+ XCTAssertEqual ( self . onCloseCounter. load ( ordering: . sequentiallyConsistent) , 1 )
67
71
}
68
72
69
73
func testCollect( ) {
@@ -73,6 +77,8 @@ class InterceptorsTests: GRPCTestCase {
73
77
collect. sendEnd ( promise: nil )
74
78
assertThat ( try collect. response. wait ( ) , . is( . with { $0. text = " 3 4 1 2 :tcelloc ohce tfiwS " } ) )
75
79
assertThat ( try collect. status. wait ( ) , . hasCode( . ok) )
80
+
81
+ XCTAssertEqual ( self . onCloseCounter. load ( ordering: . sequentiallyConsistent) , 1 )
76
82
}
77
83
78
84
func testExpand( ) {
@@ -81,6 +87,8 @@ class InterceptorsTests: GRPCTestCase {
81
87
assertThat ( response, . is( . with { $0. text = " hello :)0( dnapxe ohce tfiwS " } ) )
82
88
}
83
89
assertThat ( try expand. status. wait ( ) , . hasCode( . ok) )
90
+
91
+ XCTAssertEqual ( self . onCloseCounter. load ( ordering: . sequentiallyConsistent) , 1 )
84
92
}
85
93
86
94
func testUpdate( ) {
@@ -91,6 +99,8 @@ class InterceptorsTests: GRPCTestCase {
91
99
update. sendMessage ( . with { $0. text = " hello " } , promise: nil )
92
100
update. sendEnd ( promise: nil )
93
101
assertThat ( try update. status. wait ( ) , . hasCode( . ok) )
102
+
103
+ XCTAssertEqual ( self . onCloseCounter. load ( ordering: . sequentiallyConsistent) , 1 )
94
104
}
95
105
96
106
func testSayHello( ) {
@@ -360,6 +370,54 @@ final class ReversingInterceptors: Echo_EchoClientInterceptorFactoryProtocol {
360
370
}
361
371
}
362
372
373
+ final class CountOnCloseInterceptors : Echo_EchoServerInterceptorFactoryProtocol {
374
+ // This interceptor is stateless, let's just share it.
375
+ private let interceptors : [ ServerInterceptor < Echo_EchoRequest , Echo_EchoResponse > ]
376
+
377
+ init ( counter: ManagedAtomic < Int > ) {
378
+ self . interceptors = [ CountOnCloseServerInterceptor ( counter: counter) ]
379
+ }
380
+
381
+ func makeGetInterceptors( ) -> [ ServerInterceptor < Echo_EchoRequest , Echo_EchoResponse > ] {
382
+ return self . interceptors
383
+ }
384
+
385
+ func makeExpandInterceptors( ) -> [ ServerInterceptor < Echo_EchoRequest , Echo_EchoResponse > ] {
386
+ return self . interceptors
387
+ }
388
+
389
+ func makeCollectInterceptors( ) -> [ ServerInterceptor < Echo_EchoRequest , Echo_EchoResponse > ] {
390
+ return self . interceptors
391
+ }
392
+
393
+ func makeUpdateInterceptors( ) -> [ ServerInterceptor < Echo_EchoRequest , Echo_EchoResponse > ] {
394
+ return self . interceptors
395
+ }
396
+ }
397
+
398
+ final class CountOnCloseServerInterceptor : ServerInterceptor < Echo_EchoRequest , Echo_EchoResponse > {
399
+ private let counter : ManagedAtomic < Int >
400
+
401
+ init ( counter: ManagedAtomic < Int > ) {
402
+ self . counter = counter
403
+ }
404
+
405
+ override func receive(
406
+ _ part: GRPCServerRequestPart < Echo_EchoRequest > ,
407
+ context: ServerInterceptorContext < Echo_EchoRequest , Echo_EchoResponse >
408
+ ) {
409
+ switch part {
410
+ case . metadata:
411
+ context. closeFuture. whenComplete { _ in
412
+ self . counter. wrappingIncrement ( ordering: . sequentiallyConsistent)
413
+ }
414
+ default :
415
+ ( )
416
+ }
417
+ context. receive ( part)
418
+ }
419
+ }
420
+
363
421
private enum MagicKey : UserInfo . Key {
364
422
typealias Value = String
365
423
}
0 commit comments