2
0

ServiceClientTests.swift 2.3 KB

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