ServerTimeoutTests.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. fileprivate class TimingOutEchoProvider: Echo_EchoProvider {
  21. func get(request: Echo_EchoRequest, session _: Echo_EchoGetSession) throws -> Echo_EchoResponse {
  22. Thread.sleep(forTimeInterval: 0.2)
  23. return Echo_EchoResponse()
  24. }
  25. func expand(request: Echo_EchoRequest, session: Echo_EchoExpandSession) throws -> ServerStatus? {
  26. Thread.sleep(forTimeInterval: 0.2)
  27. return nil
  28. }
  29. func collect(session: Echo_EchoCollectSession) throws -> Echo_EchoResponse? {
  30. Thread.sleep(forTimeInterval: 0.2)
  31. return nil
  32. }
  33. func update(session: Echo_EchoUpdateSession) throws -> ServerStatus? {
  34. Thread.sleep(forTimeInterval: 0.2)
  35. return nil
  36. }
  37. }
  38. class ServerTimeoutTests: BasicEchoTestCase {
  39. static var allTests: [(String, (ServerTimeoutTests) -> () throws -> Void)] {
  40. return [
  41. ("testTimeoutUnary", testTimeoutUnary),
  42. ("testTimeoutClientStreaming", testTimeoutClientStreaming),
  43. ("testTimeoutServerStreaming", testTimeoutServerStreaming),
  44. ("testTimeoutBidirectionalStreaming", testTimeoutBidirectionalStreaming)
  45. ]
  46. }
  47. override func makeProvider() -> Echo_EchoProvider { return TimingOutEchoProvider() }
  48. override var defaultTimeout: TimeInterval { return 0.1 }
  49. }
  50. extension ServerTimeoutTests {
  51. func testTimeoutUnary() {
  52. do {
  53. let result = try client.get(Echo_EchoRequest(text: "foo")).text
  54. XCTFail("should have thrown, received \(result) instead")
  55. } catch {
  56. guard case let .callError(callResult) = error as! RPCError
  57. else { XCTFail("unexpected error \(error)"); return }
  58. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  59. XCTAssertEqual("Deadline Exceeded", callResult.statusMessage)
  60. }
  61. }
  62. func testTimeoutClientStreaming() {
  63. let completionHandlerExpectation = expectation(description: "final completion handler called")
  64. let call = try! client.collect { callResult in
  65. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  66. completionHandlerExpectation.fulfill()
  67. }
  68. let sendExpectation = expectation(description: "send completion handler 1 called")
  69. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  70. // The server only times out later in its lifecycle, so we shouldn't get an error when trying to send a message.
  71. XCTAssertNil($0)
  72. sendExpectation.fulfill()
  73. }
  74. call.waitForSendOperationsToFinish()
  75. do {
  76. let result = try call.closeAndReceive()
  77. XCTFail("should have thrown, instead received \(result)")
  78. } catch let receiveError {
  79. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  80. }
  81. waitForExpectations(timeout: defaultTimeout)
  82. }
  83. func testTimeoutServerStreaming() {
  84. let completionHandlerExpectation = expectation(description: "completion handler called")
  85. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  86. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  87. completionHandlerExpectation.fulfill()
  88. }
  89. // FIXME(danielalm): Why does `call.receive()` essentially return "end of stream" once the call times out,
  90. // rather than returning an error?
  91. XCTAssertNil(try! call.receive())
  92. waitForExpectations(timeout: defaultTimeout)
  93. }
  94. func testTimeoutBidirectionalStreaming() {
  95. let completionHandlerExpectation = expectation(description: "completion handler called")
  96. let call = try! client.update { callResult in
  97. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  98. completionHandlerExpectation.fulfill()
  99. }
  100. let sendExpectation = expectation(description: "send completion handler 1 called")
  101. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  102. // The server only times out later in its lifecycle, so we shouldn't get an error when trying to send a message.
  103. XCTAssertNil($0)
  104. sendExpectation.fulfill()
  105. }
  106. call.waitForSendOperationsToFinish()
  107. // FIXME(danielalm): Why does `call.receive()` essentially return "end of stream" once the call times out,
  108. // rather than returning an error?
  109. XCTAssertNil(try! call.receive())
  110. waitForExpectations(timeout: defaultTimeout)
  111. }
  112. }