BasicEchoTestCase.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. extension Echo_EchoRequest {
  21. init(text: String) {
  22. self.text = text
  23. }
  24. }
  25. class BasicEchoTestCase: XCTestCase {
  26. func makeProvider() -> Echo_EchoProvider { return EchoProvider() }
  27. var defaultTimeout: TimeInterval { return 1.0 }
  28. var provider: Echo_EchoProvider!
  29. var server: Echo_EchoServer!
  30. var client: Echo_EchoServiceClient!
  31. var secure: Bool { return false }
  32. override func setUp() {
  33. super.setUp()
  34. provider = makeProvider()
  35. let address = "localhost:5050"
  36. if secure {
  37. let certificateString = String(data: certificateForTests, encoding: .utf8)!
  38. server = Echo_EchoServer(address: address,
  39. certificateString: certificateString,
  40. keyString: String(data: keyForTests, encoding: .utf8)!,
  41. provider: provider)
  42. server.start(queue: DispatchQueue.global())
  43. client = Echo_EchoServiceClient(address: address, certificates: certificateString, host: "example.com")
  44. client.host = "example.com"
  45. } else {
  46. server = Echo_EchoServer(address: address, provider: provider)
  47. server.start(queue: DispatchQueue.global())
  48. client = Echo_EchoServiceClient(address: address, secure: false)
  49. }
  50. client.timeout = defaultTimeout
  51. }
  52. override func tearDown() {
  53. client = nil
  54. server.server.stop()
  55. server = nil
  56. super.tearDown()
  57. }
  58. }