ServerQuiescingTests.swift 3.3 KB

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