ClientCancellingTests.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. import Dispatch
  17. import Foundation
  18. @testable import SwiftGRPC
  19. import XCTest
  20. class ClientCancellingTests: BasicEchoTestCase {
  21. static var allTests: [(String, (ClientCancellingTests) -> () throws -> Void)] {
  22. return [
  23. ("testUnary", testUnary),
  24. ("testClientStreaming", testClientStreaming),
  25. ("testServerStreaming", testServerStreaming),
  26. ("testBidirectionalStreaming", testBidirectionalStreaming),
  27. ]
  28. }
  29. }
  30. extension ClientCancellingTests {
  31. func testUnary() {
  32. let completionHandlerExpectation = expectation(description: "final completion handler called")
  33. let call = try! client.get(Echo_EchoRequest(text: "foo bar baz")) { response, callResult in
  34. XCTAssertNil(response)
  35. XCTAssertEqual(.cancelled, callResult.statusCode)
  36. completionHandlerExpectation.fulfill()
  37. }
  38. call.cancel()
  39. waitForExpectations(timeout: defaultTimeout)
  40. }
  41. func testClientStreaming() {
  42. let completionHandlerExpectation = expectation(description: "final completion handler called")
  43. let call = try! client.collect { callResult in
  44. XCTAssertEqual(.cancelled, callResult.statusCode)
  45. completionHandlerExpectation.fulfill()
  46. }
  47. call.cancel()
  48. let sendExpectation = expectation(description: "send completion handler 1 called")
  49. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertEqual(.unknown, $0 as! CallError); sendExpectation.fulfill() }
  50. call.waitForSendOperationsToFinish()
  51. do {
  52. let result = try call.closeAndReceive()
  53. XCTFail("should have thrown, received \(result) instead")
  54. } catch let receiveError {
  55. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  56. }
  57. waitForExpectations(timeout: defaultTimeout)
  58. }
  59. func testServerStreaming() {
  60. let completionHandlerExpectation = expectation(description: "completion handler called")
  61. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  62. XCTAssertEqual(.cancelled, callResult.statusCode)
  63. completionHandlerExpectation.fulfill()
  64. }
  65. XCTAssertEqual("Swift echo expand (0): foo", try! call.receive()!.text)
  66. call.cancel()
  67. do {
  68. let result = try call.receive()
  69. XCTFail("should have thrown, received \(String(describing: result)) instead")
  70. } catch let receiveError {
  71. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  72. }
  73. waitForExpectations(timeout: defaultTimeout)
  74. }
  75. func testBidirectionalStreaming() {
  76. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  77. let call = try! client.update { callResult in
  78. XCTAssertEqual(.cancelled, callResult.statusCode)
  79. finalCompletionHandlerExpectation.fulfill()
  80. }
  81. var sendExpectation = expectation(description: "send completion handler 1 called")
  82. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  83. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  84. call.cancel()
  85. sendExpectation = expectation(description: "send completion handler 2 called")
  86. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertEqual(.unknown, $0 as! CallError); sendExpectation.fulfill() }
  87. do {
  88. let result = try call.receive()
  89. XCTFail("should have thrown, received \(String(describing: result)) instead")
  90. } catch let receiveError {
  91. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  92. }
  93. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  94. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  95. waitForExpectations(timeout: defaultTimeout)
  96. }
  97. }