Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rich push image downloading path conflict when its already downloaded #561

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Sources/Common/Service/HttpRequestRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ class UrlRequestHttpRequestRunner: HttpRequestRunner {
let directoryURL = fileType.directoryToSaveFiles(fileManager: FileManager.default)

session.downloadTask(with: url) { tempLocation, response, _ in
guard let tempLocation = tempLocation, let uniqueFileName = response?.suggestedFilename else {
guard let tempLocation = tempLocation, let suggestedFileName = response?.suggestedFilename else {
return onComplete(nil)
}

// create a unique file name so when trying to move temp file to destination it doesn't give an exception
let uniqueFileName = UUID().uuidString + "_" + suggestedFileName
let destinationURL = directoryURL
.appendingPathComponent(uniqueFileName)

Expand Down
50 changes: 49 additions & 1 deletion Tests/Shared/HttpTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ open class HttpTest: UnitTest {
public var runner: HttpRequestRunner?
public var userAgentUtil: UserAgentUtil!
public var session: URLSession?
public var publicSession: URLSession = CIOHttpClient.getBasicSession()

override open func setUp() {
super.setUp()

runner = UrlRequestHttpRequestRunner()
userAgentUtil = diGraph.userAgentUtil

/*
We don't want to run these tests on a CI server (flaky!) so, only populate the runner if
we see environment variables set in XCode.
*/
if let siteId = getEnvironmentVariable("SITE_ID"), let apiKey = getEnvironmentVariable("API_KEY") {
runner = UrlRequestHttpRequestRunner()
session = CIOHttpClient.getCIOApiSession(
siteId: siteId,
apiKey: apiKey,
Expand All @@ -40,6 +41,53 @@ open class HttpTest: UnitTest {
}
}

func testParallelDownloadFileCreatesUniquePaths() {
let expectation1 = expectation(description: "Parallel download file 1")
let expectation2 = expectation(description: "Parallel download file 2")

let url = URL(string: "https://thumbs.dreamstime.com/b/bee-flower-27533578.jpg")!
var path1: URL?
var path2: URL?

XCTAssertNotNil(runner)

// Initiate the first download
runner?.downloadFile(
url: url,
fileType: .richPushImage,
session: publicSession,
onComplete: { path in
XCTAssertNotNil(path)
path1 = path
expectation1.fulfill()
}
)

// Initiate the second download in parallel
runner?.downloadFile(
url: url,
fileType: .richPushImage,
session: publicSession,
onComplete: { path in
XCTAssertNotNil(path)
path2 = path
expectation2.fulfill()
}
)

// Wait for both downloads to complete
waitForExpectations(timeout: 20.0) { error in
if let error = error {
XCTFail("Test failed with error: \(error)")
}

// Verify that both paths are not nil and unique
XCTAssertNotNil(path1, "First path should not be nil")
XCTAssertNotNil(path2, "Second path should not be nil")
XCTAssertNotEqual(path1, path2, "Expected unique path for each parallel download")
}
}

override open func tearDown() {
runner = nil
// assert there isn't a memory leak and the runner can be deconstructed.
Expand Down