FakeChannel.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. import NIO
  17. import Logging
  18. /// A fake channel for use with generated test clients.
  19. ///
  20. /// The `FakeChannel` provides factories for calls which avoid most of the gRPC stack and don't do
  21. /// real networking. Each call relies on either a `FakeUnaryResponse` or a `FakeStreamingResponse`
  22. /// to get responses or errors. The fake response of each type should be registered with the channel
  23. /// prior to making a call via `makeFakeUnaryResponse` or `makeFakeStreamingResponse` respectively.
  24. ///
  25. /// Users will typically not be required to interact with the channel directly, instead they should
  26. /// do so via a generated test client.
  27. public class FakeChannel: GRPCChannel {
  28. /// Fake response streams keyed by their path.
  29. private var responseStreams: [String: CircularBuffer<Any>]
  30. /// A logger.
  31. public let logger: Logger
  32. public init(logger: Logger = Logger(label: "io.grpc.testing")) {
  33. self.responseStreams = [:]
  34. self.logger = logger
  35. }
  36. /// Make and store a fake unary response for the given path. Users should prefer making a response
  37. /// stream for their RPC directly via the appropriate method on their generated test client.
  38. public func makeFakeUnaryResponse<Request: GRPCPayload, Response: GRPCPayload>(
  39. path: String,
  40. requestHandler: @escaping (FakeRequestPart<Request>) -> ()
  41. ) -> FakeUnaryResponse<Request, Response> {
  42. let proxy = FakeUnaryResponse<Request, Response>(requestHandler: requestHandler)
  43. self.responseStreams[path, default: []].append(proxy)
  44. return proxy
  45. }
  46. /// Make and store a fake streaming response for the given path. Users should prefer making a
  47. /// response stream for their RPC directly via the appropriate method on their generated test
  48. /// client.
  49. public func makeFakeStreamingResponse<Request: GRPCPayload, Response: GRPCPayload>(
  50. path: String,
  51. requestHandler: @escaping (FakeRequestPart<Request>) -> ()
  52. ) -> FakeStreamingResponse<Request, Response> {
  53. let proxy = FakeStreamingResponse<Request, Response>(requestHandler: requestHandler)
  54. self.responseStreams[path, default: []].append(proxy)
  55. return proxy
  56. }
  57. /// Returns true if there are fake responses enqueued for the given path.
  58. public func hasFakeResponseEnqueued(forPath path: String) -> Bool {
  59. guard let noStreamsForPath = self.responseStreams[path]?.isEmpty else {
  60. return false
  61. }
  62. return !noStreamsForPath
  63. }
  64. // (Docs inherited from `GRPCChannel`)
  65. public func makeUnaryCall<Request: GRPCPayload, Response: GRPCPayload>(
  66. path: String,
  67. request: Request,
  68. callOptions: CallOptions
  69. ) -> UnaryCall<Request, Response> {
  70. let call = UnaryCall<Request, Response>.make(
  71. fakeResponse: self.dequeueResponseStream(forPath: path),
  72. callOptions: callOptions,
  73. logger: self.logger
  74. )
  75. call.send(self.makeRequestHead(path: path, callOptions: callOptions), request: request)
  76. return call
  77. }
  78. // (Docs inherited from `GRPCChannel`)
  79. public func makeServerStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  80. path: String,
  81. request: Request,
  82. callOptions: CallOptions,
  83. handler: @escaping (Response) -> Void
  84. ) -> ServerStreamingCall<Request, Response> {
  85. let call = ServerStreamingCall<Request, Response>.make(
  86. fakeResponse: self.dequeueResponseStream(forPath: path),
  87. callOptions: callOptions,
  88. logger: self.logger,
  89. responseHandler: handler
  90. )
  91. call.send(self.makeRequestHead(path: path, callOptions: callOptions), request: request)
  92. return call
  93. }
  94. // (Docs inherited from `GRPCChannel`)
  95. public func makeClientStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  96. path: String,
  97. callOptions: CallOptions
  98. ) -> ClientStreamingCall<Request, Response> {
  99. let call = ClientStreamingCall<Request, Response>.make(
  100. fakeResponse: self.dequeueResponseStream(forPath: path),
  101. callOptions: callOptions,
  102. logger: self.logger
  103. )
  104. call.sendHead(self.makeRequestHead(path: path, callOptions: callOptions))
  105. return call
  106. }
  107. // (Docs inherited from `GRPCChannel`)
  108. public func makeBidirectionalStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  109. path: String,
  110. callOptions: CallOptions,
  111. handler: @escaping (Response) -> Void
  112. ) -> BidirectionalStreamingCall<Request, Response> {
  113. let call = BidirectionalStreamingCall<Request, Response>.make(
  114. fakeResponse: self.dequeueResponseStream(forPath: path),
  115. callOptions: callOptions,
  116. logger: self.logger,
  117. responseHandler: handler
  118. )
  119. call.sendHead(self.makeRequestHead(path: path, callOptions: callOptions))
  120. return call
  121. }
  122. public func close() -> EventLoopFuture<Void> {
  123. // We don't have anything to close.
  124. return EmbeddedEventLoop().makeSucceededFuture(())
  125. }
  126. }
  127. extension FakeChannel {
  128. /// Dequeue a proxy for the given path and casts it to the given type, if one exists.
  129. private func dequeueResponseStream<Stream>(
  130. forPath path: String,
  131. as: Stream.Type = Stream.self
  132. ) -> Stream? {
  133. guard var streams = self.responseStreams[path], !streams.isEmpty else {
  134. return nil
  135. }
  136. // This is fine: we know we're non-empty.
  137. let first = streams.removeFirst()
  138. self.responseStreams.updateValue(streams, forKey: path)
  139. return first as? Stream
  140. }
  141. private func makeRequestHead(path: String, callOptions: CallOptions) -> _GRPCRequestHead {
  142. return _GRPCRequestHead(
  143. scheme: "http",
  144. path: path,
  145. host: "localhost",
  146. requestID: callOptions.requestIDProvider.requestID(),
  147. options: callOptions
  148. )
  149. }
  150. }