ClientCallBidirectionalStreaming.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, StreamSending {
  25. public typealias ReceivedType = OutputType
  26. public typealias SentType = InputType
  27. /// Call this to start a call. Nonblocking.
  28. public func start(metadata: Metadata, completion: ((CallResult) -> Void)?)
  29. throws -> Self {
  30. try call.start(.bidiStreaming, metadata: metadata, completion: completion)
  31. return self
  32. }
  33. public func send(_ message: InputType, completion: @escaping (Error?) -> Void) throws {
  34. let messageData = try message.serializedData()
  35. try call.sendMessage(data: messageData, completion: completion)
  36. }
  37. public func closeSend(completion: (() -> Void)?) throws {
  38. try call.close(completion: completion)
  39. }
  40. public func closeSend() throws {
  41. let sem = DispatchSemaphore(value: 0)
  42. try closeSend {
  43. sem.signal()
  44. }
  45. _ = sem.wait()
  46. }
  47. public func waitForSendOperationsToFinish() {
  48. call.messageQueueEmpty.wait()
  49. }
  50. }
  51. /// Simple fake implementation of ClientCallBidirectionalStreamingBase that returns a previously-defined set of results
  52. /// and stores sent values for later verification.
  53. open class ClientCallBidirectionalStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallBidirectionalStreaming {
  54. open class var method: String { fatalError("needs to be overridden") }
  55. open var inputs: [InputType] = []
  56. open var outputs: [OutputType] = []
  57. public init() {}
  58. open func receive(completion: @escaping (ResultOrRPCError<OutputType?>) -> Void) throws {
  59. completion(.result(outputs.first))
  60. outputs.removeFirst()
  61. }
  62. open func receive() throws -> OutputType? {
  63. defer { outputs.removeFirst() }
  64. return outputs.first
  65. }
  66. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  67. inputs.append(message)
  68. }
  69. open func closeSend(completion: (() -> Void)?) throws { completion?() }
  70. open func closeSend() throws {}
  71. open func waitForSendOperationsToFinish() {}
  72. open func cancel() {}
  73. }