NIOBasicEchoTestCase.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. override func setUp() {
  38. super.setUp()
  39. self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  40. self.server = try! GRPCServer.start(
  41. hostname: "localhost", port: 5050, eventLoopGroup: self.serverEventLoopGroup, serviceProviders: [EchoProviderNIO()])
  42. .wait()
  43. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  44. self.client = try! GRPCClient.start(
  45. host: "localhost", port: 5050, eventLoopGroup: self.clientEventLoopGroup)
  46. .map { Echo_EchoService_NIOClient(client: $0, defaultCallOptions: CallOptions(timeout: try! .seconds(5))) }
  47. .wait()
  48. }
  49. override func tearDown() {
  50. XCTAssertNoThrow(try self.client.client.close().wait())
  51. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  52. self.clientEventLoopGroup = nil
  53. self.client = nil
  54. XCTAssertNoThrow(try self.server.close().wait())
  55. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  56. self.serverEventLoopGroup = nil
  57. self.server = nil
  58. super.tearDown()
  59. }
  60. }