ServerTimeoutTests.swift 4.4 KB

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