ClientTimeoutTests.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2018, 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 Dispatch
  17. import Foundation
  18. @testable import SwiftGRPC
  19. import XCTest
  20. class ClientTimeoutTests: BasicEchoTestCase {
  21. static var allTests: [(String, (ClientTimeoutTests) -> () throws -> Void)] {
  22. return [
  23. ("testClientStreamingTimeoutBeforeSending", testClientStreamingTimeoutBeforeSending),
  24. ("testClientStreamingTimeoutAfterSending", testClientStreamingTimeoutAfterSending)
  25. ]
  26. }
  27. override var defaultTimeout: TimeInterval { return 0.1 }
  28. }
  29. extension ClientTimeoutTests {
  30. func testClientStreamingTimeoutBeforeSending() {
  31. let completionHandlerExpectation = expectation(description: "final completion handler called")
  32. let call = try! client.collect { callResult in
  33. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  34. completionHandlerExpectation.fulfill()
  35. }
  36. Thread.sleep(forTimeInterval: 0.2)
  37. let sendExpectation = expectation(description: "send completion handler 1 called")
  38. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  39. XCTAssertEqual(.unknown, $0 as! CallError)
  40. sendExpectation.fulfill()
  41. }
  42. call.waitForSendOperationsToFinish()
  43. do {
  44. let result = try call.closeAndReceive()
  45. XCTFail("should have thrown, received \(result) instead")
  46. } catch let receiveError {
  47. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  48. }
  49. waitForExpectations(timeout: defaultTimeout)
  50. }
  51. func testClientStreamingTimeoutAfterSending() {
  52. let completionHandlerExpectation = expectation(description: "final completion handler called")
  53. let call = try! client.collect { callResult in
  54. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  55. completionHandlerExpectation.fulfill()
  56. }
  57. let sendExpectation = expectation(description: "send completion handler 1 called")
  58. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  59. call.waitForSendOperationsToFinish()
  60. Thread.sleep(forTimeInterval: 0.2)
  61. do {
  62. let result = try call.closeAndReceive()
  63. XCTFail("should have thrown, received \(result) instead")
  64. } catch let receiveError {
  65. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  66. }
  67. waitForExpectations(timeout: defaultTimeout)
  68. }
  69. func testBidirectionalStreamingTimeoutPassedToReceiveMethod() {
  70. let completionHandlerExpectation = expectation(description: "final completion handler called")
  71. let call = try! client.update { callResult in
  72. XCTAssertEqual(.ok, callResult.statusCode)
  73. completionHandlerExpectation.fulfill()
  74. }
  75. do {
  76. let result = try call.receive(timeout: .now() + .milliseconds(10))
  77. XCTFail("should have thrown, received \(String(describing: result)) instead")
  78. } catch let receiveError {
  79. if case .timedOut = receiveError as! RPCError {
  80. // This is the expected case - we need to formulate this as an if statement to use case-based pattern matching.
  81. } else {
  82. XCTFail("received error \(receiveError) instead of .timedOut")
  83. }
  84. }
  85. try! call.closeSend()
  86. waitForExpectations(timeout: defaultTimeout)
  87. }
  88. // FIXME(danielalm): Add support for setting a maximum timeout on the server, to prevent DoS attacks where clients
  89. // start a ton of calls, but never finish them (i.e. essentially leaking a connection on the server side).
  90. }