@@ -4326,28 +4326,54 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
4326
4326
config. tlsConfiguration? . certificateVerification = . none
4327
4327
}
4328
4328
4329
- let client = HTTPClient (
4329
+ let higherConnectTimeout = CountingDebugInitializerUtil . duration + . milliseconds( 100 )
4330
+ var configWithHigherTimeout = config
4331
+ configWithHigherTimeout. timeout = . init( connect: higherConnectTimeout)
4332
+
4333
+ let clientWithHigherTimeout = HTTPClient (
4330
4334
eventLoopGroupProvider: . singleton,
4331
- configuration: config ,
4335
+ configuration: configWithHigherTimeout ,
4332
4336
backgroundActivityLogger: Logger (
4333
4337
label: " HTTPClient " ,
4334
4338
factory: StreamLogHandler . standardOutput ( label: )
4335
4339
)
4336
4340
)
4337
- defer { XCTAssertNoThrow ( client . shutdown ( ) ) }
4341
+ defer { XCTAssertNoThrow ( try clientWithHigherTimeout . syncShutdown ( ) ) }
4338
4342
4339
4343
let bin = HTTPBin ( . http1_1( ssl: ssl, compress: false ) )
4340
4344
defer { XCTAssertNoThrow ( try bin. shutdown ( ) ) }
4341
4345
4342
4346
let scheme = ssl ? " https " : " http "
4343
4347
4344
4348
for _ in 0 ..< 3 {
4345
- XCTAssertNoThrow ( try client. get ( url: " \( scheme) ://localhost: \( bin. port) /get " ) . wait ( ) )
4349
+ XCTAssertNoThrow (
4350
+ try clientWithHigherTimeout. get ( url: " \( scheme) ://localhost: \( bin. port) /get " ) . wait ( )
4351
+ )
4346
4352
}
4347
4353
4348
4354
// Even though multiple requests were made, the connection debug initializer must be called
4349
4355
// only once.
4350
4356
XCTAssertEqual ( connectionDebugInitializerUtil. executionCount, 1 )
4357
+
4358
+ let lowerConnectTimeout = CountingDebugInitializerUtil . duration - . milliseconds( 100 )
4359
+ var configWithLowerTimeout = config
4360
+ configWithLowerTimeout. timeout = . init( connect: lowerConnectTimeout)
4361
+
4362
+ let clientWithLowerTimeout = HTTPClient (
4363
+ eventLoopGroupProvider: . singleton,
4364
+ configuration: configWithLowerTimeout,
4365
+ backgroundActivityLogger: Logger (
4366
+ label: " HTTPClient " ,
4367
+ factory: StreamLogHandler . standardOutput ( label: )
4368
+ )
4369
+ )
4370
+ defer { XCTAssertNoThrow ( try clientWithLowerTimeout. syncShutdown ( ) ) }
4371
+
4372
+ XCTAssertThrowsError (
4373
+ try clientWithLowerTimeout. get ( url: " \( scheme) ://localhost: \( bin. port) /get " ) . wait ( )
4374
+ ) {
4375
+ XCTAssertEqual ( $0 as? HTTPClientError , . connectTimeout)
4376
+ }
4351
4377
}
4352
4378
4353
4379
func testHTTP1PlainTextConnectionDebugInitializer( ) {
@@ -4378,23 +4404,29 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
4378
4404
config. tlsConfiguration? . certificateVerification = . none
4379
4405
config. httpVersion = . automatic
4380
4406
4381
- let client = HTTPClient (
4407
+ let higherConnectTimeout = CountingDebugInitializerUtil . duration + . milliseconds( 100 )
4408
+ var configWithHigherTimeout = config
4409
+ configWithHigherTimeout. timeout = . init( connect: higherConnectTimeout)
4410
+
4411
+ let clientWithHigherTimeout = HTTPClient (
4382
4412
eventLoopGroupProvider: . singleton,
4383
- configuration: config ,
4413
+ configuration: configWithHigherTimeout ,
4384
4414
backgroundActivityLogger: Logger (
4385
4415
label: " HTTPClient " ,
4386
4416
factory: StreamLogHandler . standardOutput ( label: )
4387
4417
)
4388
4418
)
4389
- defer { XCTAssertNoThrow ( client . shutdown ( ) ) }
4419
+ defer { XCTAssertNoThrow ( try clientWithHigherTimeout . syncShutdown ( ) ) }
4390
4420
4391
4421
let bin = HTTPBin ( . http2( compress: false ) )
4392
4422
defer { XCTAssertNoThrow ( try bin. shutdown ( ) ) }
4393
4423
4394
4424
let numberOfRequests = 3
4395
4425
4396
4426
for _ in 0 ..< numberOfRequests {
4397
- XCTAssertNoThrow ( try client. get ( url: " https://localhost: \( bin. port) /get " ) . wait ( ) )
4427
+ XCTAssertNoThrow (
4428
+ try clientWithHigherTimeout. get ( url: " https://localhost: \( bin. port) /get " ) . wait ( )
4429
+ )
4398
4430
}
4399
4431
4400
4432
// Even though multiple requests were made, the connection debug initializer must be called
@@ -4404,21 +4436,44 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
4404
4436
// The stream channel debug initializer must be called only as much as the number of
4405
4437
// requests made.
4406
4438
XCTAssertEqual ( streamChannelDebugInitializerUtil. executionCount, numberOfRequests)
4439
+
4440
+ let lowerConnectTimeout = CountingDebugInitializerUtil . duration - . milliseconds( 100 )
4441
+ var configWithLowerTimeout = config
4442
+ configWithLowerTimeout. timeout = . init( connect: lowerConnectTimeout)
4443
+
4444
+ let clientWithLowerTimeout = HTTPClient (
4445
+ eventLoopGroupProvider: . singleton,
4446
+ configuration: configWithLowerTimeout,
4447
+ backgroundActivityLogger: Logger (
4448
+ label: " HTTPClient " ,
4449
+ factory: StreamLogHandler . standardOutput ( label: )
4450
+ )
4451
+ )
4452
+ defer { XCTAssertNoThrow ( try clientWithLowerTimeout. syncShutdown ( ) ) }
4453
+
4454
+ XCTAssertThrowsError (
4455
+ try clientWithLowerTimeout. get ( url: " https://localhost: \( bin. port) /get " ) . wait ( )
4456
+ ) {
4457
+ XCTAssertEqual ( $0 as? HTTPClientError , . connectTimeout)
4458
+ }
4407
4459
}
4408
4460
}
4409
4461
4410
4462
final class CountingDebugInitializerUtil : Sendable {
4411
- private let _executionCount : NIOLockedValueBox < Int >
4463
+ private let _executionCount = NIOLockedValueBox < Int > ( 0 )
4412
4464
var executionCount : Int { self . _executionCount. withLockedValue { $0 } }
4413
4465
4466
+ /// The minimum time to spend running the debug initializer.
4467
+ static let duration : TimeAmount = . milliseconds( 300 )
4468
+
4414
4469
/// The actual debug initializer.
4415
4470
func initialize( channel: Channel ) -> EventLoopFuture < Void > {
4416
4471
self . _executionCount. withLockedValue { $0 += 1 }
4417
4472
4418
- return channel. eventLoop. makeSucceededVoidFuture ( )
4419
- }
4473
+ let someScheduledTask = channel. eventLoop. scheduleTask ( in: Self . duration) {
4474
+ return channel. eventLoop. makeSucceededVoidFuture ( )
4475
+ }
4420
4476
4421
- init ( ) {
4422
- self . _executionCount = . init( 0 )
4477
+ return someScheduledTask. futureResult. flatMap { $0 }
4423
4478
}
4424
4479
}
0 commit comments