ServerInterceptorPipelineTests.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 NIOCore
  18. import NIOEmbedded
  19. import NIOHPACK
  20. import XCTest
  21. class ServerInterceptorPipelineTests: GRPCTestCase {
  22. override func setUp() {
  23. super.setUp()
  24. self.embeddedEventLoop = EmbeddedEventLoop()
  25. }
  26. private var embeddedEventLoop: EmbeddedEventLoop!
  27. private func makePipeline<Request, Response>(
  28. requests: Request.Type = Request.self,
  29. responses: Response.Type = Response.self,
  30. path: String = "/foo/bar",
  31. callType: GRPCCallType = .unary,
  32. interceptors: [ServerInterceptor<Request, Response>] = [],
  33. onRequestPart: @escaping (GRPCServerRequestPart<Request>) -> Void,
  34. onResponsePart: @escaping (GRPCServerResponsePart<Response>, EventLoopPromise<Void>?) -> Void
  35. ) -> ServerInterceptorPipeline<Request, Response> {
  36. return ServerInterceptorPipeline(
  37. logger: self.logger,
  38. eventLoop: self.embeddedEventLoop,
  39. path: path,
  40. callType: callType,
  41. remoteAddress: nil,
  42. userInfoRef: Ref(UserInfo()),
  43. interceptors: interceptors,
  44. onRequestPart: onRequestPart,
  45. onResponsePart: onResponsePart
  46. )
  47. }
  48. func testEmptyPipeline() {
  49. var requestParts: [GRPCServerRequestPart<String>] = []
  50. var responseParts: [GRPCServerResponsePart<String>] = []
  51. let pipeline = self.makePipeline(
  52. requests: String.self,
  53. responses: String.self,
  54. onRequestPart: { requestParts.append($0) },
  55. onResponsePart: { part, promise in
  56. responseParts.append(part)
  57. assertThat(promise, .is(.nil()))
  58. }
  59. )
  60. pipeline.receive(.metadata([:]))
  61. pipeline.receive(.message("foo"))
  62. pipeline.receive(.end)
  63. assertThat(requestParts, .hasCount(3))
  64. assertThat(requestParts[0].metadata, .is([:]))
  65. assertThat(requestParts[1].message, .is("foo"))
  66. assertThat(requestParts[2].isEnd, .is(true))
  67. pipeline.send(.metadata([:]), promise: nil)
  68. pipeline.send(.message("bar", .init(compress: false, flush: false)), promise: nil)
  69. pipeline.send(.end(.ok, [:]), promise: nil)
  70. assertThat(responseParts, .hasCount(3))
  71. assertThat(responseParts[0].metadata, .is([:]))
  72. assertThat(responseParts[1].message, .is("bar"))
  73. assertThat(responseParts[2].end, .is(.notNil()))
  74. // Pipelines should now be closed. We can't send or receive.
  75. let p = self.embeddedEventLoop.makePromise(of: Void.self)
  76. pipeline.send(.metadata([:]), promise: p)
  77. assertThat(try p.futureResult.wait(), .throws(.instanceOf(GRPCError.AlreadyComplete.self)))
  78. responseParts.removeAll()
  79. pipeline.receive(.end)
  80. assertThat(responseParts, .isEmpty())
  81. }
  82. func testRecordingPipeline() {
  83. let recorder = RecordingServerInterceptor<String, String>()
  84. let pipeline = self.makePipeline(
  85. interceptors: [recorder],
  86. onRequestPart: { _ in },
  87. onResponsePart: { _, _ in }
  88. )
  89. pipeline.receive(.metadata([:]))
  90. pipeline.receive(.message("foo"))
  91. pipeline.receive(.end)
  92. pipeline.send(.metadata([:]), promise: nil)
  93. pipeline.send(.message("bar", .init(compress: false, flush: false)), promise: nil)
  94. pipeline.send(.end(.ok, [:]), promise: nil)
  95. // Check the request parts are there.
  96. assertThat(recorder.requestParts, .hasCount(3))
  97. assertThat(recorder.requestParts[0].metadata, .is(.notNil()))
  98. assertThat(recorder.requestParts[1].message, .is(.notNil()))
  99. assertThat(recorder.requestParts[2].isEnd, .is(true))
  100. // Check the response parts are there.
  101. assertThat(recorder.responseParts, .hasCount(3))
  102. assertThat(recorder.responseParts[0].metadata, .is(.notNil()))
  103. assertThat(recorder.responseParts[1].message, .is(.notNil()))
  104. assertThat(recorder.responseParts[2].end, .is(.notNil()))
  105. }
  106. }
  107. internal class RecordingServerInterceptor<Request, Response>:
  108. ServerInterceptor<Request, Response> {
  109. var requestParts: [GRPCServerRequestPart<Request>] = []
  110. var responseParts: [GRPCServerResponsePart<Response>] = []
  111. override func receive(
  112. _ part: GRPCServerRequestPart<Request>,
  113. context: ServerInterceptorContext<Request, Response>
  114. ) {
  115. self.requestParts.append(part)
  116. context.receive(part)
  117. }
  118. override func send(
  119. _ part: GRPCServerResponsePart<Response>,
  120. promise: EventLoopPromise<Void>?,
  121. context: ServerInterceptorContext<Request, Response>
  122. ) {
  123. self.responseParts.append(part)
  124. context.send(part, promise: promise)
  125. }
  126. }
  127. extension GRPCServerRequestPart {
  128. var metadata: HPACKHeaders? {
  129. switch self {
  130. case let .metadata(metadata):
  131. return metadata
  132. default:
  133. return nil
  134. }
  135. }
  136. var message: Request? {
  137. switch self {
  138. case let .message(message):
  139. return message
  140. default:
  141. return nil
  142. }
  143. }
  144. var isEnd: Bool {
  145. switch self {
  146. case .end:
  147. return true
  148. default:
  149. return false
  150. }
  151. }
  152. }
  153. extension GRPCServerResponsePart {
  154. var metadata: HPACKHeaders? {
  155. switch self {
  156. case let .metadata(metadata):
  157. return metadata
  158. default:
  159. return nil
  160. }
  161. }
  162. var message: Response? {
  163. switch self {
  164. case let .message(message, _):
  165. return message
  166. default:
  167. return nil
  168. }
  169. }
  170. var end: (GRPCStatus, HPACKHeaders)? {
  171. switch self {
  172. case let .end(status, trailers):
  173. return (status, trailers)
  174. default:
  175. return nil
  176. }
  177. }
  178. }