Skip to content

Commit bda3426

Browse files
author
Ruben Nine
committed
Ensuring completion handler is called when uploadables is either nil or empty and MultipartUpload is cancelled.
1 parent aa3b9fc commit bda3426

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

FilestackSDK/Internal/Uploaders/MultipartUpload.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ class MultipartUpload: Uploader, DeferredAdd {
6868

6969
uploadQueue.sync {
7070
state = .cancelled
71-
operationQueue.cancelAllOperations()
71+
72+
if uploadables == nil || uploadables?.count == 0 {
73+
finish(with: [])
74+
} else {
75+
operationQueue.cancelAllOperations()
76+
}
7277
}
7378

7479
return true

FilestackSDKTests/UploadTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,46 @@ class UploadTests: XCTestCase {
131131
XCTAssertNotNil(error)
132132
}
133133

134+
func testCancellingStartedUploadWithoutUploadablesShouldCallCompletionHandler() {
135+
let expectation = self.expectation(description: "request should succeed")
136+
var response: [JSONResponse] = []
137+
138+
let uploadOptions = UploadOptions(preferIntelligentIngestion: true, startImmediately: true)
139+
140+
let uploader = client.upload(options: uploadOptions) { resp in
141+
response = resp
142+
expectation.fulfill()
143+
}
144+
145+
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
146+
uploader.cancel()
147+
}
148+
149+
waitForExpectations(timeout: 15, handler: nil)
150+
151+
XCTAssertEqual(response, [])
152+
}
153+
154+
func testCancellingNotStartedUploadWithoutUploadablesShouldCallCompletionHandler() {
155+
let expectation = self.expectation(description: "request should succeed")
156+
var response: [JSONResponse] = []
157+
158+
let uploadOptions = UploadOptions(preferIntelligentIngestion: true, startImmediately: false)
159+
160+
let uploader = client.upload(options: uploadOptions) { resp in
161+
response = resp
162+
expectation.fulfill()
163+
}
164+
165+
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
166+
uploader.cancel()
167+
}
168+
169+
waitForExpectations(timeout: 15, handler: nil)
170+
171+
XCTAssertEqual(response, [])
172+
}
173+
134174
func testResumableMultiPartUploadWithDownNetworkOnStart() {
135175
let uploadMultipartStartStubConditions =
136176
isScheme(Constants.uploadURL.scheme!) &&

0 commit comments

Comments
 (0)