HTTP2TransportNIOTransportServicesTests.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2024, 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. #if canImport(Network)
  17. private import GRPCCore
  18. private import GRPCHTTP2Core
  19. internal import GRPCHTTP2TransportNIOTransportServices
  20. internal import XCTest
  21. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  22. final class HTTP2TransportNIOTransportServicesTests: XCTestCase {
  23. func testGetListeningAddress_IPv4() async throws {
  24. let transport = GRPCHTTP2Core.HTTP2ServerTransport.TransportServices(
  25. address: .ipv4(host: "0.0.0.0", port: 0),
  26. config: .defaults()
  27. )
  28. try await withThrowingDiscardingTaskGroup { group in
  29. group.addTask {
  30. try await transport.listen { _ in }
  31. }
  32. group.addTask {
  33. let address = try await transport.listeningAddress
  34. let ipv4Address = try XCTUnwrap(address.ipv4)
  35. XCTAssertNotEqual(ipv4Address.port, 0)
  36. transport.beginGracefulShutdown()
  37. }
  38. }
  39. }
  40. func testGetListeningAddress_IPv6() async throws {
  41. let transport = GRPCHTTP2Core.HTTP2ServerTransport.TransportServices(
  42. address: .ipv6(host: "::1", port: 0),
  43. config: .defaults()
  44. )
  45. try await withThrowingDiscardingTaskGroup { group in
  46. group.addTask {
  47. try await transport.listen { _ in }
  48. }
  49. group.addTask {
  50. let address = try await transport.listeningAddress
  51. let ipv6Address = try XCTUnwrap(address.ipv6)
  52. XCTAssertNotEqual(ipv6Address.port, 0)
  53. transport.beginGracefulShutdown()
  54. }
  55. }
  56. }
  57. func testGetListeningAddress_UnixDomainSocket() async throws {
  58. let transport = GRPCHTTP2Core.HTTP2ServerTransport.TransportServices(
  59. address: .unixDomainSocket(path: "/tmp/niots-uds-test"),
  60. config: .defaults()
  61. )
  62. defer {
  63. // NIOTS does not unlink the UDS on close.
  64. try? FileManager.default.removeItem(atPath: "/tmp/niots-uds-test")
  65. }
  66. try await withThrowingDiscardingTaskGroup { group in
  67. group.addTask {
  68. try await transport.listen { _ in }
  69. }
  70. group.addTask {
  71. let address = try await transport.listeningAddress
  72. XCTAssertEqual(
  73. address.unixDomainSocket,
  74. GRPCHTTP2Core.SocketAddress.UnixDomainSocket(path: "/tmp/niots-uds-test")
  75. )
  76. transport.beginGracefulShutdown()
  77. }
  78. }
  79. }
  80. func testGetListeningAddress_InvalidAddress() async {
  81. let transport = GRPCHTTP2Core.HTTP2ServerTransport.TransportServices(
  82. address: .unixDomainSocket(path: "/this/should/be/an/invalid/path"),
  83. config: .defaults()
  84. )
  85. try? await withThrowingDiscardingTaskGroup { group in
  86. group.addTask {
  87. try await transport.listen { _ in }
  88. }
  89. group.addTask {
  90. do {
  91. _ = try await transport.listeningAddress
  92. XCTFail("Should have thrown a RuntimeError")
  93. } catch let error as RuntimeError {
  94. XCTAssertEqual(error.code, .serverIsStopped)
  95. XCTAssertEqual(
  96. error.message,
  97. """
  98. There is no listening address bound for this server: there may have \
  99. been an error which caused the transport to close, or it may have shut down.
  100. """
  101. )
  102. }
  103. }
  104. }
  105. }
  106. func testGetListeningAddress_StoppedListening() async throws {
  107. let transport = GRPCHTTP2Core.HTTP2ServerTransport.TransportServices(
  108. address: .ipv4(host: "0.0.0.0", port: 0),
  109. config: .defaults()
  110. )
  111. try? await withThrowingDiscardingTaskGroup { group in
  112. group.addTask {
  113. try await transport.listen { _ in }
  114. do {
  115. _ = try await transport.listeningAddress
  116. XCTFail("Should have thrown a RuntimeError")
  117. } catch let error as RuntimeError {
  118. XCTAssertEqual(error.code, .serverIsStopped)
  119. XCTAssertEqual(
  120. error.message,
  121. """
  122. There is no listening address bound for this server: there may have \
  123. been an error which caused the transport to close, or it may have shut down.
  124. """
  125. )
  126. }
  127. }
  128. group.addTask {
  129. let address = try await transport.listeningAddress
  130. XCTAssertNotNil(address.ipv4)
  131. transport.beginGracefulShutdown()
  132. }
  133. }
  134. }
  135. }
  136. #endif