ServerQuiescingTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2020, 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 EchoImplementation
  17. import EchoModel
  18. import GRPC
  19. import NIO
  20. import XCTest
  21. class ServerQuiescingTests: GRPCTestCase {
  22. func testServerQuiescing() throws {
  23. let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
  24. defer {
  25. assertThat(try group.syncShutdownGracefully(), .doesNotThrow())
  26. }
  27. let server = try Server.insecure(group: group)
  28. .withLogger(self.serverLogger)
  29. .withServiceProviders([EchoProvider()])
  30. .bind(host: "127.0.0.1", port: 0)
  31. .wait()
  32. let connectivityStateDelegate = RecordingConnectivityDelegate()
  33. let connection = ClientConnection.insecure(group: group)
  34. .withBackgroundActivityLogger(self.clientLogger)
  35. .withErrorDelegate(LoggingClientErrorDelegate())
  36. .withConnectivityStateDelegate(connectivityStateDelegate)
  37. .connect(host: "127.0.0.1", port: server.channel.localAddress!.port!)
  38. defer {
  39. assertThat(try connection.close().wait(), .doesNotThrow())
  40. }
  41. let echo = Echo_EchoClient(channel: connection)
  42. // Expect the connection to setup as normal.
  43. connectivityStateDelegate.expectChanges(2) { changes in
  44. XCTAssertEqual(changes[0], Change(from: .idle, to: .connecting))
  45. XCTAssertEqual(changes[1], Change(from: .connecting, to: .ready))
  46. }
  47. // Fire up a handful of client streaming RPCs, this will start the connection.
  48. let rpcs = (0 ..< 5).map { _ in
  49. echo.collect()
  50. }
  51. // Wait for the connectivity changes.
  52. connectivityStateDelegate.waitForExpectedChanges(timeout: .seconds(5))
  53. // Wait for the response metadata so both peers know about all RPCs.
  54. for rpc in rpcs {
  55. assertThat(try rpc.initialMetadata.wait(), .doesNotThrow())
  56. }
  57. // Start shutting down the server.
  58. let serverShutdown = server.initiateGracefulShutdown()
  59. // We should observe that we're quiescing now: this is a signal to not start any new RPCs.
  60. connectivityStateDelegate.waitForQuiescing(timeout: .seconds(5))
  61. // Queue up the expected change back to idle (i.e. when the connection is quiesced).
  62. connectivityStateDelegate.expectChange {
  63. XCTAssertEqual($0, Change(from: .ready, to: .idle))
  64. }
  65. // Finish each RPC.
  66. for (index, rpc) in rpcs.enumerated() {
  67. assertThat(try rpc.sendMessage(.with { $0.text = "\(index)" }).wait(), .doesNotThrow())
  68. assertThat(try rpc.sendEnd().wait(), .doesNotThrow())
  69. assertThat(try rpc.response.wait(), .is(.with { $0.text = "Swift echo collect: \(index)" }))
  70. }
  71. // All RPCs are done, the connection should drop back to idle.
  72. connectivityStateDelegate.waitForExpectedChanges(timeout: .seconds(5))
  73. // The server should be shutdown now.
  74. assertThat(try serverShutdown.wait(), .doesNotThrow())
  75. }
  76. }