ClientCallBidirectionalStreaming.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 closeSend(completion: (() -> Void)?) throws {
  34. try call.close(completion: completion)
  35. }
  36. public func closeSend() throws {
  37. let sem = DispatchSemaphore(value: 0)
  38. try closeSend {
  39. sem.signal()
  40. }
  41. _ = sem.wait()
  42. }
  43. }
  44. /// Simple fake implementation of ClientCallBidirectionalStreamingBase that returns a previously-defined set of results
  45. /// and stores sent values for later verification.
  46. open class ClientCallBidirectionalStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallBidirectionalStreaming {
  47. open class var method: String { fatalError("needs to be overridden") }
  48. open var inputs: [InputType] = []
  49. open var outputs: [OutputType] = []
  50. public init() {}
  51. open func _receive(timeout: DispatchTime) throws -> OutputType? {
  52. defer { if !outputs.isEmpty { outputs.removeFirst() } }
  53. return outputs.first
  54. }
  55. open func receive(completion: @escaping (ResultOrRPCError<OutputType?>) -> Void) throws {
  56. completion(.result(try self._receive(timeout: .distantFuture)))
  57. }
  58. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  59. inputs.append(message)
  60. }
  61. open func _send(_ message: InputType, timeout: DispatchTime) throws {
  62. inputs.append(message)
  63. }
  64. open func closeSend(completion: (() -> Void)?) throws { completion?() }
  65. open func closeSend() throws {}
  66. open func waitForSendOperationsToFinish() {}
  67. open func cancel() {}
  68. }