ServerSessionServerStreaming.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 ServerSessionServerStreaming: ServerSession {
  20. func waitForSendOperationsToFinish()
  21. }
  22. open class ServerSessionServerStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionServerStreaming, StreamSending {
  23. public typealias SentType = OutputType
  24. public typealias ProviderBlock = (InputType, ServerSessionServerStreamingBase) throws -> ServerStatus?
  25. private var providerBlock: ProviderBlock
  26. public init(handler: Handler, providerBlock: @escaping ProviderBlock) {
  27. self.providerBlock = providerBlock
  28. super.init(handler: handler)
  29. }
  30. public func run() throws -> ServerStatus? {
  31. let requestData = try receiveRequestAndWait()
  32. let requestMessage = try InputType(serializedData: requestData)
  33. do {
  34. return try self.providerBlock(requestMessage, self)
  35. } catch {
  36. // Errors thrown by `providerBlock` should be logged in that method;
  37. // we return the error as a status code to avoid `ServiceServer` logging this as a "really unexpected" error.
  38. return (error as? ServerStatus) ?? .processingError
  39. }
  40. }
  41. }
  42. /// Simple fake implementation of ServerSessionServerStreaming that returns a previously-defined set of results
  43. /// and stores sent values for later verification.
  44. open class ServerSessionServerStreamingTestStub<OutputType: Message>: ServerSessionTestStub, ServerSessionServerStreaming {
  45. open var lock = Mutex()
  46. open var outputs: [OutputType] = []
  47. open var status: ServerStatus?
  48. open func send(_ message: OutputType, completion _: @escaping (Error?) -> Void) throws {
  49. lock.synchronize { outputs.append(message) }
  50. }
  51. open func _send(_ message: OutputType, timeout: DispatchTime) throws {
  52. lock.synchronize { outputs.append(message) }
  53. }
  54. open func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws {
  55. lock.synchronize { self.status = status }
  56. completion?()
  57. }
  58. open func waitForSendOperationsToFinish() {}
  59. }