HTTP2TransportNIOTransportServicesTests.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. try await withThrowingDiscardingTaskGroup { group in
  60. group.addTask {
  61. try await transport.listen { _ in }
  62. }
  63. group.addTask {
  64. let address = try await transport.listeningAddress
  65. XCTAssertEqual(
  66. address.unixDomainSocket,
  67. GRPCHTTP2Core.SocketAddress.UnixDomainSocket(path: "/tmp/niots-uds-test")
  68. )
  69. transport.stopListening()
  70. }
  71. }
  72. }
  73. func testGetListeningAddress_InvalidAddress() async {
  74. let transport = GRPCHTTP2Core.HTTP2ServerTransport.TransportServices(
  75. address: .unixDomainSocket(path: "/this/should/be/an/invalid/path")
  76. )
  77. try? await withThrowingDiscardingTaskGroup { group in
  78. group.addTask {
  79. try await transport.listen { _ in }
  80. }
  81. group.addTask {
  82. do {
  83. _ = try await transport.listeningAddress
  84. XCTFail("Should have thrown a RuntimeError")
  85. } catch let error as RuntimeError {
  86. XCTAssertEqual(error.code, .serverIsStopped)
  87. XCTAssertEqual(
  88. error.message,
  89. """
  90. There is no listening address bound for this server: there may have \
  91. been an error which caused the transport to close, or it may have shut down.
  92. """
  93. )
  94. }
  95. }
  96. }
  97. }
  98. func testGetListeningAddress_StoppedListening() async throws {
  99. let transport = GRPCHTTP2Core.HTTP2ServerTransport.TransportServices(
  100. address: .ipv4(host: "0.0.0.0", port: 0)
  101. )
  102. try? await withThrowingDiscardingTaskGroup { group in
  103. group.addTask {
  104. try await transport.listen { _ in }
  105. do {
  106. _ = try await transport.listeningAddress
  107. XCTFail("Should have thrown a RuntimeError")
  108. } catch let error as RuntimeError {
  109. XCTAssertEqual(error.code, .serverIsStopped)
  110. XCTAssertEqual(
  111. error.message,
  112. """
  113. There is no listening address bound for this server: there may have \
  114. been an error which caused the transport to close, or it may have shut down.
  115. """
  116. )
  117. }
  118. }
  119. group.addTask {
  120. let address = try await transport.listeningAddress
  121. XCTAssertNotNil(address.ipv4)
  122. transport.stopListening()
  123. }
  124. }
  125. }
  126. }
  127. #endif