FunctionalTests.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 EchoModel
  18. import Foundation
  19. import NIOCore
  20. import NIOHTTP1
  21. import NIOHTTP2
  22. import XCTest
  23. @testable import GRPC
  24. class FunctionalTestsInsecureTransport: EchoTestCaseBase {
  25. override var transportSecurity: TransportSecurity {
  26. return .none
  27. }
  28. var aFewStrings: [String] {
  29. return ["foo", "bar", "baz"]
  30. }
  31. var lotsOfStrings: [String] {
  32. return (0 ..< 500).map {
  33. String(describing: $0)
  34. }
  35. }
  36. func doTestUnary(
  37. request: Echo_EchoRequest,
  38. expect response: Echo_EchoResponse,
  39. file: StaticString = #filePath,
  40. line: UInt = #line
  41. ) {
  42. let responseExpectation = self.makeResponseExpectation()
  43. let statusExpectation = self.makeStatusExpectation()
  44. let call = client.get(request)
  45. call.response.assertEqual(response, fulfill: responseExpectation, file: file, line: line)
  46. call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, file: file, line: line)
  47. self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
  48. }
  49. func doTestUnary(message: String, file: StaticString = #filePath, line: UInt = #line) {
  50. self.doTestUnary(
  51. request: Echo_EchoRequest(text: message),
  52. expect: Echo_EchoResponse(text: "Swift echo get: \(message)"),
  53. file: file,
  54. line: line
  55. )
  56. }
  57. func testUnary() throws {
  58. self.doTestUnary(message: "foo")
  59. }
  60. func testUnaryLotsOfRequests() throws {
  61. guard self.runTimeSensitiveTests() else { return }
  62. // Sending that many requests at once can sometimes trip things up, it seems.
  63. let clockStart = clock()
  64. let numberOfRequests = 200
  65. // Due to https://github.com/apple/swift-nio-http2/issues/87#issuecomment-483542401 we need to
  66. // limit the number of active streams. The default in NIOHTTP2 is 100, so we'll use it too.
  67. //
  68. // In the future we might want to build in some kind of mechanism which handles this for the
  69. // user.
  70. let batchSize = 100
  71. // Instead of setting a timeout out on the test we'll set one for each batch, if any of them
  72. // timeout then we'll bail out of the test.
  73. let batchTimeout: TimeInterval = 30.0
  74. self.continueAfterFailure = false
  75. for lowerBound in stride(from: 0, to: numberOfRequests, by: batchSize) {
  76. let upperBound = min(lowerBound + batchSize, numberOfRequests)
  77. let numberOfCalls = upperBound - lowerBound
  78. let responseExpectation =
  79. self
  80. .makeResponseExpectation(expectedFulfillmentCount: numberOfCalls)
  81. let statusExpectation = self.makeStatusExpectation(expectedFulfillmentCount: numberOfCalls)
  82. for i in lowerBound ..< upperBound {
  83. let request = Echo_EchoRequest(text: "foo \(i)")
  84. let response = Echo_EchoResponse(text: "Swift echo get: foo \(i)")
  85. let get = client.get(request)
  86. get.response.assertEqual(response, fulfill: responseExpectation)
  87. get.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation)
  88. }
  89. if upperBound % 100 == 0 {
  90. print(
  91. "\(upperBound) requests sent so far, elapsed time: \(Double(clock() - clockStart) / Double(CLOCKS_PER_SEC))"
  92. )
  93. }
  94. self.wait(for: [responseExpectation, statusExpectation], timeout: batchTimeout)
  95. }
  96. print(
  97. "total time to receive \(numberOfRequests) responses: \(Double(clock() - clockStart) / Double(CLOCKS_PER_SEC))"
  98. )
  99. }
  100. func testUnaryWithLargeData() throws {
  101. // Default max frame size is: 16,384. We'll exceed this as we also have to send the size and compression flag.
  102. let longMessage = String(repeating: "e", count: 16384)
  103. self.doTestUnary(message: longMessage)
  104. }
  105. func testUnaryEmptyRequest() throws {
  106. self.doTestUnary(
  107. request: Echo_EchoRequest(),
  108. expect: Echo_EchoResponse(text: "Swift echo get: ")
  109. )
  110. }
  111. func doTestClientStreaming(
  112. messages: [String],
  113. file: StaticString = #filePath,
  114. line: UInt = #line
  115. ) throws {
  116. let responseExpectation = self.makeResponseExpectation()
  117. let statusExpectation = self.makeStatusExpectation()
  118. let call = client.collect(callOptions: CallOptions(timeLimit: .none))
  119. call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, file: file, line: line)
  120. call.response.assertEqual(
  121. Echo_EchoResponse(text: "Swift echo collect: \(messages.joined(separator: " "))"),
  122. fulfill: responseExpectation
  123. )
  124. call.sendMessages(messages.map { .init(text: $0) }, promise: nil)
  125. call.sendEnd(promise: nil)
  126. self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
  127. }
  128. func testClientStreaming() {
  129. XCTAssertNoThrow(try self.doTestClientStreaming(messages: self.aFewStrings))
  130. }
  131. func testClientStreamingLotsOfMessages() throws {
  132. guard self.runTimeSensitiveTests() else { return }
  133. XCTAssertNoThrow(try self.doTestClientStreaming(messages: self.lotsOfStrings))
  134. }
  135. private func doTestServerStreaming(messages: [String], line: UInt = #line) throws {
  136. let responseExpectation = self.makeResponseExpectation(expectedFulfillmentCount: messages.count)
  137. let statusExpectation = self.makeStatusExpectation()
  138. var iterator = messages.enumerated().makeIterator()
  139. let call = client.expand(Echo_EchoRequest(text: messages.joined(separator: " "))) { response in
  140. if let (index, message) = iterator.next() {
  141. XCTAssertEqual(
  142. Echo_EchoResponse(text: "Swift echo expand (\(index)): \(message)"),
  143. response,
  144. line: line
  145. )
  146. responseExpectation.fulfill()
  147. } else {
  148. XCTFail("Too many responses received", line: line)
  149. }
  150. }
  151. call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, line: line)
  152. self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
  153. }
  154. func testServerStreaming() {
  155. XCTAssertNoThrow(try self.doTestServerStreaming(messages: self.aFewStrings))
  156. }
  157. func testServerStreamingLotsOfMessages() {
  158. guard self.runTimeSensitiveTests() else { return }
  159. XCTAssertNoThrow(try self.doTestServerStreaming(messages: self.lotsOfStrings))
  160. }
  161. private func doTestBidirectionalStreaming(
  162. messages: [String],
  163. waitForEachResponse: Bool = false,
  164. line: UInt = #line
  165. ) throws {
  166. let responseExpectation = self.makeResponseExpectation(expectedFulfillmentCount: messages.count)
  167. let statusExpectation = self.makeStatusExpectation()
  168. let responseReceived = waitForEachResponse ? DispatchSemaphore(value: 0) : nil
  169. var iterator = messages.enumerated().makeIterator()
  170. let call = client.update { response in
  171. if let (index, message) = iterator.next() {
  172. XCTAssertEqual(
  173. Echo_EchoResponse(text: "Swift echo update (\(index)): \(message)"),
  174. response,
  175. line: line
  176. )
  177. responseExpectation.fulfill()
  178. responseReceived?.signal()
  179. } else {
  180. XCTFail("Too many responses received", line: line)
  181. }
  182. }
  183. call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, line: line)
  184. messages.forEach { part in
  185. call.sendMessage(Echo_EchoRequest(text: part), promise: nil)
  186. XCTAssertNotEqual(
  187. responseReceived?.wait(timeout: .now() + .seconds(30)),
  188. .some(.timedOut),
  189. line: line
  190. )
  191. }
  192. call.sendEnd(promise: nil)
  193. self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
  194. }
  195. func testBidirectionalStreamingBatched() throws {
  196. XCTAssertNoThrow(try self.doTestBidirectionalStreaming(messages: self.aFewStrings))
  197. }
  198. func testBidirectionalStreamingPingPong() throws {
  199. XCTAssertNoThrow(
  200. try self
  201. .doTestBidirectionalStreaming(messages: self.aFewStrings, waitForEachResponse: true)
  202. )
  203. }
  204. func testBidirectionalStreamingLotsOfMessagesBatched() throws {
  205. guard self.runTimeSensitiveTests() else { return }
  206. XCTAssertNoThrow(try self.doTestBidirectionalStreaming(messages: self.lotsOfStrings))
  207. }
  208. func testBidirectionalStreamingLotsOfMessagesPingPong() throws {
  209. guard self.runTimeSensitiveTests() else { return }
  210. XCTAssertNoThrow(
  211. try self
  212. .doTestBidirectionalStreaming(messages: self.lotsOfStrings, waitForEachResponse: true)
  213. )
  214. }
  215. }
  216. #if canImport(NIOSSL)
  217. class FunctionalTestsAnonymousClient: FunctionalTestsInsecureTransport {
  218. override var transportSecurity: TransportSecurity {
  219. return .anonymousClient
  220. }
  221. override func testUnary() throws {
  222. try super.testUnary()
  223. }
  224. override func testUnaryLotsOfRequests() throws {
  225. try super.testUnaryLotsOfRequests()
  226. }
  227. override func testUnaryWithLargeData() throws {
  228. try super.testUnaryWithLargeData()
  229. }
  230. override func testUnaryEmptyRequest() throws {
  231. try super.testUnaryEmptyRequest()
  232. }
  233. override func testClientStreaming() {
  234. super.testClientStreaming()
  235. }
  236. override func testClientStreamingLotsOfMessages() throws {
  237. try super.testClientStreamingLotsOfMessages()
  238. }
  239. override func testServerStreaming() {
  240. super.testServerStreaming()
  241. }
  242. override func testServerStreamingLotsOfMessages() {
  243. super.testServerStreamingLotsOfMessages()
  244. }
  245. override func testBidirectionalStreamingBatched() throws {
  246. try super.testBidirectionalStreamingBatched()
  247. }
  248. override func testBidirectionalStreamingPingPong() throws {
  249. try super.testBidirectionalStreamingPingPong()
  250. }
  251. override func testBidirectionalStreamingLotsOfMessagesBatched() throws {
  252. try super.testBidirectionalStreamingLotsOfMessagesBatched()
  253. }
  254. override func testBidirectionalStreamingLotsOfMessagesPingPong() throws {
  255. try super.testBidirectionalStreamingLotsOfMessagesPingPong()
  256. }
  257. }
  258. class FunctionalTestsMutualAuthentication: FunctionalTestsInsecureTransport {
  259. override var transportSecurity: TransportSecurity {
  260. return .mutualAuthentication
  261. }
  262. override func testUnary() throws {
  263. try super.testUnary()
  264. }
  265. override func testUnaryLotsOfRequests() throws {
  266. try super.testUnaryLotsOfRequests()
  267. }
  268. override func testUnaryWithLargeData() throws {
  269. try super.testUnaryWithLargeData()
  270. }
  271. override func testUnaryEmptyRequest() throws {
  272. try super.testUnaryEmptyRequest()
  273. }
  274. override func testClientStreaming() {
  275. super.testClientStreaming()
  276. }
  277. override func testClientStreamingLotsOfMessages() throws {
  278. try super.testClientStreamingLotsOfMessages()
  279. }
  280. override func testServerStreaming() {
  281. super.testServerStreaming()
  282. }
  283. override func testServerStreamingLotsOfMessages() {
  284. super.testServerStreamingLotsOfMessages()
  285. }
  286. override func testBidirectionalStreamingBatched() throws {
  287. try super.testBidirectionalStreamingBatched()
  288. }
  289. override func testBidirectionalStreamingPingPong() throws {
  290. try super.testBidirectionalStreamingPingPong()
  291. }
  292. override func testBidirectionalStreamingLotsOfMessagesBatched() throws {
  293. try super.testBidirectionalStreamingLotsOfMessagesBatched()
  294. }
  295. override func testBidirectionalStreamingLotsOfMessagesPingPong() throws {
  296. try super.testBidirectionalStreamingLotsOfMessagesPingPong()
  297. }
  298. }
  299. #endif // canImport(NIOSSL)
  300. // MARK: - Variants using NIO TS and Network.framework
  301. // Unfortunately `swift test --generate-linuxmain` uses the macOS test discovery. Because of this
  302. // it's difficult to avoid tests which run on Linux. To get around this shortcoming we can just
  303. // run no-op tests on Linux.
  304. @available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  305. class FunctionalTestsInsecureTransportNIOTS: FunctionalTestsInsecureTransport {
  306. override var networkPreference: NetworkPreference {
  307. #if canImport(Network)
  308. return .userDefined(.networkFramework)
  309. #else
  310. // We shouldn't need this, since the tests won't do anything. However, we still need to be able
  311. // to compile this class.
  312. return .userDefined(.posix)
  313. #endif
  314. }
  315. override func testBidirectionalStreamingBatched() throws {
  316. #if canImport(Network)
  317. try super.testBidirectionalStreamingBatched()
  318. #endif
  319. }
  320. override func testBidirectionalStreamingLotsOfMessagesBatched() throws {
  321. #if canImport(Network)
  322. try super.testBidirectionalStreamingLotsOfMessagesBatched()
  323. #endif
  324. }
  325. override func testBidirectionalStreamingLotsOfMessagesPingPong() throws {
  326. #if canImport(Network)
  327. try super.testBidirectionalStreamingLotsOfMessagesPingPong()
  328. #endif
  329. }
  330. override func testBidirectionalStreamingPingPong() throws {
  331. #if canImport(Network)
  332. try super.testBidirectionalStreamingPingPong()
  333. #endif
  334. }
  335. override func testClientStreaming() {
  336. #if canImport(Network)
  337. super.testClientStreaming()
  338. #endif
  339. }
  340. override func testClientStreamingLotsOfMessages() throws {
  341. #if canImport(Network)
  342. try super.testClientStreamingLotsOfMessages()
  343. #endif
  344. }
  345. override func testServerStreaming() {
  346. #if canImport(Network)
  347. super.testServerStreaming()
  348. #endif
  349. }
  350. override func testServerStreamingLotsOfMessages() {
  351. #if canImport(Network)
  352. super.testServerStreamingLotsOfMessages()
  353. #endif
  354. }
  355. override func testUnary() throws {
  356. #if canImport(Network)
  357. try super.testUnary()
  358. #endif
  359. }
  360. override func testUnaryEmptyRequest() throws {
  361. #if canImport(Network)
  362. try super.testUnaryEmptyRequest()
  363. #endif
  364. }
  365. override func testUnaryLotsOfRequests() throws {
  366. #if canImport(Network)
  367. try super.testUnaryLotsOfRequests()
  368. #endif
  369. }
  370. override func testUnaryWithLargeData() throws {
  371. #if canImport(Network)
  372. try super.testUnaryWithLargeData()
  373. #endif
  374. }
  375. }
  376. #if canImport(NIOSSL)
  377. @available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  378. class FunctionalTestsAnonymousClientNIOTS: FunctionalTestsInsecureTransportNIOTS {
  379. override var transportSecurity: TransportSecurity {
  380. return .anonymousClient
  381. }
  382. override func testUnary() throws {
  383. try super.testUnary()
  384. }
  385. override func testUnaryLotsOfRequests() throws {
  386. try super.testUnaryLotsOfRequests()
  387. }
  388. override func testUnaryWithLargeData() throws {
  389. try super.testUnaryWithLargeData()
  390. }
  391. override func testUnaryEmptyRequest() throws {
  392. try super.testUnaryEmptyRequest()
  393. }
  394. override func testClientStreaming() {
  395. super.testClientStreaming()
  396. }
  397. override func testClientStreamingLotsOfMessages() throws {
  398. try super.testClientStreamingLotsOfMessages()
  399. }
  400. override func testServerStreaming() {
  401. super.testServerStreaming()
  402. }
  403. override func testServerStreamingLotsOfMessages() {
  404. super.testServerStreamingLotsOfMessages()
  405. }
  406. override func testBidirectionalStreamingBatched() throws {
  407. try super.testBidirectionalStreamingBatched()
  408. }
  409. override func testBidirectionalStreamingPingPong() throws {
  410. try super.testBidirectionalStreamingPingPong()
  411. }
  412. override func testBidirectionalStreamingLotsOfMessagesBatched() throws {
  413. try super.testBidirectionalStreamingLotsOfMessagesBatched()
  414. }
  415. override func testBidirectionalStreamingLotsOfMessagesPingPong() throws {
  416. try super.testBidirectionalStreamingLotsOfMessagesPingPong()
  417. }
  418. }
  419. @available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  420. class FunctionalTestsMutualAuthenticationNIOTS: FunctionalTestsInsecureTransportNIOTS {
  421. override var transportSecurity: TransportSecurity {
  422. return .mutualAuthentication
  423. }
  424. override func testUnary() throws {
  425. try super.testUnary()
  426. }
  427. override func testUnaryLotsOfRequests() throws {
  428. try super.testUnaryLotsOfRequests()
  429. }
  430. override func testUnaryWithLargeData() throws {
  431. try super.testUnaryWithLargeData()
  432. }
  433. override func testUnaryEmptyRequest() throws {
  434. try super.testUnaryEmptyRequest()
  435. }
  436. override func testClientStreaming() {
  437. super.testClientStreaming()
  438. }
  439. override func testClientStreamingLotsOfMessages() throws {
  440. try super.testClientStreamingLotsOfMessages()
  441. }
  442. override func testServerStreaming() {
  443. super.testServerStreaming()
  444. }
  445. override func testServerStreamingLotsOfMessages() {
  446. super.testServerStreamingLotsOfMessages()
  447. }
  448. override func testBidirectionalStreamingBatched() throws {
  449. try super.testBidirectionalStreamingBatched()
  450. }
  451. override func testBidirectionalStreamingPingPong() throws {
  452. try super.testBidirectionalStreamingPingPong()
  453. }
  454. override func testBidirectionalStreamingLotsOfMessagesBatched() throws {
  455. try super.testBidirectionalStreamingLotsOfMessagesBatched()
  456. }
  457. override func testBidirectionalStreamingLotsOfMessagesPingPong() throws {
  458. try super.testBidirectionalStreamingLotsOfMessagesPingPong()
  459. }
  460. }
  461. #endif // canImport(NIOSSL)