main.swift 4.6 KB

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