AsyncClientTests.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 AsyncClientTests: BasicEchoTestCase {
  21. // Using `TimingOutEchoProvider` gives us enough time to release the client before we can expect a result.
  22. override func makeProvider() -> Echo_EchoProvider { return TimingOutEchoProvider() }
  23. static var allTests: [(String, (AsyncClientTests) -> () throws -> Void)] {
  24. return [
  25. ("testAsyncUnaryRetainsClientUntilCallFinished", testAsyncUnaryRetainsClientUntilCallFinished),
  26. ("testClientStreamingRetainsClientUntilCallFinished", testClientStreamingRetainsClientUntilCallFinished),
  27. ("testServerStreamingRetainsClientUntilCallFinished", testServerStreamingRetainsClientUntilCallFinished),
  28. ("testBidiStreamingRetainsClientUntilCallFinished", testBidiStreamingRetainsClientUntilCallFinished),
  29. ]
  30. }
  31. }
  32. extension AsyncClientTests {
  33. func testAsyncUnaryRetainsClientUntilCallFinished() {
  34. let completionHandlerExpectation = expectation(description: "completion handler called")
  35. _ = try! client.get(Echo_EchoRequest(text: "foo")) { response, result in
  36. XCTAssertEqual("", response?.text)
  37. XCTAssertEqual(.ok, result.statusCode)
  38. completionHandlerExpectation.fulfill()
  39. }
  40. // The call should complete even when the client and call are not retained.
  41. client = nil
  42. waitForExpectations(timeout: 1.0)
  43. }
  44. func testClientStreamingRetainsClientUntilCallFinished() {
  45. let callCompletionHandlerExpectation = expectation(description: "call completion handler called")
  46. var call: Echo_EchoCollectCall? = try! client.collect { result in
  47. XCTAssertEqual(.ok, result.statusCode)
  48. callCompletionHandlerExpectation.fulfill()
  49. }
  50. let responseCompletionHandlerExpectation = expectation(description: "response completion handler called")
  51. try! call!.closeAndReceive { response in
  52. XCTAssertEqual("", response.result?.text)
  53. responseCompletionHandlerExpectation.fulfill()
  54. }
  55. call = nil
  56. // The call should complete even when the client and call are not retained.
  57. client = nil
  58. waitForExpectations(timeout: 1.0)
  59. }
  60. func testServerStreamingRetainsClientUntilCallFinished() {
  61. let callCompletionHandlerExpectation = expectation(description: "call completion handler called")
  62. _ = try! client.expand(.init()) { result in
  63. XCTAssertEqual(.ok, result.statusCode)
  64. callCompletionHandlerExpectation.fulfill()
  65. }
  66. // The call should complete even when the client and call are not retained.
  67. client = nil
  68. waitForExpectations(timeout: 1.0)
  69. }
  70. func testBidiStreamingRetainsClientUntilCallFinished() {
  71. let callCompletionHandlerExpectation = expectation(description: "call completion handler called")
  72. var call: Echo_EchoUpdateCall? = try! client.update { result in
  73. XCTAssertEqual(.ok, result.statusCode)
  74. callCompletionHandlerExpectation.fulfill()
  75. }
  76. let closeSendCompletionHandlerExpectation = expectation(description: "closeSend completion handler called")
  77. try! call!.closeSend {
  78. closeSendCompletionHandlerExpectation.fulfill()
  79. }
  80. call = nil
  81. // The call should complete even when the client and call are not retained.
  82. client = nil
  83. waitForExpectations(timeout: 1.0)
  84. }
  85. }