Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix write timeout not being initialised #730

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/AsyncHTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ extension HTTPClient.Configuration {
) {
self.connect = connect
self.read = read
self.write = write
}
}

Expand Down
40 changes: 35 additions & 5 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Network
import Logging
import NIOConcurrencyHelpers
import NIOCore
import NIOEmbedded
import NIOFoundationCompat
import NIOHTTP1
import NIOHTTPCompression
Expand Down Expand Up @@ -607,6 +608,35 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
}
}

func testWriteTimeout() throws {
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
configuration: HTTPClient.Configuration(timeout: HTTPClient.Configuration.Timeout(write: .nanoseconds(10))))

defer {
XCTAssertNoThrow(try localClient.syncShutdown())
}

// Create a request that writes a chunk, then waits longer than the configured write timeout,
// and then writes again. This should trigger a write timeout error.
let request = try HTTPClient.Request(url: self.defaultHTTPBinURLPrefix + "post",
method: .POST,
headers: ["transfer-encoding": "chunked"],
body: .stream { streamWriter in
_ = streamWriter.write(.byteBuffer(.init()))

let promise = self.clientGroup.next().makePromise(of: Void.self)
self.clientGroup.next().scheduleTask(in: .milliseconds(3)) {
streamWriter.write(.byteBuffer(.init())).cascade(to: promise)
}

return promise.futureResult
})

XCTAssertThrowsError(try localClient.execute(request: request).wait()) {
XCTAssertEqual($0 as? HTTPClientError, .writeTimeout)
}
}

func testConnectTimeout() throws {
#if os(Linux)
// 198.51.100.254 is reserved for documentation only and therefore should not accept any TCP connection
Expand Down Expand Up @@ -1230,8 +1260,8 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
/// openssl req -x509 -newkey rsa:4096 -keyout self_signed_key.pem -out self_signed_cert.pem -sha256 -days 99999 -nodes -subj '/CN=localhost'
let certPath = Bundle.module.path(forResource: "self_signed_cert", ofType: "pem")!
let keyPath = Bundle.module.path(forResource: "self_signed_key", ofType: "pem")!
let configuration = TLSConfiguration.makeServerConfiguration(
certificateChain: try NIOSSLCertificate.fromPEMFile(certPath).map { .certificate($0) },
let configuration = try TLSConfiguration.makeServerConfiguration(
certificateChain: NIOSSLCertificate.fromPEMFile(certPath).map { .certificate($0) },
privateKey: .file(keyPath)
)
let sslContext = try NIOSSLContext(configuration: configuration)
Expand Down Expand Up @@ -1270,8 +1300,8 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
/// openssl req -x509 -newkey rsa:4096 -keyout self_signed_key.pem -out self_signed_cert.pem -sha256 -days 99999 -nodes -subj '/CN=localhost'
let certPath = Bundle.module.path(forResource: "self_signed_cert", ofType: "pem")!
let keyPath = Bundle.module.path(forResource: "self_signed_key", ofType: "pem")!
let configuration = TLSConfiguration.makeServerConfiguration(
certificateChain: try NIOSSLCertificate.fromPEMFile(certPath).map { .certificate($0) },
let configuration = try TLSConfiguration.makeServerConfiguration(
certificateChain: NIOSSLCertificate.fromPEMFile(certPath).map { .certificate($0) },
privateKey: .file(keyPath)
)
let sslContext = try NIOSSLContext(configuration: configuration)
Expand Down Expand Up @@ -2728,7 +2758,7 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {

func uploader(_ streamWriter: HTTPClient.Body.StreamWriter) -> EventLoopFuture<Void> {
let done = streamWriter.write(.byteBuffer(ByteBuffer(string: "X")))
done.recover { error -> Void in
done.recover { error in
XCTFail("unexpected error \(error)")
}.whenSuccess {
// This is executed when we have already sent the end of the request.
Expand Down