ClientTimeoutTests.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_EchoServiceClient!
  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 channel = EmbeddedChannel()
  34. let configuration = ClientConnection.Configuration(
  35. target: .socketAddress(try! .init(unixDomainSocketPath: "/foo")),
  36. eventLoopGroup: channel.eventLoop
  37. )
  38. let connection = ClientConnection(channel: channel, configuration: configuration)
  39. let client = Echo_EchoServiceClient(connection: connection, defaultCallOptions: self.callOptions)
  40. self.channel = channel
  41. self.client = client
  42. }
  43. override func tearDown() {
  44. XCTAssertNoThrow(try self.channel.finish())
  45. }
  46. func assertDeadlineExceeded(_ response: EventLoopFuture<Echo_EchoResponse>, expectation: XCTestExpectation) {
  47. response.whenComplete { result in
  48. switch result {
  49. case .success(let response):
  50. XCTFail("unexpected response: \(response)")
  51. case .failure(let error):
  52. if let status = error as? GRPCStatus {
  53. XCTAssertEqual(status.code, .deadlineExceeded)
  54. } else {
  55. XCTFail("unexpected error: \(error)")
  56. }
  57. }
  58. expectation.fulfill()
  59. }
  60. }
  61. func assertDeadlineExceeded(_ status: EventLoopFuture<GRPCStatus>, expectation: XCTestExpectation) {
  62. status.whenComplete { result in
  63. switch result {
  64. case .success(let status):
  65. XCTAssertEqual(status.code, .deadlineExceeded)
  66. case .failure(let error):
  67. XCTFail("unexpected error: \(error)")
  68. }
  69. expectation.fulfill()
  70. }
  71. }
  72. func testUnaryTimeoutAfterSending() throws {
  73. let statusExpectation = self.expectation(description: "status fulfilled")
  74. let call = self.client.get(Echo_EchoRequest(text: "foo"))
  75. channel.embeddedEventLoop.advanceTime(by: self.timeout.asNIOTimeAmount)
  76. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  77. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  78. }
  79. func testServerStreamingTimeoutAfterSending() throws {
  80. let statusExpectation = self.expectation(description: "status fulfilled")
  81. let call = client.expand(Echo_EchoRequest(text: "foo bar baz")) { _ in }
  82. channel.embeddedEventLoop.advanceTime(by: self.timeout.asNIOTimeAmount)
  83. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  84. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  85. }
  86. func testClientStreamingTimeoutBeforeSending() throws {
  87. let responseExpectation = self.expectation(description: "response fulfilled")
  88. let statusExpectation = self.expectation(description: "status fulfilled")
  89. let call = client.collect()
  90. channel.embeddedEventLoop.advanceTime(by: self.timeout.asNIOTimeAmount)
  91. self.assertDeadlineExceeded(call.response, expectation: responseExpectation)
  92. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  93. self.wait(for: [responseExpectation, statusExpectation], timeout: self.testTimeout)
  94. }
  95. func testClientStreamingTimeoutAfterSending() throws {
  96. let responseExpectation = self.expectation(description: "response fulfilled")
  97. let statusExpectation = self.expectation(description: "status fulfilled")
  98. let call = client.collect()
  99. self.assertDeadlineExceeded(call.response, expectation: responseExpectation)
  100. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  101. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  102. call.sendEnd(promise: nil)
  103. channel.embeddedEventLoop.advanceTime(by: self.timeout.asNIOTimeAmount)
  104. self.wait(for: [responseExpectation, statusExpectation], timeout: 1.0)
  105. }
  106. func testBidirectionalStreamingTimeoutBeforeSending() {
  107. let statusExpectation = self.expectation(description: "status fulfilled")
  108. let call = client.update { _ in }
  109. channel.embeddedEventLoop.advanceTime(by: self.timeout.asNIOTimeAmount)
  110. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  111. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  112. }
  113. func testBidirectionalStreamingTimeoutAfterSending() {
  114. let statusExpectation = self.expectation(description: "status fulfilled")
  115. let call = client.update { _ in }
  116. self.assertDeadlineExceeded(call.status, expectation: statusExpectation)
  117. call.sendMessage(Echo_EchoRequest(text: "foo"), promise: nil)
  118. call.sendEnd(promise: nil)
  119. channel.embeddedEventLoop.advanceTime(by: self.timeout.asNIOTimeAmount)
  120. self.wait(for: [statusExpectation], timeout: self.testTimeout)
  121. }
  122. }