@@ -294,7 +294,7 @@ class HTTPConnectionPoolTests: XCTestCase {
294
294
let eventLoop = eventLoopGroup. next ( )
295
295
defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
296
296
297
- let request = try ! HTTPClient . Request ( url: " http://localhost:9000 " )
297
+ let request = try ! HTTPClient . Request ( url: " http://localhost: \( httpBin . port ) " )
298
298
let poolDelegate = TestDelegate ( eventLoop: eventLoop)
299
299
300
300
let pool = HTTPConnectionPool (
@@ -318,7 +318,7 @@ class HTTPConnectionPoolTests: XCTestCase {
318
318
319
319
var maybeRequest : HTTPClient . Request ?
320
320
var maybeRequestBag : RequestBag < ResponseAccumulator > ?
321
- XCTAssertNoThrow ( maybeRequest = try HTTPClient . Request ( url: " https ://localhost:\( httpBin. port) " ) )
321
+ XCTAssertNoThrow ( maybeRequest = try HTTPClient . Request ( url: " http ://localhost:\( httpBin. port) " ) )
322
322
XCTAssertNoThrow ( maybeRequestBag = try RequestBag (
323
323
request: XCTUnwrap ( maybeRequest) ,
324
324
eventLoopPreference: . indifferent,
@@ -332,13 +332,63 @@ class HTTPConnectionPoolTests: XCTestCase {
332
332
guard let requestBag = maybeRequestBag else { return XCTFail ( " Expected to get a request " ) }
333
333
334
334
pool. executeRequest ( requestBag)
335
- XCTAssertNoThrow ( try eventLoop. scheduleTask ( in: . milliseconds ( 100 ) ) { } . futureResult. wait ( ) )
335
+ XCTAssertNoThrow ( try eventLoop. scheduleTask ( in: . seconds ( 1 ) ) { } . futureResult. wait ( ) )
336
336
requestBag. cancel ( )
337
337
338
338
XCTAssertThrowsError ( try requestBag. task. futureResult. wait ( ) ) {
339
339
XCTAssertEqual ( $0 as? HTTPClientError , . cancelled)
340
340
}
341
+ XCTAssertGreaterThanOrEqual ( httpBin. createdConnections, 3 )
342
+ }
343
+
344
+ func testConnectionShutdownIsCalledOnActiveConnections( ) {
345
+ let httpBin = HTTPBin ( )
346
+ defer { XCTAssertNoThrow ( try httpBin. shutdown ( ) ) }
347
+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
348
+ let eventLoop = eventLoopGroup. next ( )
349
+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
350
+
351
+ let request = try ! HTTPClient . Request ( url: " http://localhost: \( httpBin. port) " )
352
+ let poolDelegate = TestDelegate ( eventLoop: eventLoop)
353
+
354
+ let pool = HTTPConnectionPool (
355
+ eventLoopGroup: eventLoopGroup,
356
+ sslContextCache: . init( ) ,
357
+ tlsConfiguration: . none,
358
+ clientConfiguration: . init( ) ,
359
+ key: . init( request) ,
360
+ delegate: poolDelegate,
361
+ idGenerator: . init( ) ,
362
+ backgroundActivityLogger: . init( label: " test " )
363
+ )
364
+
365
+ var maybeRequest : HTTPClient . Request ?
366
+ var maybeRequestBag : RequestBag < ResponseAccumulator > ?
367
+ XCTAssertNoThrow ( maybeRequest = try HTTPClient . Request ( url: " http://localhost: \( httpBin. port) /wait " ) )
368
+ XCTAssertNoThrow ( maybeRequestBag = try RequestBag (
369
+ request: XCTUnwrap ( maybeRequest) ,
370
+ eventLoopPreference: . indifferent,
371
+ task: . init( eventLoop: eventLoopGroup. next ( ) , logger: . init( label: " test " ) ) ,
372
+ redirectHandler: nil ,
373
+ connectionDeadline: . now( ) + . seconds( 5 ) ,
374
+ idleReadTimeout: nil ,
375
+ delegate: ResponseAccumulator ( request: XCTUnwrap ( maybeRequest) )
376
+ ) )
377
+
378
+ guard let requestBag = maybeRequestBag else { return XCTFail ( " Expected to get a request " ) }
379
+
380
+ pool. executeRequest ( requestBag)
381
+ XCTAssertNoThrow ( try eventLoop. scheduleTask ( in: . milliseconds( 500 ) ) { } . futureResult. wait ( ) )
382
+ pool. shutdown ( )
383
+
384
+ XCTAssertNoThrow ( try poolDelegate. future. wait ( ) )
385
+
386
+ XCTAssertThrowsError ( try requestBag. task. futureResult. wait ( ) ) {
387
+ XCTAssertEqual ( $0 as? HTTPClientError , . cancelled)
388
+ }
389
+
341
390
XCTAssertGreaterThanOrEqual ( httpBin. createdConnections, 1 )
391
+ XCTAssertGreaterThanOrEqual ( httpBin. activeConnections, 0 )
342
392
}
343
393
}
344
394
0 commit comments