/* * Copyright 2019, 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 import NIO import NIOHTTP1 import NIOHTTP2 @testable import GRPC import EchoModel import XCTest class FunctionalTestsInsecureTransport: EchoTestCaseBase { override var transportSecurity: TransportSecurity { return .none } var aFewStrings: [String] { return ["foo", "bar", "baz"] } var lotsOfStrings: [String] { return (0..<5_000).map { String(describing: $0) } } func doTestUnary(request: Echo_EchoRequest, expect response: Echo_EchoResponse, file: StaticString = #file, line: UInt = #line) { let responseExpectation = self.makeResponseExpectation() let statusExpectation = self.makeStatusExpectation() let call = client.get(request) call.response.assertEqual(response, fulfill: responseExpectation, file: file, line: line) call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, file: file, line: line) self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout) } func doTestUnary(message: String, file: StaticString = #file, line: UInt = #line) { self.doTestUnary(request: Echo_EchoRequest(text: message), expect: Echo_EchoResponse(text: "Swift echo get: \(message)"), file: file, line: line) } func testUnary() throws { self.doTestUnary(message: "foo") } func testUnaryLotsOfRequests() throws { // Sending that many requests at once can sometimes trip things up, it seems. let clockStart = clock() let numberOfRequests = 2_000 // Due to https://github.com/apple/swift-nio-http2/issues/87#issuecomment-483542401 we need to // limit the number of active streams. The default in NIOHTTP2 is 100, so we'll use it too. // // In the future we might want to build in some kind of mechanism which handles this for the // user. let batchSize = 100 // Instead of setting a timeout out on the test we'll set one for each batch, if any of them // timeout then we'll bail out of the test. let batchTimeout: TimeInterval = 5.0 self.continueAfterFailure = false for lowerBound in stride(from: 0, to: numberOfRequests, by: batchSize) { let upperBound = min(lowerBound + batchSize, numberOfRequests) let numberOfCalls = upperBound - lowerBound let responseExpectation = self.makeResponseExpectation(expectedFulfillmentCount: numberOfCalls) let statusExpectation = self.makeStatusExpectation(expectedFulfillmentCount: numberOfCalls) for i in lowerBound..