CompletionQueueTests.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 {
  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. }
  33. func collect(session: Echo_EchoCollectSession) throws { }
  34. func update(session: Echo_EchoUpdateSession) throws { }
  35. }
  36. class CompletionQueueTests: BasicEchoTestCase {
  37. static var allTests: [(String, (CompletionQueueTests) -> () throws -> Void)] {
  38. return [
  39. ("testCompletionQueueThrowsAfterShutdown", testCompletionQueueThrowsAfterShutdown)
  40. ]
  41. }
  42. override func makeProvider() -> Echo_EchoProvider { return ClosingProvider() }
  43. }
  44. extension CompletionQueueTests {
  45. func testCompletionQueueThrowsAfterShutdown() {
  46. (self.provider as! ClosingProvider).doneExpectation = expectation(description: "end of server-side request handler reached")
  47. let completionHandlerExpectation = expectation(description: "completion handler called")
  48. _ = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  49. XCTAssertEqual(.ok, callResult.statusCode)
  50. XCTAssertEqual("OK", callResult.statusMessage)
  51. completionHandlerExpectation.fulfill()
  52. }
  53. waitForExpectations(timeout: defaultTimeout)
  54. }
  55. }