main.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2019, 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. import GRPC
  18. import GRPCInteroperabilityTestModels
  19. import NIO
  20. import Logging
  21. let args = CommandLine.arguments
  22. guard args.count == 3, let controlPort = Int(args[1]), let retryPort = Int(args[2]) else {
  23. print("Usage: \(args[0]) <server_control_port> <server_retry_port>")
  24. exit(1)
  25. }
  26. // Notes from the test procedure are inline.
  27. // See: https://github.com/grpc/grpc/blob/master/doc/connection-backoff-interop-test-description.md
  28. // MARK: - Setup
  29. // Reduce stdout noise.
  30. LoggingSystem.bootstrap(StreamLogHandler.standardError)
  31. let group = MultiThreadedEventLoopGroup(numberOfThreads: 2)
  32. defer {
  33. try! group.syncShutdownGracefully()
  34. }
  35. // The client must connect to the control port without TLS.
  36. let controlConfig = ClientConnection.Configuration(
  37. target: .hostAndPort("localhost", controlPort),
  38. eventLoopGroup: group,
  39. connectionBackoff: .init()
  40. )
  41. // The client must connect to the retry port with TLS.
  42. let retryConfig = ClientConnection.Configuration(
  43. target: .hostAndPort("localhost", retryPort),
  44. eventLoopGroup: group,
  45. tls: .init(),
  46. connectionBackoff: .init()
  47. )
  48. // MARK: - Test Procedure
  49. print("Starting connection backoff interoperability test...")
  50. // 1. Call 'Start' on server control port with a large deadline or no deadline, wait for it to
  51. // finish and check it succeeded.
  52. let controlConnection = ClientConnection(configuration: controlConfig)
  53. let controlClient = Grpc_Testing_ReconnectServiceServiceClient(connection: controlConnection)
  54. print("Control 'Start' call started")
  55. let controlStart = controlClient.start(.init(), callOptions: .init(timeout: .infinite))
  56. let controlStartStatus = try controlStart.status.wait()
  57. assert(controlStartStatus.code == .ok, "Control Start rpc failed: \(controlStartStatus.code)")
  58. print("Control 'Start' call succeeded")
  59. // 2. Initiate a channel connection to server retry port, which should perform reconnections with
  60. // proper backoffs. A convenient way to achieve this is to call 'Start' with a deadline of 540s.
  61. // The rpc should fail with deadline exceeded.
  62. print("Retry 'Start' call started")
  63. let retryConnection = ClientConnection(configuration: retryConfig)
  64. let retryClient = Grpc_Testing_ReconnectServiceServiceClient(
  65. connection: retryConnection,
  66. defaultCallOptions: CallOptions(timeout: try! .seconds(540))
  67. )
  68. let retryStart = retryClient.start(.init())
  69. // We expect this to take some time!
  70. let retryStartStatus = try retryStart.status.wait()
  71. assert(retryStartStatus.code == .deadlineExceeded,
  72. "Retry Start rpc status was not 'deadlineExceeded': \(retryStartStatus.code)")
  73. print("Retry 'Start' call terminated with expected status")
  74. // 3. Call 'Stop' on server control port and check it succeeded.
  75. print("Control 'Stop' call started")
  76. let controlStop = controlClient.stop(.init())
  77. let controlStopStatus = try controlStop.status.wait()
  78. assert(controlStopStatus.code == .ok, "Control Stop rpc failed: \(controlStopStatus.code)")
  79. print("Control 'Stop' call succeeded")
  80. // 4. Check the response to see whether the server thinks the backoffs passed the test.
  81. let controlResponse = try controlStop.response.wait()
  82. assert(controlResponse.passed, "TEST FAILED")
  83. print("TEST PASSED")
  84. // MARK: - Tear down
  85. // Close the connections.
  86. // We expect close to fail on the retry connection because the channel should never be successfully
  87. // started.
  88. try? retryConnection.close().wait()
  89. try controlConnection.close().wait()