ServerInterceptorPipelineTests.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. @testable import GRPC
  17. import NIO
  18. import NIOHPACK
  19. import XCTest
  20. class ServerInterceptorPipelineTests: GRPCTestCase {
  21. override func setUp() {
  22. super.setUp()
  23. self.embeddedEventLoop = EmbeddedEventLoop()
  24. }
  25. private var embeddedEventLoop: EmbeddedEventLoop!
  26. private func makePipeline<Request, Response>(
  27. requests: Request.Type = Request.self,
  28. responses: Response.Type = Response.self,
  29. path: String = "/foo/bar",
  30. callType: GRPCCallType = .unary,
  31. interceptors: [ServerInterceptor<Request, Response>] = [],
  32. onRequestPart: @escaping (GRPCServerRequestPart<Request>) -> Void,
  33. onResponsePart: @escaping (GRPCServerResponsePart<Response>, EventLoopPromise<Void>?) -> Void
  34. ) -> ServerInterceptorPipeline<Request, Response> {
  35. return ServerInterceptorPipeline(
  36. logger: self.logger,
  37. eventLoop: self.embeddedEventLoop,
  38. path: path,
  39. callType: callType,
  40. remoteAddress: nil,
  41. userInfoRef: Ref(UserInfo()),
  42. interceptors: interceptors,
  43. onRequestPart: onRequestPart,
  44. onResponsePart: onResponsePart
  45. )
  46. }
  47. func testEmptyPipeline() {
  48. var requestParts: [GRPCServerRequestPart<String>] = []
  49. var responseParts: [GRPCServerResponsePart<String>] = []
  50. let pipeline = self.makePipeline(
  51. requests: String.self,
  52. responses: String.self,
  53. onRequestPart: { requestParts.append($0) },
  54. onResponsePart: { part, promise in
  55. responseParts.append(part)
  56. assertThat(promise, .is(.nil()))
  57. }
  58. )
  59. pipeline.receive(.metadata([:]))
  60. pipeline.receive(.message("foo"))
  61. pipeline.receive(.end)
  62. assertThat(requestParts, .hasCount(3))
  63. assertThat(requestParts[0].metadata, .is([:]))
  64. assertThat(requestParts[1].message, .is("foo"))
  65. assertThat(requestParts[2].isEnd, .is(true))
  66. pipeline.send(.metadata([:]), promise: nil)
  67. pipeline.send(.message("bar", .init(compress: false, flush: false)), promise: nil)
  68. pipeline.send(.end(.ok, [:]), promise: nil)
  69. assertThat(responseParts, .hasCount(3))
  70. assertThat(responseParts[0].metadata, .is([:]))
  71. assertThat(responseParts[1].message, .is("bar"))
  72. assertThat(responseParts[2].end, .is(.notNil()))
  73. // Pipelines should now be closed. We can't send or receive.
  74. let p = self.embeddedEventLoop.makePromise(of: Void.self)
  75. pipeline.send(.metadata([:]), promise: p)
  76. assertThat(try p.futureResult.wait(), .throws(.instanceOf(GRPCError.AlreadyComplete.self)))
  77. responseParts.removeAll()
  78. pipeline.receive(.end)
  79. assertThat(responseParts, .isEmpty())
  80. }
  81. func testRecordingPipeline() {
  82. let recorder = RecordingServerInterceptor<String, String>()
  83. let pipeline = self.makePipeline(
  84. interceptors: [recorder],
  85. onRequestPart: { _ in },
  86. onResponsePart: { _, _ in }
  87. )
  88. pipeline.receive(.metadata([:]))
  89. pipeline.receive(.message("foo"))
  90. pipeline.receive(.end)
  91. pipeline.send(.metadata([:]), promise: nil)
  92. pipeline.send(.message("bar", .init(compress: false, flush: false)), promise: nil)
  93. pipeline.send(.end(.ok, [:]), promise: nil)
  94. // Check the request parts are there.
  95. assertThat(recorder.requestParts, .hasCount(3))
  96. assertThat(recorder.requestParts[0].metadata, .is(.notNil()))
  97. assertThat(recorder.requestParts[1].message, .is(.notNil()))
  98. assertThat(recorder.requestParts[2].isEnd, .is(true))
  99. // Check the response parts are there.
  100. assertThat(recorder.responseParts, .hasCount(3))
  101. assertThat(recorder.responseParts[0].metadata, .is(.notNil()))
  102. assertThat(recorder.responseParts[1].message, .is(.notNil()))
  103. assertThat(recorder.responseParts[2].end, .is(.notNil()))
  104. }
  105. }
  106. internal class RecordingServerInterceptor<Request, Response>:
  107. ServerInterceptor<Request, Response> {
  108. var requestParts: [GRPCServerRequestPart<Request>] = []
  109. var responseParts: [GRPCServerResponsePart<Response>] = []
  110. override func receive(
  111. _ part: GRPCServerRequestPart<Request>,
  112. context: ServerInterceptorContext<Request, Response>
  113. ) {
  114. self.requestParts.append(part)
  115. context.receive(part)
  116. }
  117. override func send(
  118. _ part: GRPCServerResponsePart<Response>,
  119. promise: EventLoopPromise<Void>?,
  120. context: ServerInterceptorContext<Request, Response>
  121. ) {
  122. self.responseParts.append(part)
  123. context.send(part, promise: promise)
  124. }
  125. }
  126. extension GRPCServerRequestPart {
  127. var metadata: HPACKHeaders? {
  128. switch self {
  129. case let .metadata(metadata):
  130. return metadata
  131. default:
  132. return nil
  133. }
  134. }
  135. var message: Request? {
  136. switch self {
  137. case let .message(message):
  138. return message
  139. default:
  140. return nil
  141. }
  142. }
  143. var isEnd: Bool {
  144. switch self {
  145. case .end:
  146. return true
  147. default:
  148. return false
  149. }
  150. }
  151. }
  152. extension GRPCServerResponsePart {
  153. var metadata: HPACKHeaders? {
  154. switch self {
  155. case let .metadata(metadata):
  156. return metadata
  157. default:
  158. return nil
  159. }
  160. }
  161. var message: Response? {
  162. switch self {
  163. case let .message(message, _):
  164. return message
  165. default:
  166. return nil
  167. }
  168. }
  169. var end: (GRPCStatus, HPACKHeaders)? {
  170. switch self {
  171. case let .end(status, trailers):
  172. return (status, trailers)
  173. default:
  174. return nil
  175. }
  176. }
  177. }