CompletionQueueTests.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. fileprivate class ClosingProvider: Echo_EchoProvider {
  21. var doneExpectation: XCTestExpectation!
  22. func get(request: Echo_EchoRequest, session: Echo_EchoGetSession) throws -> Echo_EchoResponse {
  23. return Echo_EchoResponse()
  24. }
  25. func expand(request: Echo_EchoRequest, session: Echo_EchoExpandSession) throws -> ServerStatus? {
  26. let closeSem = DispatchSemaphore(value: 0)
  27. try! session.close(withStatus: .ok) {
  28. closeSem.signal()
  29. }
  30. XCTAssertThrowsError(try session.send(Echo_EchoResponse()))
  31. doneExpectation.fulfill()
  32. return nil
  33. }
  34. func collect(session: Echo_EchoCollectSession) throws -> Echo_EchoResponse? { fatalError("not implemented") }
  35. func update(session: Echo_EchoUpdateSession) throws -> ServerStatus? { fatalError("not implemented") }
  36. }
  37. class CompletionQueueTests: BasicEchoTestCase {
  38. static var allTests: [(String, (CompletionQueueTests) -> () throws -> Void)] {
  39. return [
  40. ("testCompletionQueueThrowsAfterShutdown", testCompletionQueueThrowsAfterShutdown)
  41. ]
  42. }
  43. override func makeProvider() -> Echo_EchoProvider { return ClosingProvider() }
  44. }
  45. extension CompletionQueueTests {
  46. func testCompletionQueueThrowsAfterShutdown() {
  47. (self.provider as! ClosingProvider).doneExpectation = expectation(description: "end of server-side request handler reached")
  48. let completionHandlerExpectation = expectation(description: "completion handler called")
  49. _ = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  50. XCTAssertEqual(.ok, callResult.statusCode)
  51. XCTAssertEqual("OK", callResult.statusMessage)
  52. completionHandlerExpectation.fulfill()
  53. }
  54. waitForExpectations(timeout: defaultTimeout)
  55. }
  56. }