ConfigurationTests.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. import GRPC
  17. import NIOEmbedded
  18. import XCTest
  19. final class ConfigurationTests: GRPCTestCase {
  20. private var eventLoop: EmbeddedEventLoop!
  21. private var clientDefaults: ClientConnection.Configuration {
  22. return .default(target: .unixDomainSocket("/ignored"), eventLoopGroup: self.eventLoop)
  23. }
  24. private var serverDefaults: Server.Configuration {
  25. return .default(
  26. target: .unixDomainSocket("/ignored"),
  27. eventLoopGroup: self.eventLoop,
  28. serviceProviders: []
  29. )
  30. }
  31. override func setUp() {
  32. super.setUp()
  33. self.eventLoop = EmbeddedEventLoop()
  34. }
  35. override func tearDown() {
  36. XCTAssertNoThrow(try self.eventLoop.syncShutdownGracefully())
  37. super.tearDown()
  38. }
  39. private let maxFrameSizeMinimum = (1 << 14)
  40. private let maxFrameSizeMaximum = (1 << 24) - 1
  41. private func doTestHTTPMaxFrameSizeIsClamped(for configuration: HasHTTP2Configuration) {
  42. var configuration = configuration
  43. configuration.httpMaxFrameSize = 0
  44. XCTAssertEqual(configuration.httpMaxFrameSize, self.maxFrameSizeMinimum)
  45. configuration.httpMaxFrameSize = .max
  46. XCTAssertEqual(configuration.httpMaxFrameSize, self.maxFrameSizeMaximum)
  47. configuration.httpMaxFrameSize = self.maxFrameSizeMinimum + 1
  48. XCTAssertEqual(configuration.httpMaxFrameSize, self.maxFrameSizeMinimum + 1)
  49. }
  50. func testHTTPMaxFrameSizeIsClampedForClient() {
  51. self.doTestHTTPMaxFrameSizeIsClamped(for: self.clientDefaults)
  52. }
  53. func testHTTPMaxFrameSizeIsClampedForServer() {
  54. self.doTestHTTPMaxFrameSizeIsClamped(for: self.serverDefaults)
  55. }
  56. private let targetWindowSizeMinimum = 1
  57. private let targetWindowSizeMaximum = Int(Int32.max)
  58. private func doTestHTTPTargetWindowSizeIsClamped(for configuration: HasHTTP2Configuration) {
  59. var configuration = configuration
  60. configuration.httpTargetWindowSize = .min
  61. XCTAssertEqual(configuration.httpTargetWindowSize, self.targetWindowSizeMinimum)
  62. configuration.httpTargetWindowSize = .max
  63. XCTAssertEqual(configuration.httpTargetWindowSize, self.targetWindowSizeMaximum)
  64. configuration.httpTargetWindowSize = self.targetWindowSizeMinimum + 1
  65. XCTAssertEqual(configuration.httpTargetWindowSize, self.targetWindowSizeMinimum + 1)
  66. }
  67. func testHTTPTargetWindowSizeIsClampedForClient() {
  68. self.doTestHTTPTargetWindowSizeIsClamped(for: self.clientDefaults)
  69. }
  70. func testHTTPTargetWindowSizeIsClampedForServer() {
  71. self.doTestHTTPTargetWindowSizeIsClamped(for: self.serverDefaults)
  72. }
  73. }
  74. private protocol HasHTTP2Configuration {
  75. var httpMaxFrameSize: Int { get set }
  76. var httpTargetWindowSize: Int { get set }
  77. }
  78. extension ClientConnection.Configuration: HasHTTP2Configuration {}
  79. extension Server.Configuration: HasHTTP2Configuration {}