NIOClientCancellingTests.swift 3.1 KB

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