NIOClientTimeoutTests.swift 4.0 KB

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