ClientTimeoutTests.swift 3.7 KB

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