HTTP2TransportNIOPosixTests.swift 4.8 KB

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