ClientTimeoutTests.swift 5.3 KB

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