ServiceClientTests.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 Foundation
  17. @testable import SwiftGRPC
  18. import XCTest
  19. class ServiceClientTests: BasicEchoTestCase {
  20. private var sharedChannel: Channel?
  21. override func setUp() {
  22. super.setUp()
  23. sharedChannel = Channel(address: address, secure: false)
  24. }
  25. override func tearDown() {
  26. sharedChannel = nil
  27. super.tearDown()
  28. }
  29. func testSharingChannelBetweenClientsUnaryAsync() {
  30. let firstCallExpectation = expectation(description: "First call completes successfully")
  31. let secondCallExpectation = expectation(description: "Second call completes successfully")
  32. do {
  33. let client1 = Echo_EchoServiceClient(channel: sharedChannel!)
  34. try _ = client1.get(Echo_EchoRequest(text: "foo")) { _, callResult in
  35. XCTAssertEqual(.ok, callResult.statusCode)
  36. firstCallExpectation.fulfill()
  37. }
  38. let client2 = Echo_EchoServiceClient(channel: sharedChannel!)
  39. try _ = client2.get(Echo_EchoRequest(text: "foo")) { _, callResult in
  40. XCTAssertEqual(.ok, callResult.statusCode)
  41. secondCallExpectation.fulfill()
  42. }
  43. } catch let error {
  44. XCTFail(error.localizedDescription)
  45. }
  46. waitForExpectations(timeout: defaultTimeout)
  47. }
  48. func testSharedChannelStillWorksAfterFirstUnaryClientCompletes() {
  49. do {
  50. let client1 = Echo_EchoServiceClient(channel: sharedChannel!)
  51. let response1 = try client1.get(Echo_EchoRequest(text: "foo")).text
  52. XCTAssertEqual("Swift echo get: foo", response1)
  53. } catch let error {
  54. XCTFail(error.localizedDescription)
  55. }
  56. do {
  57. let client2 = Echo_EchoServiceClient(channel: sharedChannel!)
  58. let response2 = try client2.get(Echo_EchoRequest(text: "foo")).text
  59. XCTAssertEqual("Swift echo get: foo", response2)
  60. } catch let error {
  61. XCTFail(error.localizedDescription)
  62. }
  63. }
  64. }