Skip to content

Commit 5a4702f

Browse files
authored
Add Pause/resume swift integration tests (#10591)
1 parent 75cc4df commit 5a4702f

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

FirebaseStorage/Tests/Integration/StorageIntegration.swift

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,88 @@ class StorageResultTests: StorageIntegrationCommon {
587587
waitForExpectations()
588588
}
589589

590+
func testResumeGetFile() {
591+
let expectation = self.expectation(description: #function)
592+
let expectationPause = self.expectation(description: "pause")
593+
let expectationResume = self.expectation(description: "resume")
594+
let ref = storage.reference().child("ios/public/1mb")
595+
596+
let fileURL = URL(fileURLWithPath: "\(NSTemporaryDirectory())/hello.txt")
597+
let task = ref.write(toFile: fileURL)
598+
var downloadedBytes: Int64 = 0
599+
var resumeAtBytes = 256 * 1024
600+
601+
task.observe(StorageTaskStatus.success) { snapshot in
602+
XCTAssertEqual(snapshot.description, "<State: Success>")
603+
expectation.fulfill()
604+
}
605+
task.observe(StorageTaskStatus.progress) { snapshot in
606+
let description = snapshot.description
607+
XCTAssertTrue(description.contains("State: Progress") ||
608+
description.contains("State: Resume"))
609+
let progress = snapshot.progress
610+
if let completed = progress?.completedUnitCount {
611+
XCTAssertGreaterThanOrEqual(completed, downloadedBytes)
612+
downloadedBytes = completed
613+
if completed > resumeAtBytes {
614+
task.pause()
615+
expectationPause.fulfill()
616+
resumeAtBytes = Int.max
617+
}
618+
}
619+
}
620+
task.observe(StorageTaskStatus.pause) { snapshot in
621+
XCTAssertEqual(snapshot.description, "<State: Paused>")
622+
task.resume()
623+
expectationResume.fulfill()
624+
}
625+
waitForExpectations()
626+
XCTAssertEqual(resumeAtBytes, Int.max)
627+
}
628+
629+
func testResumeGetFileInBackgroundQueue() {
630+
let expectation = self.expectation(description: #function)
631+
let expectationPause = self.expectation(description: "pause")
632+
let expectationResume = self.expectation(description: "resume")
633+
let ref = storage.reference().child("ios/public/1mb")
634+
635+
let fileURL = URL(fileURLWithPath: "\(NSTemporaryDirectory())/hello.txt")
636+
let task = ref.write(toFile: fileURL)
637+
var downloadedBytes: Int64 = 0
638+
var resumeAtBytes = 256 * 1024
639+
640+
task.observe(StorageTaskStatus.success) { snapshot in
641+
XCTAssertEqual(snapshot.description, "<State: Success>")
642+
expectation.fulfill()
643+
}
644+
task.observe(StorageTaskStatus.progress) { snapshot in
645+
let description = snapshot.description
646+
XCTAssertTrue(description.contains("State: Progress") ||
647+
description.contains("State: Resume"))
648+
let progress = snapshot.progress
649+
if let completed = progress?.completedUnitCount {
650+
XCTAssertGreaterThanOrEqual(completed, downloadedBytes)
651+
downloadedBytes = completed
652+
if completed > resumeAtBytes {
653+
DispatchQueue.global(qos: .background).async {
654+
task.pause()
655+
}
656+
expectationPause.fulfill()
657+
resumeAtBytes = Int.max
658+
}
659+
}
660+
}
661+
task.observe(StorageTaskStatus.pause) { snapshot in
662+
XCTAssertEqual(snapshot.description, "<State: Paused>")
663+
DispatchQueue.global(qos: .background).async {
664+
task.resume()
665+
}
666+
expectationResume.fulfill()
667+
}
668+
waitForExpectations()
669+
XCTAssertEqual(resumeAtBytes, Int.max)
670+
}
671+
590672
func testPagedListFiles() {
591673
let expectation = self.expectation(description: #function)
592674
let ref = storage.reference(withPath: "ios/public/list")

0 commit comments

Comments
 (0)