ClientCallBidirectionalStreaming.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2018, 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 Dispatch
  17. import Foundation
  18. import SwiftProtobuf
  19. public protocol ClientCallBidirectionalStreaming: ClientCall {
  20. func waitForSendOperationsToFinish()
  21. // TODO: Move the other, message type-dependent, methods into this protocol. At the moment, this is not possible,
  22. // as the protocol would then have an associated type requirement (and become pretty much unusable in the process).
  23. }
  24. open class ClientCallBidirectionalStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallBidirectionalStreaming, StreamReceiving {
  25. public typealias ReceivedType = OutputType
  26. /// Call this to start a call. Nonblocking.
  27. public func start(metadata: Metadata, completion: ((CallResult) -> Void)?)
  28. throws -> Self {
  29. try call.start(.bidiStreaming, metadata: metadata, completion: completion)
  30. return self
  31. }
  32. public func send(_ message: InputType, completion: @escaping (Error?) -> Void) throws {
  33. let messageData = try message.serializedData()
  34. try call.sendMessage(data: messageData, completion: completion)
  35. }
  36. public func closeSend(completion: (() -> Void)?) throws {
  37. try call.close(completion: completion)
  38. }
  39. public func closeSend() throws {
  40. let sem = DispatchSemaphore(value: 0)
  41. try closeSend {
  42. sem.signal()
  43. }
  44. _ = sem.wait()
  45. }
  46. public func waitForSendOperationsToFinish() {
  47. call.messageQueueEmpty.wait()
  48. }
  49. }
  50. /// Simple fake implementation of ClientCallBidirectionalStreamingBase that returns a previously-defined set of results
  51. /// and stores sent values for later verification.
  52. open class ClientCallBidirectionalStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallBidirectionalStreaming {
  53. open class var method: String { fatalError("needs to be overridden") }
  54. open var inputs: [InputType] = []
  55. open var outputs: [OutputType] = []
  56. public init() {}
  57. open func receive(completion: @escaping (ResultOrRPCError<OutputType?>) -> Void) throws {
  58. completion(.result(outputs.first))
  59. outputs.removeFirst()
  60. }
  61. open func receive() throws -> OutputType? {
  62. defer { outputs.removeFirst() }
  63. return outputs.first
  64. }
  65. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  66. inputs.append(message)
  67. }
  68. open func closeSend(completion: (() -> Void)?) throws { completion?() }
  69. open func closeSend() throws {}
  70. open func waitForSendOperationsToFinish() {}
  71. open func cancel() {}
  72. }