GRPCIdleTests.swift 2.7 KB

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