HTTP2TransportNIOTransportServicesTests.swift 4.5 KB

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