GRPCIdleTests.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. .bind(host: "localhost", port: 0)
  41. .wait()
  42. defer {
  43. XCTAssertNoThrow(try server.close().wait())
  44. }
  45. // Setup a state change recorder for the client.
  46. let stateRecorder = RecordingConnectivityDelegate()
  47. stateRecorder.expectChanges(3) { changes in
  48. XCTAssertEqual(changes, [
  49. Change(from: .idle, to: .connecting),
  50. Change(from: .connecting, to: .ready),
  51. Change(from: .ready, to: isServerIdleFirst ? .transientFailure : .idle)
  52. ])
  53. }
  54. // Setup a connection.
  55. let connection = ClientConnection.insecure(group: group)
  56. .withConnectivityStateDelegate(stateRecorder)
  57. .withConnectionIdleTimeout(clientIdle)
  58. .connect(host: "localhost", port: server.channel.localAddress!.port!)
  59. defer {
  60. XCTAssertNoThrow(try connection.close().wait())
  61. }
  62. let client = Echo_EchoClient(channel: connection)
  63. // Make a call; this will trigger channel creation.
  64. let get = client.get(.with { $0.text = "ignored" })
  65. let status = try get.status.wait()
  66. XCTAssertEqual(status.code, .ok)
  67. // Now wait for the state changes.
  68. stateRecorder.waitForExpectedChanges(timeout: .seconds(10))
  69. }
  70. }