ClientTimeoutTests.swift 5.5 KB

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