ClientTimeoutTests.swift 4.3 KB

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