Skip to content

Commit afb173a

Browse files
authored
Close channel if streamClosed returns close (#723)
1 parent c645547 commit afb173a

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

Sources/Hummingbird/Environment.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ public struct Environment: Sendable, Decodable, ExpressibleByDictionaryLiteral {
164164
}
165165

166166
/// Create Environment initialised from the `.env` file
167-
public static func dotEnv(_ dovEnvPath: String = ".env") async throws -> Self {
168-
guard let dotEnv = await loadDotEnv(dovEnvPath) else { return [:] }
167+
public static func dotEnv(_ dotEnvPath: String = ".env") async throws -> Self {
168+
guard let dotEnv = await loadDotEnv(dotEnvPath) else { return [:] }
169169
return try .init(rawValues: self.parseDotEnv(dotEnv))
170170
}
171171

172172
/// Load `.env` file into string
173-
internal static func loadDotEnv(_ dovEnvPath: String = ".env") async -> String? {
173+
internal static func loadDotEnv(_ dotEnvPath: String = ".env") async -> String? {
174174
do {
175-
let fileHandle = try NIOFileHandle(path: dovEnvPath)
175+
let fileHandle = try NIOFileHandle(path: dotEnvPath)
176176
defer {
177177
try? fileHandle.close()
178178
}

Sources/HummingbirdHTTP2/HTTP2ServerConnectionManager.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ extension HTTP2ServerConnectionManager {
293293
loopBoundHandler.triggerGracefulShutdown()
294294
}
295295
case .close:
296-
LoopBoundHandler(self).triggerGracefulShutdown()
296+
channelHandlerContext?.close(promise: nil)
297+
297298
case .none:
298299
break
299300
}

Tests/HummingbirdHTTP2Tests/HTTP2Tests.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import NIOHTTP1
2323
import NIOHTTPTypes
2424
import NIOPosix
2525
import NIOSSL
26+
import ServiceLifecycle
2627
import XCTest
2728

2829
final class HummingBirdHTTP2Tests: XCTestCase {
@@ -243,4 +244,54 @@ final class HummingBirdHTTP2Tests: XCTestCase {
243244
}
244245
)
245246
}
247+
248+
func testChildChannelGracefulShutdown() async throws {
249+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)
250+
defer {
251+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
252+
}
253+
var logger = Logger(label: "Hummingbird")
254+
logger.logLevel = .trace
255+
256+
var tlsConfiguration = try getClientTLSConfiguration()
257+
// no way to override the SSL server name with AsyncHTTPClient so need to set
258+
// hostname verification off
259+
tlsConfiguration.certificateVerification = .noHostnameVerification
260+
try await withHTTPClient(.init(tlsConfiguration: tlsConfiguration)) { httpClient in
261+
try await withThrowingTaskGroup(of: Void.self) { group in
262+
let promise = Promise<Int>()
263+
let server = try HTTPServerBuilder.http2Upgrade(tlsConfiguration: getServerTLSConfiguration()).buildServer(
264+
configuration: .init(address: .hostname(port: 0), serverName: testServerName),
265+
eventLoopGroup: eventLoopGroup,
266+
logger: logger,
267+
responder: { (_, responseWriter: consuming ResponseWriter, _) in
268+
try await Task.sleep(for: .seconds(2))
269+
try await responseWriter.writeResponse(.init(status: .ok))
270+
},
271+
onServerRunning: { await promise.complete($0.localAddress!.port!) }
272+
)
273+
let serviceGroup = ServiceGroup(
274+
configuration: .init(
275+
services: [server],
276+
gracefulShutdownSignals: [.sigterm, .sigint],
277+
logger: logger
278+
)
279+
)
280+
281+
group.addTask {
282+
try await serviceGroup.run()
283+
}
284+
285+
let port = await promise.wait()
286+
group.addTask {
287+
let request = HTTPClientRequest(url: "https://localhost:\(port)/")
288+
let response = try await httpClient.execute(request, deadline: .now() + .seconds(30))
289+
XCTAssertEqual(response.status, .ok)
290+
}
291+
try await Task.sleep(for: .milliseconds(500))
292+
await serviceGroup.triggerGracefulShutdown()
293+
}
294+
}
295+
}
296+
246297
}

0 commit comments

Comments
 (0)