ChannelShutdownTests.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 ChannelShutdownTests: BasicEchoTestCase {
  19. static var allTests: [(String, (ChannelShutdownTests) -> () throws -> Void)] {
  20. return [
  21. ("testThrowsWhenCreatingCallWithAlreadyShutDownChannel", testThrowsWhenCreatingCallWithAlreadyShutDownChannel),
  22. ("testCallReceiveThrowsWhenChannelIsShutDown", testCallReceiveThrowsWhenChannelIsShutDown),
  23. ("testCallCloseThrowsWhenChannelIsShutDown", testCallCloseThrowsWhenChannelIsShutDown),
  24. ("testCallCloseAndReceiveThrowsWhenChannelIsShutDown", testCallCloseAndReceiveThrowsWhenChannelIsShutDown),
  25. ("testCallSendThrowsWhenChannelIsShutDown", testCallSendThrowsWhenChannelIsShutDown),
  26. ("testCancelsActiveCallWhenShutdownIsCalled", testCancelsActiveCallWhenShutdownIsCalled),
  27. ]
  28. }
  29. }
  30. extension ChannelShutdownTests {
  31. func testThrowsWhenCreatingCallWithAlreadyShutDownChannel() {
  32. self.client.channel.shutdown()
  33. XCTAssertThrowsError(try self.client.channel.makeCall("foobar")) { error in
  34. XCTAssertEqual(.alreadyShutdown, error as? Channel.Error)
  35. }
  36. }
  37. func testCallReceiveThrowsWhenChannelIsShutDown() {
  38. let call = try! self.client.channel.makeCall("foo")
  39. self.client.channel.shutdown()
  40. XCTAssertThrowsError(try call.receiveMessage { _ in }) { error in
  41. XCTAssertEqual(.completionQueueShutdown, error as? CallError)
  42. }
  43. }
  44. func testCallCloseThrowsWhenChannelIsShutDown() {
  45. let call = try! self.client.channel.makeCall("foo")
  46. self.client.channel.shutdown()
  47. XCTAssertThrowsError(try call.close()) { error in
  48. XCTAssertEqual(.completionQueueShutdown, error as? CallError)
  49. }
  50. }
  51. func testCallCloseAndReceiveThrowsWhenChannelIsShutDown() {
  52. let call = try! self.client.channel.makeCall("foo")
  53. self.client.channel.shutdown()
  54. XCTAssertThrowsError(try call.closeAndReceiveMessage { _ in }) { error in
  55. XCTAssertEqual(.completionQueueShutdown, error as? CallError)
  56. }
  57. }
  58. func testCallSendThrowsWhenChannelIsShutDown() {
  59. let call = try! self.client.channel.makeCall("foo")
  60. self.client.channel.shutdown()
  61. XCTAssertThrowsError(try call.sendMessage(data: Data())) { error in
  62. XCTAssertEqual(.completionQueueShutdown, error as? CallError)
  63. }
  64. }
  65. func testCancelsActiveCallWhenShutdownIsCalled() {
  66. let errorExpectation = self.expectation(description: "error is returned to call when channel is shut down")
  67. let call = try! self.client.channel.makeCall("foo")
  68. try! call.receiveMessage { result in
  69. XCTAssertFalse(result.success)
  70. errorExpectation.fulfill()
  71. }
  72. self.client.channel.shutdown()
  73. self.waitForExpectations(timeout: 0.1)
  74. XCTAssertThrowsError(try call.close()) { error in
  75. XCTAssertEqual(.completionQueueShutdown, error as? CallError)
  76. }
  77. }
  78. }