ClientCancellingTests.swift 2.7 KB

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