FakeChannel.swift 6.2 KB

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