Skip to content

Commit 6aa4a1d

Browse files
committed
convert upload function with concurrency
1 parent 815ade1 commit 6aa4a1d

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

Tests/MultipartFormDataParserTests/_TestFunctions/TestFunction_APIKit.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,27 @@ extension XCTestCase {
3535
retryCount: UInt = 3,
3636
file: StaticString = #filePath,
3737
line: UInt = #line
38-
) throws -> TestEntity {
39-
let exp = expectation(description: "response")
38+
) async throws -> TestEntity {
4039
let request = TestRequest(
4140
genbaNeko: genbaNeko,
4241
denwaNeko: denwaNeko,
4342
message: message,
4443
file: file,
4544
line: line
4645
)
47-
var result: Result<TestRequest.Response, SessionTaskError>!
48-
session.send(request, callbackQueue: nil) {
49-
result = $0
50-
exp.fulfill()
46+
let result = await withCheckedContinuation { cont in
47+
session.send(request, callbackQueue: nil) {
48+
cont.resume(returning: $0)
49+
}
5150
}
5251

53-
wait(for: [exp], timeout: 10)
54-
55-
switch result! {
52+
switch result {
5653
case let .success(response):
5754
return response
5855
case let .failure(error):
5956
if retryCount > 0 {
6057
print("retry: \(retryCount)")
61-
return try uploadWithAPIKit(genbaNeko: genbaNeko, denwaNeko: denwaNeko, message: message, retryCount: retryCount - 1, file: file, line: line)
58+
return try await uploadWithAPIKit(genbaNeko: genbaNeko, denwaNeko: denwaNeko, message: message, retryCount: retryCount - 1, file: file, line: line)
6259
}
6360
throw error
6461
}

Tests/MultipartFormDataParserTests/_TestFunctions/TestFunction_Alamofire.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ extension XCTestCase {
1818
retryCount: UInt = 3,
1919
file: StaticString = #filePath,
2020
line: UInt = #line
21-
) throws -> TestEntity? {
22-
let exp = expectation(description: "response")
21+
) async throws -> TestEntity? {
2322
let task = session.upload(
2423
multipartFormData: { formData in
2524
formData.append(
@@ -39,17 +38,15 @@ extension XCTestCase {
3938
to: "https://localhost/upload",
4039
interceptor: Interceptor()
4140
)
42-
var response: AFDataResponse<TestEntity>!
4341
let decoder = JSONDecoder()
4442
decoder.keyDecodingStrategy = .convertFromSnakeCase
45-
task.responseDecodable(of: TestEntity.self, decoder: decoder) {
46-
response = $0
47-
exp.fulfill()
48-
}
49-
wait(for: [exp], timeout: timeoutInterval)
5043

51-
XCTAssertNotNil(response, file: file, line: line)
52-
XCTAssertEqual(response?.response?.statusCode, 200, file: file, line: line)
44+
let response = await withCheckedContinuation { cont in
45+
task.responseDecodable(of: TestEntity.self, decoder: decoder) {
46+
cont.resume(returning: $0)
47+
}
48+
}
49+
XCTAssertEqual(response.response?.statusCode, 200, file: file, line: line)
5350
switch response.result {
5451
case let .success(entity): return entity
5552
case let .failure(error): throw error

0 commit comments

Comments
 (0)