/* * Copyright 2018, gRPC Authors All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import Dispatch import Foundation @testable import SwiftGRPC import XCTest // Waits 10ms before each send operation and does not log sending errors, // as these are expected when the call times out. private class SleepingEchoProvider: Echo_EchoProvider { func get(request: Echo_EchoRequest, session _: Echo_EchoGetSession) throws -> Echo_EchoResponse { Thread.sleep(forTimeInterval: 0.1) var response = Echo_EchoResponse() response.text = "Swift echo get: " + request.text return response } func expand(request: Echo_EchoRequest, session: Echo_EchoExpandSession) throws -> ServerStatus? { let parts = request.text.components(separatedBy: " ") for (i, part) in parts.enumerated() { Thread.sleep(forTimeInterval: 0.1) var response = Echo_EchoResponse() response.text = "Swift echo expand (\(i)): \(part)" try session.send(response) } return .ok } func collect(session: Echo_EchoCollectSession) throws -> Echo_EchoResponse? { Thread.sleep(forTimeInterval: 0.1) var parts: [String] = [] while true { do { guard let request = try session.receive() else { break } // End of stream parts.append(request.text) } catch { break } } var response = Echo_EchoResponse() response.text = "Swift echo collect: " + parts.joined(separator: " ") return response } func update(session: Echo_EchoUpdateSession) throws -> ServerStatus? { var count = 0 while true { do { Thread.sleep(forTimeInterval: 0.1) guard let request = try session.receive() else { break } // End of stream var response = Echo_EchoResponse() response.text = "Swift echo update (\(count)): \(request.text)" count += 1 try session.send(response) } catch { break } } return .ok } } class ClientCancellingTests: BasicEchoTestCase { static var allTests: [(String, (ClientCancellingTests) -> () throws -> Void)] { return [ ("testUnary", testUnary), ("testClientStreaming", testClientStreaming), ("testServerStreaming", testServerStreaming), ("testBidirectionalStreaming", testBidirectionalStreaming), ] } override func makeProvider() -> Echo_EchoProvider { return SleepingEchoProvider() } } private func manyWords(_ count: Int) -> String { return (0..