main.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 ArgumentParser
  17. import Foundation
  18. import GRPC
  19. import GRPCInteroperabilityTestModels
  20. import Logging
  21. import NIO
  22. // Notes from the test procedure are inline.
  23. // See: https://github.com/grpc/grpc/blob/master/doc/connection-backoff-interop-test-description.md
  24. // MARK: - Setup
  25. // Since this is a long running test, print connectivity state changes to stdout with timestamps.
  26. // We'll redirect logs to stderr so that stdout contains information only relevant to the test.
  27. class PrintingConnectivityStateDelegate: ConnectivityStateDelegate {
  28. func connectivityStateDidChange(from oldState: ConnectivityState,
  29. to newState: ConnectivityState) {
  30. print("[\(Date())] connectivity state change: \(oldState) → \(newState)")
  31. }
  32. }
  33. func runTest(controlPort: Int, retryPort: Int) throws {
  34. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  35. defer {
  36. try! group.syncShutdownGracefully()
  37. }
  38. // MARK: - Test Procedure
  39. print("[\(Date())] Starting connection backoff interoperability test...")
  40. // 1. Call 'Start' on server control port with a large deadline or no deadline, wait for it to
  41. // finish and check it succeeded.
  42. let controlConnection = ClientConnection.insecure(group: group)
  43. .connect(host: "localhost", port: controlPort)
  44. let controlClient = Grpc_Testing_ReconnectServiceClient(channel: controlConnection)
  45. print("[\(Date())] Control 'Start' call started")
  46. let controlStart = controlClient.start(.init(), callOptions: .init(timeLimit: .none))
  47. let controlStartStatus = try controlStart.status.wait()
  48. assert(controlStartStatus.code == .ok, "Control Start rpc failed: \(controlStartStatus.code)")
  49. print("[\(Date())] Control 'Start' call succeeded")
  50. // 2. Initiate a channel connection to server retry port, which should perform reconnections with
  51. // proper backoffs. A convenient way to achieve this is to call 'Start' with a deadline of 540s.
  52. // The rpc should fail with deadline exceeded.
  53. print("[\(Date())] Retry 'Start' call started")
  54. let retryConnection = ClientConnection.usingTLSBackedByNIOSSL(on: group)
  55. .withConnectivityStateDelegate(PrintingConnectivityStateDelegate())
  56. .connect(host: "localhost", port: retryPort)
  57. let retryClient = Grpc_Testing_ReconnectServiceClient(
  58. channel: retryConnection,
  59. defaultCallOptions: CallOptions(timeLimit: .timeout(.seconds(540)))
  60. )
  61. let retryStart = retryClient.start(.init())
  62. // We expect this to take some time!
  63. let retryStartStatus = try retryStart.status.wait()
  64. assert(
  65. retryStartStatus.code == .deadlineExceeded,
  66. "Retry Start rpc status was not 'deadlineExceeded': \(retryStartStatus.code)"
  67. )
  68. print("[\(Date())] Retry 'Start' call terminated with expected status")
  69. // 3. Call 'Stop' on server control port and check it succeeded.
  70. print("[\(Date())] Control 'Stop' call started")
  71. let controlStop = controlClient.stop(.init())
  72. let controlStopStatus = try controlStop.status.wait()
  73. assert(controlStopStatus.code == .ok, "Control Stop rpc failed: \(controlStopStatus.code)")
  74. print("[\(Date())] Control 'Stop' call succeeded")
  75. // 4. Check the response to see whether the server thinks the backoffs passed the test.
  76. let controlResponse = try controlStop.response.wait()
  77. assert(controlResponse.passed, "TEST FAILED")
  78. print("[\(Date())] TEST PASSED")
  79. // MARK: - Tear down
  80. // Close the connections.
  81. // We expect close to fail on the retry connection because the channel should never be successfully
  82. // started.
  83. print("[\(Date())] Closing Retry connection")
  84. try? retryConnection.close().wait()
  85. print("[\(Date())] Closing Control connection")
  86. try controlConnection.close().wait()
  87. }
  88. struct ConnectionBackoffInteropTest: ParsableCommand {
  89. @Option
  90. var controlPort: Int
  91. @Option
  92. var retryPort: Int
  93. func run() throws {
  94. do {
  95. try runTest(controlPort: self.controlPort, retryPort: self.retryPort)
  96. } catch {
  97. print("[\(Date())] Unexpected error: \(error)")
  98. throw error
  99. }
  100. }
  101. }
  102. ConnectionBackoffInteropTest.main()