Skip to content

Commit 26afbc1

Browse files
authored
make sure HTTPClient is shutdown (#98)
Motivation: Right now, HTTPClient only asserts that it's shut down if it was started with its own EventLoopGroup. That however is weird because it's lifecycle model depends on the parameters you pass to `init`. Modifications: Always validate the lifecycle (in debug mode). Result: API makes more sense.
1 parent bbf7966 commit 26afbc1

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

Sources/AsyncHTTPClient/HTTPClient.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,7 @@ public class HTTPClient {
6666
}
6767

6868
deinit {
69-
switch self.eventLoopGroupProvider {
70-
case .shared:
71-
return
72-
case .createNew:
73-
assert(self.isShutdown.load(), "Client not stopped before the deinit.")
74-
}
69+
assert(self.isShutdown.load(), "Client not shut down before the deinit. Please call client.syncShutdown() when no longer needed.")
7570
}
7671

7772
/// Shuts down the client and `EventLoopGroup` if it was created by the client.

Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class HTTPClientInternalTests: XCTestCase {
7777
let httpBin = HttpBin()
7878
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
7979
defer {
80-
try! httpClient.syncShutdown()
80+
XCTAssertNoThrow(try httpClient.syncShutdown())
8181
httpBin.shutdown()
8282
}
8383

@@ -107,7 +107,7 @@ class HTTPClientInternalTests: XCTestCase {
107107
let httpBin = HttpBin()
108108
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
109109
defer {
110-
try! httpClient.syncShutdown()
110+
XCTAssertNoThrow(try httpClient.syncShutdown())
111111
httpBin.shutdown()
112112
}
113113

@@ -168,7 +168,7 @@ class HTTPClientInternalTests: XCTestCase {
168168
let httpBin = HttpBin(channelPromise: promise)
169169

170170
defer {
171-
try! httpClient.syncShutdown()
171+
XCTAssertNoThrow(try httpClient.syncShutdown())
172172
httpBin.shutdown()
173173
}
174174

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class HTTPClientTests: XCTestCase {
5454
let httpBin = HttpBin()
5555
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
5656
defer {
57-
try! httpClient.syncShutdown()
57+
XCTAssertNoThrow(try httpClient.syncShutdown())
5858
httpBin.shutdown()
5959
}
6060

@@ -67,7 +67,8 @@ class HTTPClientTests: XCTestCase {
6767
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 8)
6868
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(elg))
6969
defer {
70-
try! elg.syncShutdownGracefully()
70+
XCTAssertNoThrow(try httpClient.syncShutdown())
71+
XCTAssertNoThrow(try elg.syncShutdownGracefully())
7172
httpBin.shutdown()
7273
}
7374

@@ -85,7 +86,7 @@ class HTTPClientTests: XCTestCase {
8586
let httpBin = HttpBin()
8687
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
8788
defer {
88-
try! httpClient.syncShutdown()
89+
XCTAssertNoThrow(try httpClient.syncShutdown())
8990
httpBin.shutdown()
9091
}
9192

@@ -102,7 +103,7 @@ class HTTPClientTests: XCTestCase {
102103
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew,
103104
configuration: HTTPClient.Configuration(certificateVerification: .none))
104105
defer {
105-
try! httpClient.syncShutdown()
106+
XCTAssertNoThrow(try httpClient.syncShutdown())
106107
httpBin.shutdown()
107108
}
108109

@@ -115,7 +116,7 @@ class HTTPClientTests: XCTestCase {
115116
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew,
116117
configuration: HTTPClient.Configuration(certificateVerification: .none))
117118
defer {
118-
try! httpClient.syncShutdown()
119+
XCTAssertNoThrow(try httpClient.syncShutdown())
119120
httpBin.shutdown()
120121
}
121122

@@ -136,7 +137,7 @@ class HTTPClientTests: XCTestCase {
136137
configuration: HTTPClient.Configuration(certificateVerification: .none, followRedirects: true))
137138

138139
defer {
139-
try! httpClient.syncShutdown()
140+
XCTAssertNoThrow(try httpClient.syncShutdown())
140141
httpBin.shutdown()
141142
httpsBin.shutdown()
142143
}
@@ -154,7 +155,7 @@ class HTTPClientTests: XCTestCase {
154155
configuration: HTTPClient.Configuration(certificateVerification: .none, followRedirects: true))
155156

156157
defer {
157-
try! httpClient.syncShutdown()
158+
XCTAssertNoThrow(try httpClient.syncShutdown())
158159
httpBin.shutdown()
159160
}
160161

@@ -176,7 +177,7 @@ class HTTPClientTests: XCTestCase {
176177
let httpBin = HttpBin()
177178
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
178179
defer {
179-
try! httpClient.syncShutdown()
180+
XCTAssertNoThrow(try httpClient.syncShutdown())
180181
httpBin.shutdown()
181182
}
182183

@@ -187,7 +188,7 @@ class HTTPClientTests: XCTestCase {
187188
func testMultipleContentLengthHeaders() throws {
188189
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
189190
defer {
190-
try! httpClient.syncShutdown()
191+
XCTAssertNoThrow(try httpClient.syncShutdown())
191192
}
192193
let httpBin = HttpBin()
193194
defer {
@@ -208,7 +209,7 @@ class HTTPClientTests: XCTestCase {
208209
let httpBin = HttpBin()
209210
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
210211
defer {
211-
try! httpClient.syncShutdown()
212+
XCTAssertNoThrow(try httpClient.syncShutdown())
212213
httpBin.shutdown()
213214
}
214215

@@ -226,7 +227,7 @@ class HTTPClientTests: XCTestCase {
226227
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
227228

228229
defer {
229-
try! httpClient.syncShutdown()
230+
XCTAssertNoThrow(try httpClient.syncShutdown())
230231
httpBin.shutdown()
231232
}
232233

@@ -242,7 +243,7 @@ class HTTPClientTests: XCTestCase {
242243
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew, configuration: HTTPClient.Configuration(timeout: HTTPClient.Configuration.Timeout(read: .milliseconds(150))))
243244

244245
defer {
245-
try! httpClient.syncShutdown()
246+
XCTAssertNoThrow(try httpClient.syncShutdown())
246247
httpBin.shutdown()
247248
}
248249

@@ -258,7 +259,7 @@ class HTTPClientTests: XCTestCase {
258259
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
259260

260261
defer {
261-
try! httpClient.syncShutdown()
262+
XCTAssertNoThrow(try httpClient.syncShutdown())
262263
httpBin.shutdown()
263264
}
264265

@@ -274,7 +275,7 @@ class HTTPClientTests: XCTestCase {
274275
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
275276

276277
defer {
277-
try! httpClient.syncShutdown()
278+
XCTAssertNoThrow(try httpClient.syncShutdown())
278279
httpBin.shutdown()
279280
}
280281

@@ -300,7 +301,7 @@ class HTTPClientTests: XCTestCase {
300301
configuration: .init(proxy: .server(host: "localhost", port: httpBin.port))
301302
)
302303
defer {
303-
try! httpClient.syncShutdown()
304+
XCTAssertNoThrow(try httpClient.syncShutdown())
304305
httpBin.shutdown()
305306
}
306307
let res = try httpClient.get(url: "http://test/ok").wait()
@@ -317,7 +318,7 @@ class HTTPClientTests: XCTestCase {
317318
)
318319
)
319320
defer {
320-
try! httpClient.syncShutdown()
321+
XCTAssertNoThrow(try httpClient.syncShutdown())
321322
httpBin.shutdown()
322323
}
323324
let res = try httpClient.get(url: "https://test/ok").wait()
@@ -328,7 +329,7 @@ class HTTPClientTests: XCTestCase {
328329
let httpBin = HttpBin()
329330
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
330331
defer {
331-
try! httpClient.syncShutdown()
332+
XCTAssertNoThrow(try httpClient.syncShutdown())
332333
httpBin.shutdown()
333334
}
334335

@@ -354,7 +355,7 @@ class HTTPClientTests: XCTestCase {
354355
configuration: HTTPClient.Configuration(certificateVerification: .none))
355356

356357
defer {
357-
try! httpClient.syncShutdown()
358+
XCTAssertNoThrow(try httpClient.syncShutdown())
358359
httpBin.shutdown()
359360
}
360361

@@ -371,7 +372,7 @@ class HTTPClientTests: XCTestCase {
371372
configuration: HTTPClient.Configuration(certificateVerification: .none, ignoreUncleanSSLShutdown: true))
372373

373374
defer {
374-
try! httpClient.syncShutdown()
375+
XCTAssertNoThrow(try httpClient.syncShutdown())
375376
httpBin.shutdown()
376377
}
377378

@@ -389,7 +390,7 @@ class HTTPClientTests: XCTestCase {
389390
configuration: HTTPClient.Configuration(certificateVerification: .none))
390391

391392
defer {
392-
try! httpClient.syncShutdown()
393+
XCTAssertNoThrow(try httpClient.syncShutdown())
393394
httpBin.shutdown()
394395
}
395396

@@ -407,7 +408,7 @@ class HTTPClientTests: XCTestCase {
407408
configuration: HTTPClient.Configuration(certificateVerification: .none))
408409

409410
defer {
410-
try! httpClient.syncShutdown()
411+
XCTAssertNoThrow(try httpClient.syncShutdown())
411412
httpBin.shutdown()
412413
}
413414

@@ -423,7 +424,7 @@ class HTTPClientTests: XCTestCase {
423424
configuration: HTTPClient.Configuration(certificateVerification: .none))
424425

425426
defer {
426-
try! httpClient.syncShutdown()
427+
XCTAssertNoThrow(try httpClient.syncShutdown())
427428
httpBin.shutdown()
428429
}
429430

@@ -440,7 +441,7 @@ class HTTPClientTests: XCTestCase {
440441
configuration: HTTPClient.Configuration(certificateVerification: .none, ignoreUncleanSSLShutdown: true))
441442

442443
defer {
443-
try! httpClient.syncShutdown()
444+
XCTAssertNoThrow(try httpClient.syncShutdown())
444445
httpBin.shutdown()
445446
}
446447

@@ -457,7 +458,7 @@ class HTTPClientTests: XCTestCase {
457458
configuration: HTTPClient.Configuration(certificateVerification: .none))
458459

459460
defer {
460-
try! httpClient.syncShutdown()
461+
XCTAssertNoThrow(try httpClient.syncShutdown())
461462
httpBin.shutdown()
462463
}
463464

@@ -474,7 +475,7 @@ class HTTPClientTests: XCTestCase {
474475
configuration: HTTPClient.Configuration(certificateVerification: .none, ignoreUncleanSSLShutdown: true))
475476

476477
defer {
477-
try! httpClient.syncShutdown()
478+
XCTAssertNoThrow(try httpClient.syncShutdown())
478479
httpBin.shutdown()
479480
}
480481

@@ -491,7 +492,8 @@ class HTTPClientTests: XCTestCase {
491492
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup),
492493
configuration: HTTPClient.Configuration(followRedirects: true))
493494
defer {
494-
try! eventLoopGroup.syncShutdownGracefully()
495+
XCTAssertNoThrow(try httpClient.syncShutdown())
496+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
495497
httpBin.shutdown()
496498
}
497499

0 commit comments

Comments
 (0)