ChannelConnectivityTests.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. @testable import SwiftGRPC
  17. import XCTest
  18. final class ChannelConnectivityTests: BasicEchoTestCase {
  19. override var defaultTimeout: TimeInterval { return 0.4 }
  20. }
  21. extension ChannelConnectivityTests {
  22. func testDanglingConnectivityObserversDontCrash() {
  23. let completionHandlerExpectation = expectation(description: "completion handler called")
  24. client.channel.addConnectivityObserver { connectivityState in
  25. print("ConnectivityState: \(connectivityState)")
  26. }
  27. let request = Echo_EchoRequest(text: "foo bar baz foo bar baz")
  28. _ = try! client.expand(request) { callResult in
  29. print("callResult.statusCode: \(callResult.statusCode)")
  30. completionHandlerExpectation.fulfill()
  31. }
  32. DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) {
  33. self.client = nil // Deallocating the client
  34. }
  35. waitForExpectations(timeout: 0.5)
  36. }
  37. func testMultipleConnectivityObserversAreCalled() {
  38. // Linux doesn't yet support `assertForOverFulfill` or `expectedFulfillmentCount`, and since these are
  39. // called multiple times, we can't use expectations. https://bugs.swift.org/browse/SR-6249
  40. var firstObserverCalled = false
  41. var secondObserverCalled = false
  42. client.channel.addConnectivityObserver { _ in firstObserverCalled = true }
  43. client.channel.addConnectivityObserver { _ in secondObserverCalled = true }
  44. let completionHandlerExpectation = expectation(description: "completion handler called")
  45. _ = try! client.get(Echo_EchoRequest(text: "foo")) { _, _ in
  46. completionHandlerExpectation.fulfill()
  47. }
  48. waitForExpectations(timeout: 0.5)
  49. XCTAssertTrue(firstObserverCalled)
  50. XCTAssertTrue(secondObserverCalled)
  51. }
  52. }