ChannelShutdownTests.swift 2.7 KB

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