NIOServerTests.swift 6.4 KB

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