2
0

FakeChannel.swift 5.7 KB

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