NIOServerTests.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 NIO
  19. import NIOHTTP1
  20. import NIOHTTP2
  21. @testable import SwiftGRPCNIO
  22. import XCTest
  23. class NIOServerTests: NIOBasicEchoTestCase {
  24. static var allTests: [(String, (NIOServerTests) -> () throws -> Void)] {
  25. return [
  26. ("testUnary", testUnary),
  27. ("testUnaryLotsOfRequests", testUnaryLotsOfRequests),
  28. ("testClientStreaming", testClientStreaming),
  29. ("testClientStreamingLotsOfMessages", testClientStreamingLotsOfMessages),
  30. ("testServerStreaming", testServerStreaming),
  31. ("testServerStreamingLotsOfMessages", testServerStreamingLotsOfMessages),
  32. ("testBidirectionalStreamingBatched", testBidirectionalStreamingBatched),
  33. ("testBidirectionalStreamingPingPong", testBidirectionalStreamingPingPong),
  34. ("testBidirectionalStreamingLotsOfMessagesBatched", testBidirectionalStreamingLotsOfMessagesBatched),
  35. ("testBidirectionalStreamingLotsOfMessagesPingPong", testBidirectionalStreamingLotsOfMessagesPingPong)
  36. ]
  37. }
  38. static let aFewStrings = ["foo", "bar", "baz"]
  39. static let lotsOfStrings = (0..<5_000).map { String(describing: $0) }
  40. }
  41. extension NIOServerTests {
  42. func testUnary() throws {
  43. XCTAssertEqual(try client.get(Echo_EchoRequest(text: "foo")).response.wait().text, "Swift echo get: foo")
  44. }
  45. func testUnaryLotsOfRequests() throws {
  46. // Sending that many requests at once can sometimes trip things up, it seems.
  47. let clockStart = clock()
  48. let numberOfRequests = 2_000
  49. for i in 0..<numberOfRequests {
  50. if i % 1_000 == 0 && i > 0 {
  51. print("\(i) requests sent so far, elapsed time: \(Double(clock() - clockStart) / Double(CLOCKS_PER_SEC))")
  52. }
  53. XCTAssertEqual(try client.get(Echo_EchoRequest(text: "foo \(i)")).response.wait().text, "Swift echo get: foo \(i)")
  54. }
  55. print("total time for \(numberOfRequests) requests: \(Double(clock() - clockStart) / Double(CLOCKS_PER_SEC))")
  56. }
  57. func testUnaryWithLargeData() throws {
  58. // Default max frame size is: 16,384. We'll exceed this as we also have to send the size and compression flag.
  59. let longMessage = String(repeating: "e", count: 16_384)
  60. XCTAssertEqual(try client.get(Echo_EchoRequest(text: longMessage)).response.wait().text, "Swift echo get: \(longMessage)")
  61. }
  62. func testUnaryEmptyRequest() throws {
  63. XCTAssertNoThrow(try client.get(Echo_EchoRequest()).response.wait())
  64. }
  65. }
  66. extension NIOServerTests {
  67. func doTestClientStreaming(messages: [String], file: StaticString = #file, line: UInt = #line) throws {
  68. let call = client.collect()
  69. var queue = call.newMessageQueue()
  70. for message in messages {
  71. queue = queue.then { call.sendMessage(Echo_EchoRequest(text: message)) }
  72. }
  73. queue.whenSuccess { call.sendEnd(promise: nil) }
  74. XCTAssertEqual("Swift echo collect: " + messages.joined(separator: " "), try call.response.wait().text, file: file, line: line)
  75. XCTAssertEqual(.ok, try call.status.wait().code, file: file, line: line)
  76. }
  77. func testClientStreaming() {
  78. XCTAssertNoThrow(try doTestClientStreaming(messages: NIOServerTests.aFewStrings))
  79. }
  80. func testClientStreamingLotsOfMessages() throws {
  81. XCTAssertNoThrow(try doTestClientStreaming(messages: NIOServerTests.lotsOfStrings))
  82. }
  83. }
  84. extension NIOServerTests {
  85. func doTestServerStreaming(messages: [String], file: StaticString = #file, line: UInt = #line) throws {
  86. var index = 0
  87. let call = client.expand(Echo_EchoRequest.with { $0.text = messages.joined(separator: " ") }) { response in
  88. XCTAssertEqual("Swift echo expand (\(index)): \(messages[index])", response.text, file: file, line: line)
  89. index += 1
  90. }
  91. XCTAssertEqual(try call.status.wait().code, .ok, file: file, line: line)
  92. XCTAssertEqual(index, messages.count)
  93. }
  94. func testServerStreaming() {
  95. XCTAssertNoThrow(try doTestServerStreaming(messages: NIOServerTests.aFewStrings))
  96. }
  97. func testServerStreamingLotsOfMessages() {
  98. XCTAssertNoThrow(try doTestServerStreaming(messages: NIOServerTests.lotsOfStrings))
  99. }
  100. }
  101. extension NIOServerTests {
  102. private func doTestBidirectionalStreaming(messages: [String], waitForEachResponse: Bool = false, timeout: GRPCTimeout? = nil, file: StaticString = #file, line: UInt = #line) throws {
  103. let responseReceived = waitForEachResponse ? DispatchSemaphore(value: 0) : nil
  104. var index = 0
  105. let callOptions = timeout.map { CallOptions(timeout: $0) }
  106. let call = client.update(callOptions: callOptions) { response in
  107. XCTAssertEqual("Swift echo update (\(index)): \(messages[index])", response.text, file: file, line: line)
  108. responseReceived?.signal()
  109. index += 1
  110. }
  111. messages.forEach { part in
  112. call.sendMessage(Echo_EchoRequest(text: part), promise: nil)
  113. XCTAssertNotEqual(responseReceived?.wait(timeout: .now() + .seconds(1)), .some(.timedOut), file: file, line: line)
  114. }
  115. call.sendEnd(promise: nil)
  116. XCTAssertEqual(try call.status.wait().code, .ok, file: file, line: line)
  117. XCTAssertEqual(index, messages.count)
  118. }
  119. func testBidirectionalStreamingBatched() throws {
  120. XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: NIOServerTests.aFewStrings))
  121. }
  122. func testBidirectionalStreamingPingPong() throws {
  123. XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: NIOServerTests.aFewStrings, waitForEachResponse: true))
  124. }
  125. func testBidirectionalStreamingLotsOfMessagesBatched() throws {
  126. XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: NIOServerTests.lotsOfStrings, timeout: try .seconds(15)))
  127. }
  128. func testBidirectionalStreamingLotsOfMessagesPingPong() throws {
  129. XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: NIOServerTests.lotsOfStrings, waitForEachResponse: true, timeout: try .seconds(15)))
  130. }
  131. }