FakeChannel.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Logging
  17. import NIOCore
  18. import NIOEmbedded
  19. import SwiftProtobuf
  20. // This type is deprecated, but we need to '@unchecked Sendable' to avoid warnings in our own code.
  21. @available(swift, deprecated: 5.6)
  22. extension FakeChannel: @unchecked Sendable {}
  23. /// A fake channel for use with generated test clients.
  24. ///
  25. /// The `FakeChannel` provides factories for calls which avoid most of the gRPC stack and don't do
  26. /// real networking. Each call relies on either a `FakeUnaryResponse` or a `FakeStreamingResponse`
  27. /// to get responses or errors. The fake response of each type should be registered with the channel
  28. /// prior to making a call via `makeFakeUnaryResponse` or `makeFakeStreamingResponse` respectively.
  29. ///
  30. /// Users will typically not be required to interact with the channel directly, instead they should
  31. /// do so via a generated test client.
  32. @available(
  33. swift,
  34. deprecated: 5.6,
  35. message:
  36. "GRPCChannel implementations must be Sendable but this implementation is not. Using a client and server on localhost is the recommended alternative."
  37. )
  38. public class FakeChannel: GRPCChannel {
  39. /// Fake response streams keyed by their path.
  40. private var responseStreams: [String: CircularBuffer<Any>]
  41. /// A logger.
  42. public let logger: Logger
  43. public init(
  44. logger: Logger = Logger(
  45. label: "io.grpc",
  46. factory: { _ in
  47. SwiftLogNoOpLogHandler()
  48. }
  49. )
  50. ) {
  51. self.responseStreams = [:]
  52. self.logger = logger
  53. }
  54. /// Make and store a fake unary response for the given path. Users should prefer making a response
  55. /// stream for their RPC directly via the appropriate method on their generated test client.
  56. public func makeFakeUnaryResponse<Request, Response>(
  57. path: String,
  58. requestHandler: @escaping (FakeRequestPart<Request>) -> Void
  59. ) -> FakeUnaryResponse<Request, Response> {
  60. let proxy = FakeUnaryResponse<Request, Response>(requestHandler: requestHandler)
  61. self.responseStreams[path, default: []].append(proxy)
  62. return proxy
  63. }
  64. /// Make and store a fake streaming response for the given path. Users should prefer making a
  65. /// response stream for their RPC directly via the appropriate method on their generated test
  66. /// client.
  67. public func makeFakeStreamingResponse<Request, Response>(
  68. path: String,
  69. requestHandler: @escaping (FakeRequestPart<Request>) -> Void
  70. ) -> FakeStreamingResponse<Request, Response> {
  71. let proxy = FakeStreamingResponse<Request, Response>(requestHandler: requestHandler)
  72. self.responseStreams[path, default: []].append(proxy)
  73. return proxy
  74. }
  75. /// Returns true if there are fake responses enqueued for the given path.
  76. public func hasFakeResponseEnqueued(forPath path: String) -> Bool {
  77. guard let noStreamsForPath = self.responseStreams[path]?.isEmpty else {
  78. return false
  79. }
  80. return !noStreamsForPath
  81. }
  82. public func makeCall<Request: Message, Response: Message>(
  83. path: String,
  84. type: GRPCCallType,
  85. callOptions: CallOptions,
  86. interceptors: [ClientInterceptor<Request, Response>]
  87. ) -> Call<Request, Response> {
  88. return self._makeCall(
  89. path: path,
  90. type: type,
  91. callOptions: callOptions,
  92. interceptors: interceptors
  93. )
  94. }
  95. public func makeCall<Request: GRPCPayload, Response: GRPCPayload>(
  96. path: String,
  97. type: GRPCCallType,
  98. callOptions: CallOptions,
  99. interceptors: [ClientInterceptor<Request, Response>]
  100. ) -> Call<Request, Response> {
  101. return self._makeCall(
  102. path: path,
  103. type: type,
  104. callOptions: callOptions,
  105. interceptors: interceptors
  106. )
  107. }
  108. private func _makeCall<Request: Message, Response: Message>(
  109. path: String,
  110. type: GRPCCallType,
  111. callOptions: CallOptions,
  112. interceptors: [ClientInterceptor<Request, Response>]
  113. ) -> Call<Request, Response> {
  114. let stream: _FakeResponseStream<Request, Response>? = self.dequeueResponseStream(forPath: path)
  115. let eventLoop = stream?.channel.eventLoop ?? EmbeddedEventLoop()
  116. return Call(
  117. path: path,
  118. type: type,
  119. eventLoop: eventLoop,
  120. options: callOptions,
  121. interceptors: interceptors,
  122. transportFactory: .fake(stream)
  123. )
  124. }
  125. private func _makeCall<Request: GRPCPayload, Response: GRPCPayload>(
  126. path: String,
  127. type: GRPCCallType,
  128. callOptions: CallOptions,
  129. interceptors: [ClientInterceptor<Request, Response>]
  130. ) -> Call<Request, Response> {
  131. let stream: _FakeResponseStream<Request, Response>? = self.dequeueResponseStream(forPath: path)
  132. let eventLoop = stream?.channel.eventLoop ?? EmbeddedEventLoop()
  133. return Call(
  134. path: path,
  135. type: type,
  136. eventLoop: eventLoop,
  137. options: callOptions,
  138. interceptors: interceptors,
  139. transportFactory: .fake(stream)
  140. )
  141. }
  142. public func close() -> EventLoopFuture<Void> {
  143. // We don't have anything to close.
  144. return EmbeddedEventLoop().makeSucceededFuture(())
  145. }
  146. }
  147. @available(swift, deprecated: 5.6)
  148. extension FakeChannel {
  149. /// Dequeue a proxy for the given path and casts it to the given type, if one exists.
  150. private func dequeueResponseStream<Stream>(
  151. forPath path: String,
  152. as: Stream.Type = Stream.self
  153. ) -> Stream? {
  154. guard var streams = self.responseStreams[path], !streams.isEmpty else {
  155. return nil
  156. }
  157. // This is fine: we know we're non-empty.
  158. let first = streams.removeFirst()
  159. self.responseStreams.updateValue(streams, forKey: path)
  160. return first as? Stream
  161. }
  162. private func makeRequestHead(path: String, callOptions: CallOptions) -> _GRPCRequestHead {
  163. return _GRPCRequestHead(
  164. scheme: "http",
  165. path: path,
  166. host: "localhost",
  167. options: callOptions,
  168. requestID: nil
  169. )
  170. }
  171. }