ClientTimeoutTests.swift 5.6 KB

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