ErrorHandlingTests.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // TODO(danielalm): Also run a similar set of tests with SSL enabled.
  21. class ErrorHandlingTests: XCTestCase {
  22. static var allTests: [(String, (ErrorHandlingTests) -> () throws -> Void)] {
  23. return [
  24. ("testConnectionFailureUnary", testConnectionFailureUnary),
  25. ("testConnectionFailureClientStreaming", testConnectionFailureClientStreaming),
  26. ("testConnectionFailureServerStreaming", testConnectionFailureServerStreaming),
  27. ("testConnectionFailureBidirectionalStreaming", testConnectionFailureBidirectionalStreaming)
  28. ]
  29. }
  30. let address = "localhost:5050"
  31. let defaultTimeout: TimeInterval = 1.0
  32. }
  33. extension ErrorHandlingTests {
  34. func testConnectionFailureUnary() {
  35. let client = Echo_EchoServiceClient(address: "localhost:1234", secure: false)
  36. client.timeout = defaultTimeout
  37. do {
  38. _ = try client.get(Echo_EchoRequest(text: "foo")).text
  39. XCTFail("should have thrown")
  40. } catch {
  41. guard case let .callError(callResult) = error as! RPCError
  42. else { XCTFail("unexpected error \(error)"); return }
  43. XCTAssertEqual(.unavailable, callResult.statusCode)
  44. XCTAssertEqual("Connect Failed", callResult.statusMessage)
  45. }
  46. }
  47. func testConnectionFailureClientStreaming() {
  48. let client = Echo_EchoServiceClient(address: "localhost:1234", secure: false)
  49. client.timeout = defaultTimeout
  50. let completionHandlerExpectation = expectation(description: "final completion handler called")
  51. let call = try! client.collect { callResult in
  52. XCTAssertEqual(.unavailable, callResult.statusCode)
  53. completionHandlerExpectation.fulfill()
  54. }
  55. let sendExpectation = expectation(description: "send completion handler 1 called")
  56. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  57. XCTAssertEqual(.unknown, $0 as! CallError)
  58. sendExpectation.fulfill()
  59. }
  60. call.waitForSendOperationsToFinish()
  61. do {
  62. _ = try call.closeAndReceive()
  63. XCTFail("should have thrown")
  64. } catch let receiveError {
  65. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  66. }
  67. waitForExpectations(timeout: defaultTimeout)
  68. }
  69. func testConnectionFailureServerStreaming() {
  70. let client = Echo_EchoServiceClient(address: "localhost:1234", secure: false)
  71. client.timeout = defaultTimeout
  72. let completionHandlerExpectation = expectation(description: "completion handler called")
  73. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  74. XCTAssertEqual(.unavailable, callResult.statusCode)
  75. completionHandlerExpectation.fulfill()
  76. }
  77. do {
  78. _ = try call.receive()
  79. XCTFail("should have thrown")
  80. } catch let receiveError {
  81. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  82. }
  83. waitForExpectations(timeout: defaultTimeout)
  84. }
  85. func testConnectionFailureBidirectionalStreaming() {
  86. let client = Echo_EchoServiceClient(address: "localhost:1234", secure: false)
  87. client.timeout = defaultTimeout
  88. let completionHandlerExpectation = expectation(description: "completion handler called")
  89. let call = try! client.update { callResult in
  90. XCTAssertEqual(.unavailable, callResult.statusCode)
  91. completionHandlerExpectation.fulfill()
  92. }
  93. let sendExpectation = expectation(description: "send completion handler 1 called")
  94. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  95. XCTAssertEqual(.unknown, $0 as! CallError)
  96. sendExpectation.fulfill()
  97. }
  98. call.waitForSendOperationsToFinish()
  99. do {
  100. _ = try call.receive()
  101. XCTFail("should have thrown")
  102. } catch let receiveError {
  103. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  104. }
  105. waitForExpectations(timeout: defaultTimeout)
  106. }
  107. }