main.swift 4.6 KB

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