ConnectionFailureTests.swift 4.2 KB

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