ClientTimeoutTests.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. @testable import GRPC
  18. import EchoModel
  19. import NIO
  20. import XCTest
  21. class ClientTimeoutTests: GRPCTestCase {
  22. var channel: EmbeddedChannel!
  23. var client: Echo_EchoClient!
  24. let timeout = TimeAmount.milliseconds(100)
  25. var callOptions: CallOptions {
  26. // We use a deadline here because internally we convert timeouts into deadlines by diffing
  27. // with `DispatchTime.now()`. We therefore need the deadline to be known in advance. Note we
  28. // use zero because `EmbeddedEventLoop`s time starts at zero.
  29. var options = self.callOptionsWithLogger
  30. options.timeLimit = .deadline(.uptimeNanoseconds(0) + timeout)
  31. return options
  32. }
  33. // Note: this is not related to the call timeout since we're using an EmbeddedChannel. We require
  34. // this in case the timeout doesn't work.
  35. let testTimeout: TimeInterval = 0.1
  36. override func setUp() {
  37. super.setUp()
  38. let connection = EmbeddedGRPCChannel(logger: self.clientLogger)
  39. XCTAssertNoThrow(try connection.embeddedChannel.connect(to: SocketAddress(unixDomainSocketPath: "/foo")))
  40. let client = Echo_EchoClient(channel: connection, defaultCallOptions: self.callOptions)
  41. self.channel = connection.embeddedChannel
  42. self.client = client
  43. }
  44. override func tearDown() {
  45. XCTAssertNoThrow(try self.channel.finish())
  46. super.tearDown()
  47. }
  48. func assertRPCTimedOut(_ response: EventLoopFuture<Echo_EchoResponse>, expectation: XCTestExpectation) {
  49. response.whenComplete { result in
  50. switch result {
  51. case .success(let response):
  52. XCTFail("unexpected response: \(response)")
  53. case .failure(let error):
  54. XCTAssertTrue(error is GRPCError.RPCTimedOut)
  55. }
  56. expectation.fulfill()
  57. }
  58. }
  59. func assertDeadlineExceeded(_ status: EventLoopFuture<GRPCStatus>, expectation: XCTestExpectation) {
  60. status.whenComplete { result in
  61. switch result {
  62. case .success(let status):
  63. XCTAssertEqual(status.code, .deadlineExceeded)
  64. case .failure(let error):
  65. XCTFail("unexpected error: \(error)")
  66. }
  67. expectation.fulfill()
  68. }
  69. }
  70. func testUnaryTimeoutAfterSending() throws {
  71. let statusExpectation = self.expectation(description: "status fulfilled")
  72. let call = self.client.get(Echo_EchoRequest(text: "foo"))
  73. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  74. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  75. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  76. }
  77. func testServerStreamingTimeoutAfterSending() throws {
  78. let statusExpectation = self.expectation(description: "status fulfilled")
  79. let call = client.expand(Echo_EchoRequest(text: "foo bar baz")) { _ in }
  80. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  81. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  82. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  83. }
  84. func testClientStreamingTimeoutBeforeSending() throws {
  85. let responseExpectation = self.expectation(description: "response fulfilled")
  86. let statusExpectation = self.expectation(description: "status fulfilled")
  87. let call = client.collect()
  88. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  89. self.assertRPCTimedOut(call.response, expectation: responseExpectation)
  90. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  91. self.wait(for: [responseExpectation, statusExpectation], timeout: self.testTimeout)
  92. }
  93. func testClientStreamingTimeoutAfterSending() throws {
  94. let responseExpectation = self.expectation(description: "response fulfilled")
  95. let statusExpectation = self.expectation(description: "status fulfilled")
  96. let call = client.collect()
  97. self.assertRPCTimedOut(call.response, expectation: responseExpectation)
  98. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  99. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  100. call.sendEnd(promise: nil)
  101. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  102. self.wait(for: [responseExpectation, statusExpectation], timeout: 1.0)
  103. }
  104. func testBidirectionalStreamingTimeoutBeforeSending() {
  105. let statusExpectation = self.expectation(description: "status fulfilled")
  106. let call = client.update { _ in }
  107. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  108. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  109. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  110. }
  111. func testBidirectionalStreamingTimeoutAfterSending() {
  112. let statusExpectation = self.expectation(description: "status fulfilled")
  113. let call = client.update { _ in }
  114. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  115. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  116. call.sendEnd(promise: nil)
  117. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  118. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  119. }
  120. }