ServerInterceptorPipelineTests.swift 5.7 KB

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