ClientTimeoutTests.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. return CallOptions(timeLimit: .deadline(.uptimeNanoseconds(0) + timeout))
  30. }
  31. // Note: this is not related to the call timeout since we're using an EmbeddedChannel. We require
  32. // this in case the timeout doesn't work.
  33. let testTimeout: TimeInterval = 0.1
  34. override func setUp() {
  35. super.setUp()
  36. let connection = EmbeddedGRPCChannel()
  37. XCTAssertNoThrow(try connection.embeddedChannel.connect(to: SocketAddress(unixDomainSocketPath: "/foo")))
  38. let client = Echo_EchoClient(channel: connection, defaultCallOptions: self.callOptions)
  39. self.channel = connection.embeddedChannel
  40. self.client = client
  41. }
  42. override func tearDown() {
  43. XCTAssertNoThrow(try self.channel.finish())
  44. }
  45. func assertRPCTimedOut(_ response: EventLoopFuture<Echo_EchoResponse>, expectation: XCTestExpectation) {
  46. response.whenComplete { result in
  47. switch result {
  48. case .success(let response):
  49. XCTFail("unexpected response: \(response)")
  50. case .failure(let error):
  51. XCTAssertTrue(error is GRPCError.RPCTimedOut)
  52. }
  53. expectation.fulfill()
  54. }
  55. }
  56. func assertDeadlineExceeded(_ status: EventLoopFuture<GRPCStatus>, expectation: XCTestExpectation) {
  57. status.whenComplete { result in
  58. switch result {
  59. case .success(let status):
  60. XCTAssertEqual(status.code, .deadlineExceeded)
  61. case .failure(let error):
  62. XCTFail("unexpected error: \(error)")
  63. }
  64. expectation.fulfill()
  65. }
  66. }
  67. func testUnaryTimeoutAfterSending() throws {
  68. let statusExpectation = self.expectation(description: "status fulfilled")
  69. let call = self.client.get(Echo_EchoRequest(text: "foo"))
  70. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  71. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  72. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  73. }
  74. func testServerStreamingTimeoutAfterSending() throws {
  75. let statusExpectation = self.expectation(description: "status fulfilled")
  76. let call = client.expand(Echo_EchoRequest(text: "foo bar baz")) { _ in }
  77. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  78. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  79. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  80. }
  81. func testClientStreamingTimeoutBeforeSending() throws {
  82. let responseExpectation = self.expectation(description: "response fulfilled")
  83. let statusExpectation = self.expectation(description: "status fulfilled")
  84. let call = client.collect()
  85. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  86. self.assertRPCTimedOut(call.response, expectation: responseExpectation)
  87. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  88. self.wait(for: [responseExpectation, statusExpectation], timeout: self.testTimeout)
  89. }
  90. func testClientStreamingTimeoutAfterSending() throws {
  91. let responseExpectation = self.expectation(description: "response fulfilled")
  92. let statusExpectation = self.expectation(description: "status fulfilled")
  93. let call = client.collect()
  94. self.assertRPCTimedOut(call.response, expectation: responseExpectation)
  95. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  96. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  97. call.sendEnd(promise: nil)
  98. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  99. self.wait(for: [responseExpectation, statusExpectation], timeout: 1.0)
  100. }
  101. func testBidirectionalStreamingTimeoutBeforeSending() {
  102. let statusExpectation = self.expectation(description: "status fulfilled")
  103. let call = client.update { _ in }
  104. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  105. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  106. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  107. }
  108. func testBidirectionalStreamingTimeoutAfterSending() {
  109. let statusExpectation = self.expectation(description: "status fulfilled")
  110. let call = client.update { _ in }
  111. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  112. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  113. call.sendEnd(promise: nil)
  114. channel.embeddedEventLoop.advanceTime(by: self.timeout)
  115. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  116. }
  117. }