GRPCIdleTests.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. @testable import GRPC
  19. import NIO
  20. import XCTest
  21. class GRPCIdleTests: GRPCTestCase {
  22. func testClientIdleTimeout() {
  23. XCTAssertNoThrow(
  24. try self
  25. .doTestIdleTimeout(serverIdle: .minutes(5), clientIdle: .milliseconds(100))
  26. )
  27. }
  28. func testServerIdleTimeout() throws {
  29. XCTAssertNoThrow(
  30. try self
  31. .doTestIdleTimeout(serverIdle: .milliseconds(100), clientIdle: .minutes(5))
  32. )
  33. }
  34. func doTestIdleTimeout(serverIdle: TimeAmount, clientIdle: TimeAmount) throws {
  35. // Is the server idling first? This determines what state change the client should see when the
  36. // idle happens.
  37. let isServerIdleFirst = serverIdle < clientIdle
  38. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  39. defer {
  40. XCTAssertNoThrow(try group.syncShutdownGracefully())
  41. }
  42. // Setup a server.
  43. let server = try Server.insecure(group: group)
  44. .withServiceProviders([EchoProvider()])
  45. .withConnectionIdleTimeout(serverIdle)
  46. .withLogger(self.serverLogger)
  47. .bind(host: "localhost", port: 0)
  48. .wait()
  49. defer {
  50. XCTAssertNoThrow(try server.close().wait())
  51. }
  52. // Setup a state change recorder for the client.
  53. let stateRecorder = RecordingConnectivityDelegate()
  54. stateRecorder.expectChanges(3) { changes in
  55. XCTAssertEqual(changes, [
  56. Change(from: .idle, to: .connecting),
  57. Change(from: .connecting, to: .ready),
  58. Change(from: .ready, to: isServerIdleFirst ? .transientFailure : .idle),
  59. ])
  60. }
  61. // Setup a connection.
  62. let connection = ClientConnection.insecure(group: group)
  63. .withConnectivityStateDelegate(stateRecorder)
  64. .withConnectionIdleTimeout(clientIdle)
  65. .withBackgroundActivityLogger(self.clientLogger)
  66. .connect(host: "localhost", port: server.channel.localAddress!.port!)
  67. defer {
  68. XCTAssertNoThrow(try connection.close().wait())
  69. }
  70. let client = Echo_EchoClient(channel: connection)
  71. // Make a call; this will trigger channel creation.
  72. let get = client.get(.with { $0.text = "ignored" })
  73. let status = try get.status.wait()
  74. XCTAssertEqual(status.code, .ok)
  75. // Now wait for the state changes.
  76. stateRecorder.waitForExpectedChanges(timeout: .seconds(10))
  77. }
  78. }