NIOFunctionalTests.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2019, 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 NIO
  19. import NIOHTTP1
  20. import NIOHTTP2
  21. @testable import SwiftGRPCNIO
  22. import XCTest
  23. class NIOFunctionalTestsInsecureTransport: NIOEchoTestCaseBase {
  24. override var transportSecurity: TransportSecurity {
  25. return .none
  26. }
  27. var aFewStrings: [String] {
  28. return ["foo", "bar", "baz"]
  29. }
  30. var lotsOfStrings: [String] {
  31. return (0..<5_000).map {
  32. String(describing: $0)
  33. }
  34. }
  35. }
  36. extension NIOFunctionalTestsInsecureTransport {
  37. func makeExpectation(description: String, expectedFulfillmentCount: Int = 1, assertForOverFulfill: Bool = true) -> XCTestExpectation {
  38. let expectation = self.expectation(description: description)
  39. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  40. expectation.assertForOverFulfill = assertForOverFulfill
  41. return expectation
  42. }
  43. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  44. return makeExpectation(description: "Expecting status received",
  45. expectedFulfillmentCount: expectedFulfillmentCount)
  46. }
  47. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  48. return makeExpectation(description: "Expecting \(expectedFulfillmentCount) response(s)",
  49. expectedFulfillmentCount: expectedFulfillmentCount)
  50. }
  51. }
  52. extension NIOFunctionalTestsInsecureTransport {
  53. func doTestUnary(request: Echo_EchoRequest, expect response: Echo_EchoResponse, file: StaticString = #file, line: UInt = #line) {
  54. let responseExpectation = self.makeResponseExpectation()
  55. let statusExpectation = self.makeStatusExpectation()
  56. let call = client.get(request)
  57. call.response.assertEqual(response, fulfill: responseExpectation, file: file, line: line)
  58. call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, file: file, line: line)
  59. self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
  60. }
  61. func doTestUnary(message: String, file: StaticString = #file, line: UInt = #line) {
  62. self.doTestUnary(request: Echo_EchoRequest(text: message), expect: Echo_EchoResponse(text: "Swift echo get: \(message)"), file: file, line: line)
  63. }
  64. func testUnary() throws {
  65. self.doTestUnary(message: "foo")
  66. }
  67. func testUnaryLotsOfRequests() throws {
  68. self.defaultTestTimeout = 60.0
  69. // Sending that many requests at once can sometimes trip things up, it seems.
  70. let clockStart = clock()
  71. let numberOfRequests = 2_000
  72. let responseExpectation = self.makeResponseExpectation(expectedFulfillmentCount: numberOfRequests)
  73. let statusExpectation = self.makeStatusExpectation(expectedFulfillmentCount: numberOfRequests)
  74. for i in 0..<numberOfRequests {
  75. if i % 1_000 == 0 && i > 0 {
  76. print("\(i) requests sent so far, elapsed time: \(Double(clock() - clockStart) / Double(CLOCKS_PER_SEC))")
  77. }
  78. let request = Echo_EchoRequest(text: "foo \(i)")
  79. let response = Echo_EchoResponse(text: "Swift echo get: foo \(i)")
  80. let call = client.get(request)
  81. call.response.assertEqual(response, fulfill: responseExpectation)
  82. call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation)
  83. }
  84. print("total time to send \(numberOfRequests) requests: \(Double(clock() - clockStart) / Double(CLOCKS_PER_SEC))")
  85. self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
  86. print("total time to receive \(numberOfRequests) responses: \(Double(clock() - clockStart) / Double(CLOCKS_PER_SEC))")
  87. }
  88. func testUnaryWithLargeData() throws {
  89. // Default max frame size is: 16,384. We'll exceed this as we also have to send the size and compression flag.
  90. let longMessage = String(repeating: "e", count: 16_384)
  91. self.doTestUnary(message: longMessage)
  92. }
  93. func testUnaryEmptyRequest() throws {
  94. self.doTestUnary(request: Echo_EchoRequest(), expect: Echo_EchoResponse(text: "Swift echo get: "))
  95. }
  96. }
  97. extension NIOFunctionalTestsInsecureTransport {
  98. func doTestClientStreaming(messages: [String], file: StaticString = #file, line: UInt = #line) throws {
  99. let responseExpectation = self.makeResponseExpectation()
  100. let statusExpectation = self.makeStatusExpectation()
  101. let call = client.collect(callOptions: CallOptions(timeout: .infinite))
  102. call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, file: file, line: line)
  103. call.response.assertEqual(Echo_EchoResponse(text: "Swift echo collect: \(messages.joined(separator: " "))"), fulfill: responseExpectation)
  104. var queue = call.newMessageQueue()
  105. for message in messages {
  106. queue = queue.flatMap { call.sendMessage(Echo_EchoRequest(text: message)) }
  107. }
  108. queue.whenSuccess { call.sendEnd(promise: nil) }
  109. self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
  110. }
  111. func testClientStreaming() {
  112. XCTAssertNoThrow(try doTestClientStreaming(messages: aFewStrings))
  113. }
  114. func testClientStreamingLotsOfMessages() throws {
  115. self.defaultTestTimeout = 15.0
  116. XCTAssertNoThrow(try doTestClientStreaming(messages: lotsOfStrings))
  117. }
  118. }
  119. extension NIOFunctionalTestsInsecureTransport {
  120. func doTestServerStreaming(messages: [String], file: StaticString = #file, line: UInt = #line) throws {
  121. let responseExpectation = self.makeResponseExpectation(expectedFulfillmentCount: messages.count)
  122. let statusExpectation = self.makeStatusExpectation()
  123. var iterator = messages.enumerated().makeIterator()
  124. let call = client.expand(Echo_EchoRequest(text: messages.joined(separator: " "))) { response in
  125. if let (index, message) = iterator.next() {
  126. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo expand (\(index)): \(message)"), response, file: file, line: line)
  127. responseExpectation.fulfill()
  128. } else {
  129. XCTFail("Too many responses received", file: file, line: line)
  130. }
  131. }
  132. call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, file: file, line: line)
  133. self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
  134. }
  135. func testServerStreaming() {
  136. XCTAssertNoThrow(try doTestServerStreaming(messages: aFewStrings))
  137. }
  138. func testServerStreamingLotsOfMessages() {
  139. self.defaultTestTimeout = 15.0
  140. XCTAssertNoThrow(try doTestServerStreaming(messages: lotsOfStrings))
  141. }
  142. }
  143. extension NIOFunctionalTestsInsecureTransport {
  144. private func doTestBidirectionalStreaming(messages: [String], waitForEachResponse: Bool = false, file: StaticString = #file, line: UInt = #line) throws {
  145. let responseExpectation = self.makeResponseExpectation(expectedFulfillmentCount: messages.count)
  146. let statusExpectation = self.makeStatusExpectation()
  147. let responseReceived = waitForEachResponse ? DispatchSemaphore(value: 0) : nil
  148. var iterator = messages.enumerated().makeIterator()
  149. let call = client.update { response in
  150. if let (index, message) = iterator.next() {
  151. XCTAssertEqual(Echo_EchoResponse(text: "Swift echo update (\(index)): \(message)"), response, file: file, line: line)
  152. responseExpectation.fulfill()
  153. responseReceived?.signal()
  154. } else {
  155. XCTFail("Too many responses received", file: file, line: line)
  156. }
  157. }
  158. call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, file: file, line: line)
  159. messages.forEach { part in
  160. call.sendMessage(Echo_EchoRequest(text: part), promise: nil)
  161. XCTAssertNotEqual(responseReceived?.wait(timeout: .now() + .seconds(1)), .some(.timedOut), file: file, line: line)
  162. }
  163. call.sendEnd(promise: nil)
  164. self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
  165. }
  166. func testBidirectionalStreamingBatched() throws {
  167. XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: aFewStrings))
  168. }
  169. func testBidirectionalStreamingPingPong() throws {
  170. XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: aFewStrings, waitForEachResponse: true))
  171. }
  172. func testBidirectionalStreamingLotsOfMessagesBatched() throws {
  173. self.defaultTestTimeout = 15.0
  174. XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: lotsOfStrings))
  175. }
  176. func testBidirectionalStreamingLotsOfMessagesPingPong() throws {
  177. self.defaultTestTimeout = 15.0
  178. XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: lotsOfStrings, waitForEachResponse: true))
  179. }
  180. }
  181. class NIOFunctionalTestsAnonymousClient: NIOFunctionalTestsInsecureTransport {
  182. override var transportSecurity: TransportSecurity {
  183. return .anonymousClient
  184. }
  185. }
  186. class NIOFunctionalTestsMutualAuthentication: NIOFunctionalTestsInsecureTransport {
  187. override var transportSecurity: TransportSecurity {
  188. return .mutualAuthentication
  189. }
  190. }