NIOClientTimeoutTests.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Foundation
  17. import SwiftGRPCNIO
  18. import NIO
  19. import XCTest
  20. class NIOClientTimeoutTests: NIOEchoTestCaseBase {
  21. let optionsWithShortTimeout = CallOptions(timeout: try! GRPCTimeout.milliseconds(10))
  22. let moreThanShortTimeout: TimeInterval = 0.011
  23. static var allTests: [(String, (NIOClientTimeoutTests) -> () throws -> Void)] {
  24. return [
  25. ("testUnaryTimeoutAfterSending", testUnaryTimeoutAfterSending),
  26. ("testServerStreamingTimeoutAfterSending", testServerStreamingTimeoutAfterSending),
  27. ("testClientStreamingTimeoutBeforeSending", testClientStreamingTimeoutBeforeSending),
  28. ("testClientStreamingTimeoutAfterSending", testClientStreamingTimeoutAfterSending),
  29. ("testBidirectionalStreamingTimeoutBeforeSending", testBidirectionalStreamingTimeoutBeforeSending),
  30. ("testBidirectionalStreamingTimeoutAfterSending", testBidirectionalStreamingTimeoutAfterSending)
  31. ]
  32. }
  33. private func expectDeadlineExceeded(forStatus status: EventLoopFuture<GRPCStatus>) {
  34. let statusExpectation = self.expectation(description: "status received")
  35. status.whenSuccess { status in
  36. XCTAssertEqual(status.code, .deadlineExceeded)
  37. statusExpectation.fulfill()
  38. }
  39. status.whenFailure { error in
  40. XCTFail("unexpectedly received error for status: \(error)")
  41. }
  42. }
  43. private func expectDeadlineExceeded(forResponse response: EventLoopFuture<Echo_EchoResponse>) {
  44. let responseExpectation = self.expectation(description: "response received")
  45. response.whenFailure { error in
  46. XCTAssertEqual((error as? GRPCStatus)?.code, .deadlineExceeded)
  47. responseExpectation.fulfill()
  48. }
  49. response.whenSuccess { response in
  50. XCTFail("response received after deadline")
  51. }
  52. }
  53. }
  54. extension NIOClientTimeoutTests {
  55. func testUnaryTimeoutAfterSending() {
  56. // The request gets fired on call creation, so we need a very short timeout.
  57. let callOptions = CallOptions(timeout: try! .milliseconds(1))
  58. let call = client.get(Echo_EchoRequest(text: "foo"), callOptions: callOptions)
  59. self.expectDeadlineExceeded(forStatus: call.status)
  60. self.expectDeadlineExceeded(forResponse: call.response)
  61. waitForExpectations(timeout: defaultTestTimeout)
  62. }
  63. func testServerStreamingTimeoutAfterSending() {
  64. // The request gets fired on call creation, so we need a very short timeout.
  65. let callOptions = CallOptions(timeout: try! .milliseconds(1))
  66. let call = client.expand(Echo_EchoRequest(text: "foo bar baz"), callOptions: callOptions) { _ in }
  67. self.expectDeadlineExceeded(forStatus: call.status)
  68. waitForExpectations(timeout: defaultTestTimeout)
  69. }
  70. func testClientStreamingTimeoutBeforeSending() {
  71. let call = client.collect(callOptions: optionsWithShortTimeout)
  72. self.expectDeadlineExceeded(forStatus: call.status)
  73. self.expectDeadlineExceeded(forResponse: call.response)
  74. waitForExpectations(timeout: defaultTestTimeout)
  75. }
  76. func testClientStreamingTimeoutAfterSending() {
  77. let call = client.collect(callOptions: optionsWithShortTimeout)
  78. self.expectDeadlineExceeded(forStatus: call.status)
  79. self.expectDeadlineExceeded(forResponse: call.response)
  80. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  81. // Timeout before sending `.end`
  82. Thread.sleep(forTimeInterval: moreThanShortTimeout)
  83. call.sendEnd(promise: nil)
  84. waitForExpectations(timeout: defaultTestTimeout)
  85. }
  86. func testBidirectionalStreamingTimeoutBeforeSending() {
  87. let call = client.update(callOptions: optionsWithShortTimeout) { _ in }
  88. self.expectDeadlineExceeded(forStatus: call.status)
  89. Thread.sleep(forTimeInterval: moreThanShortTimeout)
  90. waitForExpectations(timeout: defaultTestTimeout)
  91. }
  92. func testBidirectionalStreamingTimeoutAfterSending() {
  93. let call = client.update(callOptions: optionsWithShortTimeout) { _ in }
  94. self.expectDeadlineExceeded(forStatus: call.status)
  95. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  96. // Timeout before sending `.end`
  97. Thread.sleep(forTimeInterval: moreThanShortTimeout)
  98. call.sendEnd(promise: nil)
  99. waitForExpectations(timeout: defaultTestTimeout)
  100. }
  101. }