ServerTimeoutTests.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 {
  26. Thread.sleep(forTimeInterval: 0.2)
  27. }
  28. func collect(session: Echo_EchoCollectSession) throws {
  29. Thread.sleep(forTimeInterval: 0.2)
  30. }
  31. func update(session: Echo_EchoUpdateSession) throws {
  32. Thread.sleep(forTimeInterval: 0.2)
  33. }
  34. }
  35. class ServerTimeoutTests: XCTestCase {
  36. static var allTests: [(String, (ServerTimeoutTests) -> () throws -> Void)] {
  37. return [
  38. ("testTimeoutUnary", testTimeoutUnary),
  39. ("testTimeoutClientStreaming", testTimeoutClientStreaming),
  40. ("testTimeoutServerStreaming", testTimeoutServerStreaming),
  41. ("testTimeoutBidirectionalStreaming", testTimeoutBidirectionalStreaming)
  42. ]
  43. }
  44. let defaultTimeout: TimeInterval = 0.1
  45. fileprivate let provider = TimingOutEchoProvider()
  46. var server: Echo_EchoServer!
  47. var client: Echo_EchoServiceClient!
  48. override func setUp() {
  49. super.setUp()
  50. let address = "localhost:5050"
  51. server = Echo_EchoServer(address: address, provider: provider)
  52. server.start(queue: DispatchQueue.global())
  53. client = Echo_EchoServiceClient(address: address, secure: false)
  54. client.timeout = defaultTimeout
  55. }
  56. override func tearDown() {
  57. client = nil
  58. server.server.stop()
  59. server = nil
  60. super.tearDown()
  61. }
  62. }
  63. extension ServerTimeoutTests {
  64. func testTimeoutUnary() {
  65. do {
  66. _ = try client.get(Echo_EchoRequest(text: "foo")).text
  67. XCTFail("should have thrown")
  68. } catch {
  69. guard case let .callError(callResult) = error as! RPCError
  70. else { XCTFail("unexpected error \(error)"); return }
  71. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  72. XCTAssertEqual("Deadline Exceeded", callResult.statusMessage)
  73. }
  74. }
  75. func testTimeoutClientStreaming() {
  76. let completionHandlerExpectation = expectation(description: "final completion handler called")
  77. let call = try! client.collect { callResult in
  78. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  79. completionHandlerExpectation.fulfill()
  80. }
  81. let sendExpectation = expectation(description: "send completion handler 1 called")
  82. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  83. // The server only times out later in its lifecycle, so we shouldn't get an error when trying to send a message.
  84. XCTAssertNil($0)
  85. sendExpectation.fulfill()
  86. }
  87. call.waitForSendOperationsToFinish()
  88. do {
  89. _ = try call.closeAndReceive()
  90. XCTFail("should have thrown")
  91. } catch let receiveError {
  92. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  93. }
  94. waitForExpectations(timeout: defaultTimeout)
  95. }
  96. func testTimeoutServerStreaming() {
  97. let completionHandlerExpectation = expectation(description: "completion handler called")
  98. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  99. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  100. completionHandlerExpectation.fulfill()
  101. }
  102. // TODO(danielalm): Why doesn't `call.receive()` throw once the call times out?
  103. XCTAssertNil(try! call.receive())
  104. waitForExpectations(timeout: defaultTimeout)
  105. }
  106. func testTimeoutBidirectionalStreaming() {
  107. let completionHandlerExpectation = expectation(description: "completion handler called")
  108. let call = try! client.update { callResult in
  109. XCTAssertEqual(.deadlineExceeded, callResult.statusCode)
  110. completionHandlerExpectation.fulfill()
  111. }
  112. let sendExpectation = expectation(description: "send completion handler 1 called")
  113. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  114. // The server only times out later in its lifecycle, so we shouldn't get an error when trying to send a message.
  115. XCTAssertNil($0)
  116. sendExpectation.fulfill()
  117. }
  118. call.waitForSendOperationsToFinish()
  119. // FIXME(danielalm): Why does `call.receive()` only throw on Linux (but not macOS) once the call times out?
  120. #if os(Linux)
  121. do {
  122. _ = try call.receive()
  123. XCTFail("should have thrown")
  124. } catch let receiveError {
  125. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  126. }
  127. #else
  128. XCTAssertNil(try! call.receive())
  129. #endif
  130. waitForExpectations(timeout: defaultTimeout)
  131. }
  132. }