GRPCIdleTests.swift 2.8 KB

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