2
0

ClientTimeoutTests.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 EchoModel
  17. import Foundation
  18. @testable import GRPC
  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) + self.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(
  40. try connection.embeddedChannel
  41. .connect(to: SocketAddress(unixDomainSocketPath: "/foo"))
  42. )
  43. let client = Echo_EchoClient(channel: connection, defaultCallOptions: self.callOptions)
  44. self.channel = connection.embeddedChannel
  45. self.client = client
  46. }
  47. override func tearDown() {
  48. XCTAssertNoThrow(try self.channel.finish())
  49. super.tearDown()
  50. }
  51. func assertRPCTimedOut(
  52. _ response: EventLoopFuture<Echo_EchoResponse>,
  53. expectation: XCTestExpectation
  54. ) {
  55. response.whenComplete { result in
  56. switch result {
  57. case let .success(response):
  58. XCTFail("unexpected response: \(response)")
  59. case let .failure(error):
  60. XCTAssertTrue(error is GRPCError.RPCTimedOut)
  61. }
  62. expectation.fulfill()
  63. }
  64. }
  65. func assertDeadlineExceeded(
  66. _ status: EventLoopFuture<GRPCStatus>,
  67. expectation: XCTestExpectation
  68. ) {
  69. status.whenComplete { result in
  70. switch result {
  71. case let .success(status):
  72. XCTAssertEqual(status.code, .deadlineExceeded)
  73. case let .failure(error):
  74. XCTFail("unexpected error: \(error)")
  75. }
  76. expectation.fulfill()
  77. }
  78. }
  79. func testUnaryTimeoutAfterSending() throws {
  80. let statusExpectation = self.expectation(description: "status fulfilled")
  81. let call = self.client.get(Echo_EchoRequest(text: "foo"))
  82. self.channel.embeddedEventLoop.advanceTime(by: self.timeout)
  83. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  84. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  85. }
  86. func testServerStreamingTimeoutAfterSending() throws {
  87. let statusExpectation = self.expectation(description: "status fulfilled")
  88. let call = self.client.expand(Echo_EchoRequest(text: "foo bar baz")) { _ in }
  89. self.channel.embeddedEventLoop.advanceTime(by: self.timeout)
  90. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  91. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  92. }
  93. func testClientStreamingTimeoutBeforeSending() throws {
  94. let responseExpectation = self.expectation(description: "response fulfilled")
  95. let statusExpectation = self.expectation(description: "status fulfilled")
  96. let call = self.client.collect()
  97. self.channel.embeddedEventLoop.advanceTime(by: self.timeout)
  98. self.assertRPCTimedOut(call.response, expectation: responseExpectation)
  99. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  100. self.wait(for: [responseExpectation, statusExpectation], timeout: self.testTimeout)
  101. }
  102. func testClientStreamingTimeoutAfterSending() throws {
  103. let responseExpectation = self.expectation(description: "response fulfilled")
  104. let statusExpectation = self.expectation(description: "status fulfilled")
  105. let call = self.client.collect()
  106. self.assertRPCTimedOut(call.response, expectation: responseExpectation)
  107. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  108. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  109. call.sendEnd(promise: nil)
  110. self.channel.embeddedEventLoop.advanceTime(by: self.timeout)
  111. self.wait(for: [responseExpectation, statusExpectation], timeout: 1.0)
  112. }
  113. func testBidirectionalStreamingTimeoutBeforeSending() {
  114. let statusExpectation = self.expectation(description: "status fulfilled")
  115. let call = self.client.update { _ in }
  116. self.channel.embeddedEventLoop.advanceTime(by: self.timeout)
  117. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  118. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  119. }
  120. func testBidirectionalStreamingTimeoutAfterSending() {
  121. let statusExpectation = self.expectation(description: "status fulfilled")
  122. let call = self.client.update { _ in }
  123. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  124. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  125. call.sendEnd(promise: nil)
  126. self.channel.embeddedEventLoop.advanceTime(by: self.timeout)
  127. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  128. }
  129. }