Skip to content

Commit e25f178

Browse files
committed
add more comments
1 parent 79fa6e8 commit e25f178

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

Sources/Hub/Downloader.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ class Downloader: NSObject, ObservableObject {
5858
setupDownload(from: url, with: authToken, resumeSize: resumeSize, headers: headers, expectedSize: expectedSize, timeout: timeout, numRetries: numRetries)
5959
}
6060

61+
/// Sets up and initiates a file download operation
62+
///
63+
/// - Parameters:
64+
/// - url: Source URL to download from
65+
/// - authToken: Bearer token for authentication with Hugging Face
66+
/// - resumeSize: Number of bytes already downloaded for resuming interrupted downloads
67+
/// - headers: Additional HTTP headers to include in the request
68+
/// - expectedSize: Expected file size in bytes for validation
69+
/// - timeout: Time interval before the request times out
70+
/// - numRetries: Number of retry attempts for failed downloads
6171
private func setupDownload(
6272
from url: URL,
6373
with authToken: String?,

Tests/HubTests/DownloaderTests.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
//
77

88
import XCTest
9-
@testable import Hub
109
import Combine
10+
@testable import Hub
11+
12+
/// Errors that can occur during the download process
1113
enum DownloadError: Error {
1214
case invalidDownloadLocation
1315
case unexpectedError
1416
}
17+
1518
final class DownloaderTests: XCTestCase {
1619
var tempDir: URL!
1720

@@ -26,6 +29,7 @@ final class DownloaderTests: XCTestCase {
2629
super.tearDown()
2730
}
2831

32+
/// This test downloads a known config file, verifies the download completes, checks the content matches expected value
2933
func testSuccessfulDownload() async throws {
3034
// Create a test file
3135
let url = URL(string: "https://huggingface.co/coreml-projects/Llama-2-7b-chat-coreml/resolve/main/config.json")!
@@ -75,6 +79,7 @@ final class DownloaderTests: XCTestCase {
7579
XCTAssertEqual(try String(contentsOf: destination, encoding: .utf8), fileContent)
7680
}
7781

82+
/// This test attempts to download with incorrect expected file, verifies the download fails, ensures no partial file is left behind
7883
func testDownloadFailsWithIncorrectSize() async throws {
7984
let url = URL(string: "https://huggingface.co/coreml-projects/Llama-2-7b-chat-coreml/resolve/main/config.json")!
8085
let destination = tempDir.appendingPathComponent("config.json")
@@ -97,6 +102,8 @@ final class DownloaderTests: XCTestCase {
97102
XCTAssertFalse(FileManager.default.fileExists(atPath: destination.path))
98103
}
99104

105+
/// This test downloads an LFS file, interrupts the download at 50% and 75% progress,
106+
/// verifies the download can resume and complete successfully, checks the final file exists and has content
100107
func testSuccessfulInterruptedDownload() async throws {
101108
let url = URL(string: "https://huggingface.co/coreml-projects/sam-2-studio/resolve/main/SAM%202%20Studio%201.1.zip")!
102109
let destination = tempDir.appendingPathComponent("SAM%202%20Studio%201.1.zip")
@@ -108,18 +115,23 @@ final class DownloaderTests: XCTestCase {
108115
let downloader = Downloader(
109116
from: url,
110117
to: destination,
111-
expectedSize: 73194001
118+
expectedSize: 73194001 // Correct size for verification
112119
)
120+
121+
// First interruption point at 50%
113122
var threshold = 0.5
114123

115124
var subscriber: AnyCancellable?
116125

117126
do {
127+
// Monitor download progress and interrupt at thresholds to test if
128+
// download continues from where it left off
118129
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
119130
subscriber = downloader.downloadState.sink { state in
120131
switch state {
121132
case .downloading(let progress):
122133
if threshold != 1.0 && progress >= threshold {
134+
// Move to next threshold and interrupt
123135
threshold = threshold == 0.5 ? 0.75 : 1.0
124136
downloader.cancel()
125137
}

0 commit comments

Comments
 (0)