ClientCancellingTests.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2019, 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 EchoModel
  17. import Foundation
  18. import GRPC
  19. import XCTest
  20. class ClientCancellingTests: EchoTestCaseBase {
  21. func testUnary() {
  22. let statusReceived = self.expectation(description: "status received")
  23. let responseReceived = self.expectation(description: "response received")
  24. let call = client.get(Echo_EchoRequest(text: "foo bar baz"))
  25. call.cancel(promise: nil)
  26. call.response.whenFailure { error in
  27. XCTAssertTrue(error is GRPCError.RPCCancelledByClient)
  28. responseReceived.fulfill()
  29. }
  30. call.status.whenSuccess { status in
  31. XCTAssertEqual(status.code, .cancelled)
  32. statusReceived.fulfill()
  33. }
  34. waitForExpectations(timeout: self.defaultTestTimeout)
  35. }
  36. func testClientStreaming() throws {
  37. let statusReceived = self.expectation(description: "status received")
  38. let responseReceived = self.expectation(description: "response received")
  39. let call = client.collect()
  40. call.cancel(promise: nil)
  41. call.response.whenFailure { error in
  42. XCTAssertTrue(error is GRPCError.RPCCancelledByClient)
  43. responseReceived.fulfill()
  44. }
  45. call.status.whenSuccess { status in
  46. XCTAssertEqual(status.code, .cancelled)
  47. statusReceived.fulfill()
  48. }
  49. waitForExpectations(timeout: self.defaultTestTimeout)
  50. }
  51. func testServerStreaming() {
  52. let statusReceived = self.expectation(description: "status received")
  53. let call = client.expand(Echo_EchoRequest(text: "foo bar baz")) { _ in
  54. XCTFail("response should not be received after cancelling call")
  55. }
  56. call.cancel(promise: nil)
  57. call.status.whenSuccess { status in
  58. XCTAssertEqual(status.code, .cancelled)
  59. statusReceived.fulfill()
  60. }
  61. waitForExpectations(timeout: self.defaultTestTimeout)
  62. }
  63. func testBidirectionalStreaming() {
  64. let statusReceived = self.expectation(description: "status received")
  65. let call = client.update { _ in
  66. XCTFail("response should not be received after cancelling call")
  67. }
  68. call.cancel(promise: nil)
  69. call.status.whenSuccess { status in
  70. XCTAssertEqual(status.code, .cancelled)
  71. statusReceived.fulfill()
  72. }
  73. waitForExpectations(timeout: self.defaultTestTimeout)
  74. }
  75. }