GRPCIdleTests.swift 2.6 KB

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