HTTP2ConnectionStateTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2021, 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 Logging
  18. import NIO
  19. import NIOHTTP2
  20. import XCTest
  21. final class HTTP2ConnectionStateTests: GRPCTestCase {
  22. private final class Placeholder {}
  23. private var placeholders: [Placeholder] = []
  24. private let channel = EmbeddedChannel()
  25. private var multiplexer: HTTP2StreamMultiplexer!
  26. private var eventLoop: EmbeddedEventLoop {
  27. return self.channel.embeddedEventLoop
  28. }
  29. override func setUp() {
  30. super.setUp()
  31. self.multiplexer = HTTP2StreamMultiplexer(
  32. mode: .client,
  33. channel: self.channel,
  34. inboundStreamInitializer: nil
  35. )
  36. }
  37. private func makeHTTP2ConnectionState() -> HTTP2ConnectionState {
  38. let placeholder = Placeholder()
  39. self.placeholders.append(placeholder)
  40. return HTTP2ConnectionState(connectionManagerID: ObjectIdentifier(placeholder))
  41. }
  42. func testNewPooledConnection() {
  43. let state = self.makeHTTP2ConnectionState()
  44. XCTAssertEqual(state.availableTokens, 0)
  45. XCTAssertEqual(state.borrowedTokens, 0)
  46. XCTAssert(state.isIdle)
  47. }
  48. func testIdleToConnected() {
  49. var state = self.makeHTTP2ConnectionState()
  50. state.willStartConnecting()
  51. XCTAssertEqual(state.availableTokens, 0)
  52. XCTAssertFalse(state.isIdle)
  53. state.connected(multiplexer: self.multiplexer)
  54. // 100 is the default value
  55. XCTAssertEqual(state.availableTokens, 100)
  56. let newTokenLimit = 10
  57. let oldLimit = state.updateMaximumTokens(newTokenLimit)
  58. XCTAssertEqual(oldLimit, 100)
  59. XCTAssertEqual(state.availableTokens, newTokenLimit)
  60. }
  61. func testBorrowAndReturnTokens() {
  62. var state = self.makeHTTP2ConnectionState()
  63. state.willStartConnecting()
  64. state.connected(multiplexer: self.multiplexer)
  65. _ = state.updateMaximumTokens(10)
  66. XCTAssertEqual(state.availableTokens, 10)
  67. XCTAssertEqual(state.borrowedTokens, 0)
  68. _ = state.borrowTokens(1)
  69. XCTAssertEqual(state.borrowedTokens, 1)
  70. XCTAssertEqual(state.availableTokens, 9)
  71. _ = state.borrowTokens(9)
  72. XCTAssertEqual(state.borrowedTokens, 10)
  73. XCTAssertEqual(state.availableTokens, 0)
  74. state.returnToken()
  75. XCTAssertEqual(state.borrowedTokens, 9)
  76. XCTAssertEqual(state.availableTokens, 1)
  77. }
  78. func testConnectivityChanges() {
  79. var state = self.makeHTTP2ConnectionState()
  80. XCTAssert(state.isIdle)
  81. XCTAssertEqual(state.connectivityStateChanged(to: .idle), .nothing)
  82. state.willStartConnecting()
  83. XCTAssertFalse(state.isIdle)
  84. // No changes expected.
  85. XCTAssertEqual(state.connectivityStateChanged(to: .connecting), .nothing)
  86. XCTAssertEqual(state.connectivityStateChanged(to: .transientFailure), .nothing)
  87. XCTAssertEqual(state.connectivityStateChanged(to: .connecting), .nothing)
  88. // We do nothing on '.ready', instead we wait for '.connected(multiplexer:)' as our signal
  89. // that we're actually ready (since it provides the 'HTTP2StreamMultiplexer'.
  90. XCTAssertEqual(state.connectivityStateChanged(to: .ready), .nothing)
  91. state.connected(multiplexer: self.multiplexer)
  92. let readyState = state
  93. // The connection dropped, so the multiplexer we hold is no longer valid, as such we need to ask
  94. // for a new one.
  95. XCTAssertEqual(state.connectivityStateChanged(to: .transientFailure), .startConnectingAgain)
  96. // Restore the connection in the ready state.
  97. state = readyState
  98. // Shutdown: we'll drop the connection from the list, it's the end of the road for this
  99. // connection.
  100. XCTAssertEqual(state.connectivityStateChanged(to: .shutdown), .removeFromConnectionList)
  101. }
  102. }