NIOClientTimeoutTests.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: NIOBasicEchoTestCase {
  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. // This test tends to crash randomly on Linux, so it is disabled there for now.
  31. //("testBidirectionalStreamingTimeoutAfterSending", testBidirectionalStreamingTimeoutAfterSending),
  32. ]
  33. }
  34. private func expectDeadlineExceeded(forStatus status: EventLoopFuture<GRPCStatus>) {
  35. let statusExpectation = self.expectation(description: "status received")
  36. status.whenSuccess { status in
  37. XCTAssertEqual(status.code, .deadlineExceeded)
  38. statusExpectation.fulfill()
  39. }
  40. status.whenFailure { error in
  41. XCTFail("unexpectedly received error for status: \(error)")
  42. }
  43. }
  44. private func expectDeadlineExceeded(forResponse response: EventLoopFuture<Echo_EchoResponse>) {
  45. let responseExpectation = self.expectation(description: "response received")
  46. response.whenFailure { error in
  47. XCTAssertEqual((error as? GRPCStatus)?.code, .deadlineExceeded)
  48. responseExpectation.fulfill()
  49. }
  50. response.whenSuccess { response in
  51. XCTFail("response received after deadline")
  52. }
  53. }
  54. }
  55. extension NIOClientTimeoutTests {
  56. func testUnaryTimeoutAfterSending() {
  57. // The request gets fired on call creation, so we need a very short timeout.
  58. let callOptions = CallOptions(timeout: try! .milliseconds(1))
  59. let call = client.get(Echo_EchoRequest(text: "foo"), callOptions: callOptions)
  60. self.expectDeadlineExceeded(forStatus: call.status)
  61. self.expectDeadlineExceeded(forResponse: call.response)
  62. waitForExpectations(timeout: defaultTestTimeout)
  63. }
  64. func testServerStreamingTimeoutAfterSending() {
  65. // The request gets fired on call creation, so we need a very short timeout.
  66. let callOptions = CallOptions(timeout: try! .milliseconds(1))
  67. let call = client.expand(Echo_EchoRequest(text: "foo bar baz"), callOptions: callOptions) { _ in }
  68. self.expectDeadlineExceeded(forStatus: call.status)
  69. waitForExpectations(timeout: defaultTestTimeout)
  70. }
  71. func testClientStreamingTimeoutBeforeSending() {
  72. let call = client.collect(callOptions: optionsWithShortTimeout)
  73. self.expectDeadlineExceeded(forStatus: call.status)
  74. self.expectDeadlineExceeded(forResponse: call.response)
  75. waitForExpectations(timeout: defaultTestTimeout)
  76. }
  77. func testClientStreamingTimeoutAfterSending() {
  78. let call = client.collect(callOptions: optionsWithShortTimeout)
  79. self.expectDeadlineExceeded(forStatus: call.status)
  80. self.expectDeadlineExceeded(forResponse: call.response)
  81. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  82. // Timeout before sending `.end`
  83. Thread.sleep(forTimeInterval: moreThanShortTimeout)
  84. call.sendEnd(promise: nil)
  85. waitForExpectations(timeout: defaultTestTimeout)
  86. }
  87. func testBidirectionalStreamingTimeoutBeforeSending() {
  88. let call = client.update(callOptions: optionsWithShortTimeout) { _ in }
  89. self.expectDeadlineExceeded(forStatus: call.status)
  90. Thread.sleep(forTimeInterval: moreThanShortTimeout)
  91. waitForExpectations(timeout: defaultTestTimeout)
  92. }
  93. func testBidirectionalStreamingTimeoutAfterSending() {
  94. let call = client.update(callOptions: optionsWithShortTimeout) { _ in }
  95. self.expectDeadlineExceeded(forStatus: call.status)
  96. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  97. // Timeout before sending `.end`
  98. Thread.sleep(forTimeInterval: moreThanShortTimeout)
  99. call.sendEnd(promise: nil)
  100. waitForExpectations(timeout: defaultTestTimeout)
  101. }
  102. }