NIOBasicEchoTestCase.swift 2.2 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 Dispatch
  17. import Foundation
  18. import NIO
  19. @testable import SwiftGRPCNIO
  20. import XCTest
  21. extension Echo_EchoRequest {
  22. init(text: String) {
  23. self.text = text
  24. }
  25. }
  26. extension Echo_EchoResponse {
  27. init(text: String) {
  28. self.text = text
  29. }
  30. }
  31. class NIOBasicEchoTestCase: XCTestCase {
  32. var defaultTestTimeout: TimeInterval = 1.0
  33. var serverEventLoopGroup: EventLoopGroup!
  34. var server: GRPCServer!
  35. var clientEventLoopGroup: EventLoopGroup!
  36. var client: Echo_EchoService_NIOClient!
  37. func makeEchoProvider() -> Echo_EchoProvider_NIO { return EchoProviderNIO() }
  38. override func setUp() {
  39. super.setUp()
  40. self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  41. self.server = try! GRPCServer.start(
  42. hostname: "localhost", port: 5050, eventLoopGroup: self.serverEventLoopGroup, serviceProviders: [makeEchoProvider()])
  43. .wait()
  44. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  45. self.client = try! GRPCClient.start(
  46. host: "localhost", port: 5050, eventLoopGroup: self.clientEventLoopGroup)
  47. .map { Echo_EchoService_NIOClient(client: $0, defaultCallOptions: CallOptions(timeout: try! .seconds(5))) }
  48. .wait()
  49. }
  50. override func tearDown() {
  51. XCTAssertNoThrow(try self.client.client.close().wait())
  52. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  53. self.clientEventLoopGroup = nil
  54. self.client = nil
  55. XCTAssertNoThrow(try self.server.close().wait())
  56. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  57. self.serverEventLoopGroup = nil
  58. self.server = nil
  59. super.tearDown()
  60. }
  61. }