Skip to content

Commit

Permalink
fix(storage): use fulfillment
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian committed Oct 12, 2023
1 parent f52f08c commit 0143d2c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
/// Then: The operation completes successfully with the data retrieved
func testDownloadDataToMemory() async throws {
let key = UUID().uuidString
await uploadData(key: key, data: key.data(using: .utf8)!)
try await uploadData(key: key, data: key.data(using: .utf8)!)
_ = try await Amplify.Storage.downloadData(key: key, options: .init()).value
_ = try await Amplify.Storage.remove(key: key)
}
Expand All @@ -185,7 +185,7 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
let key = UUID().uuidString
let timestamp = String(Date().timeIntervalSince1970)
let timestampData = timestamp.data(using: .utf8)!
await uploadData(key: key, data: timestampData)
try await uploadData(key: key, data: timestampData)
let filePath = NSTemporaryDirectory() + key + ".tmp"
let fileURL = URL(fileURLWithPath: filePath)
removeIfExists(fileURL)
Expand All @@ -209,7 +209,7 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
/// Then: The operation completes successfully with the URL retrieved
func testGetRemoteURL() async throws {
let key = UUID().uuidString
await uploadData(key: key, dataString: key)
try await uploadData(key: key, dataString: key)

let remoteURL = try await Amplify.Storage.getURL(key: key)

Expand Down Expand Up @@ -274,7 +274,7 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
func testListFromPublic() async throws {
let key = UUID().uuidString
let expectedMD5Hex = "\"\(key.md5())\""
await uploadData(key: key, dataString: key)
try await uploadData(key: key, dataString: key)
let options = StorageListRequest.Options(accessLevel: .guest,
targetIdentityId: nil,
path: key)
Expand Down Expand Up @@ -311,7 +311,7 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
for i in 0..<objectCount {
let key = "\(path)/\(i).txt"
let data = Data("\(i)".utf8)
await uploadData(key: key, data: data)
try await uploadData(key: key, data: data)
uploadedKeys.append(key)
}

Expand Down Expand Up @@ -382,7 +382,7 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
for fileIndex in 1 ... 10 {
let key = folder + "file" + String(fileIndex) + ".txt"
keys.append(key)
await uploadData(key: key, dataString: key)
try await uploadData(key: key, dataString: key)
}
let options = StorageListRequest.Options(accessLevel: .guest,
targetIdentityId: nil,
Expand Down Expand Up @@ -417,7 +417,7 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
for fileIndex in 1 ... 10 {
let key = folder + "file" + String(fileIndex) + ".txt"
keys.append(key)
await uploadData(key: key, dataString: key)
try await uploadData(key: key, dataString: key)
}

let options = StorageListRequest.Options(accessLevel: .guest,
Expand Down Expand Up @@ -451,7 +451,7 @@ class AWSS3StoragePluginBasicIntegrationTests: AWSS3StoragePluginTestBase {
/// Then: The operation completes successfully with the key removed from storage
func testRemoveKey() async throws {
let key = UUID().uuidString
await uploadData(key: key, dataString: key)
try await uploadData(key: key, dataString: key)

let result = try await Amplify.Storage.remove(key: key, options: nil)
XCTAssertEqual(result, key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@ class AWSS3StoragePluginNegativeTests: AWSS3StoragePluginTestBase {
let key = UUID().uuidString
let expectedKey = "public/" + key
let failInvoked = expectation(description: "Failed is invoked")
let getError = await waitError(with: failInvoked) {
return try await Amplify.Storage.downloadData(key: key, options: .init()).value
}

guard let getError = getError else {
do {
_ = try await Amplify.Storage.downloadData(key: key, options: .init()).value
XCTFail("Expected error from Download operation")
return
}

guard let storageError = getError as? StorageError,
case let .keyNotFound(key, _, _, _) = storageError else {
XCTFail("Expected keyNotFound error, got \(getError)")
return
} catch StorageError.keyNotFound(let key, _, _, _) {
XCTAssertEqual(key, expectedKey)
failInvoked.fulfill()
} catch {
XCTFail("Expected StorageError.keyNotFound error, got \(error)")
}

XCTAssertEqual(key, expectedKey)
await fulfillment(of: [failInvoked], timeout: 600)
}

/// Given: Object does not exist in storage
Expand All @@ -45,25 +40,23 @@ class AWSS3StoragePluginNegativeTests: AWSS3StoragePluginTestBase {
let filePath = NSTemporaryDirectory() + key + ".tmp"
let fileURL = URL(fileURLWithPath: filePath)
let failInvoked = expectation(description: "Failed is invoked")
let getError = await waitError(with: failInvoked) {
return try await Amplify.Storage.downloadFile(key: key, local: fileURL, options: nil).value
}

guard let getError = getError else {
XCTFail("Expected error from Download operation")
return
do {
_ = try await Amplify.Storage.downloadFile(key: key, local: fileURL, options: nil).value
} catch StorageError.keyNotFound(let key, _, _, _) {
XCTAssertEqual(key, expectedKey)
failInvoked.fulfill()
} catch {
XCTFail("Expected StorageError.keyNotFound error, got \(error)")
}

if FileManager.default.fileExists(atPath: fileURL.path) {
XCTFail("local file should not exist")
}
XCTAssertFalse(
FileManager.default.fileExists(atPath: fileURL.path),
"local file should not exist"
)

await fulfillment(of: [failInvoked], timeout: 600)

guard let storageError = getError as? StorageError,
case let .keyNotFound(key, _, _, _) = storageError else {
XCTFail("Expected keyNotFound error, got \(getError)")
return
}
XCTAssertEqual(key, expectedKey)
}

/// Given: A path to file that does not exist
Expand All @@ -73,23 +66,22 @@ class AWSS3StoragePluginNegativeTests: AWSS3StoragePluginTestBase {
let key = UUID().uuidString
let filePath = NSTemporaryDirectory() + key + ".tmp"
let fileURL = URL(fileURLWithPath: filePath)
let failedInvoked = expectation(description: "Failed is invoked")
let uploadError = await waitError(with: failedInvoked) {
return try await Amplify.Storage.uploadFile(key: key, local: fileURL, options: nil).value
}
let failInvoked = expectation(description: "Failed is invoked")

guard let uploadError = uploadError else {
XCTFail("Expected error from Download operation")
return
do {
_ = try await Amplify.Storage.uploadFile(key: key, local: fileURL, options: nil).value
} catch StorageError.localFileNotFound(let description, _, _) {
XCTAssertEqual(
description,
StorageErrorConstants.localFileNotFound.errorDescription
)
failInvoked.fulfill()
} catch {
XCTFail("Expected localFileNotFound error, got \(error)")
}

guard let storageError = uploadError as? StorageError,
case let .localFileNotFound(description, _, _) = storageError else {
XCTFail("Expected localFileNotFound error, got \(uploadError)")
return
}
await fulfillment(of: [failInvoked], timeout: 600)

XCTAssertEqual(description, StorageErrorConstants.localFileNotFound.errorDescription)
}

/// Given: An unreadable file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AWSS3StoragePluginOptionsUsabilityTests: AWSS3StoragePluginTestBase {
/// Then: Retrieve data successfully when the URL has not expired and fail to after the expiry time
func testGetRemoteURLWithExpires() async throws {
let key = UUID().uuidString
await uploadData(key: key, dataString: key)
try await uploadData(key: key, dataString: key)

#if os(iOS)
let expires = 20
Expand Down Expand Up @@ -67,7 +67,7 @@ class AWSS3StoragePluginOptionsUsabilityTests: AWSS3StoragePluginTestBase {
XCTAssertEqual(dataString, key)
}
task.resume()
await waitForExpectations(timeout: TestCommonConstants.networkTimeout)
await fulfillment(of: [dataTaskCompleteInvoked], timeout: TestCommonConstants.networkTimeout)

try await Task.sleep(seconds: 30)
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class AWSS3StoragePluginProgressTests: AWSS3StoragePluginTestBase {
}
.store(in: &cancellables)

await waitForExpectations(timeout: TestCommonConstants.networkTimeout)
await fulfillment(
of: [completionReceived, progressReceived],
timeout: TestCommonConstants.networkTimeout
)
// Remove the key
await remove(key: key)
}
Expand Down Expand Up @@ -84,7 +87,11 @@ class AWSS3StoragePluginProgressTests: AWSS3StoragePluginTestBase {
receiveValue: { _ in resultValueReceived.fulfill() }
)
.store(in: &cancellables)
await waitForExpectations(timeout: 0.5)

await fulfillment(
of: [resultCompletionReceived, resultValueReceived],
timeout: 0.5
)

// Progress listener should immediately complete without delivering a value
let progressValueReceived = expectation(description: "progressValueReceived")
Expand All @@ -96,7 +103,11 @@ class AWSS3StoragePluginProgressTests: AWSS3StoragePluginTestBase {
receiveValue: { _ in progressValueReceived.fulfill() }
)
.store(in: &cancellables)
await waitForExpectations(timeout: 0.5)

await fulfillment(
of: [progressValueReceived, progressCompletionReceived],
timeout: 0.5
)
// Remove the key
await remove(key: key)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,27 @@ final class StorageStressTests: XCTestCase {
/// When: Upload the data simultaneously from 10 tasks
/// Then: The operation completes successfully
func testUploadMultipleSmallDataObjects() async {
let uploadExpectation = expectation(description: "Small data object uploaded successfully",
expectedFulfillmentCount: concurrencyLimit)
let removeExpectation = expectation(description: "Data object removed successfully",
expectedFulfillmentCount: concurrencyLimit)
let uploadExpectation = expectation(description: "Small data object uploaded successfully")
uploadExpectation.expectedFulfillmentCount = concurrencyLimit

let removeExpectation = expectation(description: "Data object removed successfully")
removeExpectation.expectedFulfillmentCount = concurrencyLimit

for _ in 1...concurrencyLimit {
Task {
do {
let key = UUID().uuidString
let uploadKey = try await Amplify.Storage.uploadData(key: key,
data: smallDataObjectForStressTest,
options: nil).value
let uploadKey = try await Amplify.Storage.uploadData(
key: key,
data: smallDataObjectForStressTest,
options: nil
).value

XCTAssertEqual(uploadKey, key)
await uploadExpectation.fulfill()
uploadExpectation.fulfill()

try await Amplify.Storage.remove(key: key)
await removeExpectation.fulfill()
removeExpectation.fulfill()
} catch {
XCTFail("Error: \(error)")
}
Expand All @@ -92,14 +97,17 @@ final class StorageStressTests: XCTestCase {
let removeExpectation = expectation(description: "Data object removed successfully")
do {
let key = UUID().uuidString
let uploadKey = try await Amplify.Storage.uploadData(key: key,
data: largeDataObjectForStressTest,
options: nil).value
let uploadKey = try await Amplify.Storage.uploadData(
key: key,
data: largeDataObjectForStressTest,
options: nil
).value

XCTAssertEqual(uploadKey, key)
await uploadExpectation.fulfill()
uploadExpectation.fulfill()

try await Amplify.Storage.remove(key: key)
await removeExpectation.fulfill()
removeExpectation.fulfill()
} catch {
XCTFail("Error: \(error)")
}
Expand All @@ -110,26 +118,29 @@ final class StorageStressTests: XCTestCase {
/// When: Object is downloaded simultaneously from 10 tasks
/// Then: The operation completes successfully with the data retrieved
func testDownloadMultipleSmallDataObjects() async {
let downloadExpectation = expectation(description: "Data object downloaded successfully",
expectedFulfillmentCount: concurrencyLimit)
let uploadExpectation = expectation(description: "Data object uploaded successfully",
expectedFulfillmentCount: concurrencyLimit)
let removeExpectation = expectation(description: "Data object removed successfully",
expectedFulfillmentCount: concurrencyLimit)
let downloadExpectation = expectation(description: "Data object downloaded successfully")
downloadExpectation.expectedFulfillmentCount = concurrencyLimit

let uploadExpectation = expectation(description: "Data object uploaded successfully")
uploadExpectation.expectedFulfillmentCount = concurrencyLimit

let removeExpectation = expectation(description: "Data object removed successfully")
removeExpectation.expectedFulfillmentCount = concurrencyLimit

for _ in 1...concurrencyLimit {
Task {
let key = UUID().uuidString
let uploadKey = try await Amplify.Storage.uploadData(key: key,
data: smallDataObjectForStressTest,
options: nil).value
XCTAssertEqual(uploadKey, key)
await uploadExpectation.fulfill()
uploadExpectation.fulfill()

let _ = try await Amplify.Storage.downloadData(key: key, options: .init()).value
await downloadExpectation.fulfill()
_ = try await Amplify.Storage.downloadData(key: key, options: .init()).value
downloadExpectation.fulfill()

try await Amplify.Storage.remove(key: key)
await removeExpectation.fulfill()
removeExpectation.fulfill()
}
}

Expand All @@ -145,17 +156,20 @@ final class StorageStressTests: XCTestCase {
let removeExpectation = expectation(description: "Data object removed successfully")
do {
let key = UUID().uuidString
let uploadKey = try await Amplify.Storage.uploadData(key: key,
data: largeDataObjectForStressTest,
options: nil).value
let uploadKey = try await Amplify.Storage.uploadData(
key: key,
data: largeDataObjectForStressTest,
options: nil
).value

XCTAssertEqual(uploadKey, key)
await uploadExpectation.fulfill()
uploadExpectation.fulfill()

let _ = try await Amplify.Storage.downloadData(key: key, options: .init()).value
await downloadExpectation.fulfill()
downloadExpectation.fulfill()

try await Amplify.Storage.remove(key: key)
await removeExpectation.fulfill()
removeExpectation.fulfill()
} catch {
XCTFail("Error: \(error)")
}
Expand All @@ -177,10 +191,10 @@ final class StorageStressTests: XCTestCase {
password: AWSS3StoragePluginTestBase.password,
email: AWSS3StoragePluginTestBase.email1)
Self.isFirstUserSignedUp = true
await registerFirstUserComplete.fulfill()
registerFirstUserComplete.fulfill()
} catch {
XCTFail("Failed to Sign up user: \(error)")
await registerFirstUserComplete.fulfill()
registerFirstUserComplete.fulfill()
}
}

Expand All @@ -191,10 +205,10 @@ final class StorageStressTests: XCTestCase {
password: AWSS3StoragePluginTestBase.password,
email: AWSS3StoragePluginTestBase.email2)
Self.isSecondUserSignedUp = true
await registerSecondUserComplete.fulfill()
registerSecondUserComplete.fulfill()
} catch {
XCTFail("Failed to Sign up user: \(error)")
await registerSecondUserComplete.fulfill()
registerSecondUserComplete.fulfill()
}
}

Expand Down

0 comments on commit 0143d2c

Please sign in to comment.