|
| 1 | +// |
| 2 | +// Copyright Amazon.com Inc. or its affiliates. |
| 3 | +// All Rights Reserved. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | +// |
| 7 | + |
| 8 | +import Combine |
| 9 | +import XCTest |
| 10 | +import Amplify |
| 11 | + |
| 12 | +@testable import AmplifyTestCommon |
| 13 | + |
| 14 | +/// Tests asserting that Storage Operations dispatch progress updates via various flavors of the API. |
| 15 | +/// |
| 16 | +/// These tests are useful to play around with the different thresholds for progress notifications. |
| 17 | +/// You will always receive a completion notification as long as the upload completes, even if you |
| 18 | +/// attach the result listener after the upload has completed. |
| 19 | +/// |
| 20 | +/// However, if you attach a **progress** listener to an operation that has already completed, |
| 21 | +/// you will not receive a value from that publisher. |
| 22 | +/// is no guarantee at what threshold you will receive more granular updates, since this is controlled |
| 23 | +/// by the OS. |
| 24 | +/// |
| 25 | +/// On my laptop, on my network, I only get "progress: 1.0" notifications for payloads |
| 26 | +/// of ~0-1MB. After ~1 MB, I start getting notified more frequently. |
| 27 | +class AWSS3StoragePluginProgressTests: AWSS3StoragePluginTestBase { |
| 28 | + |
| 29 | + func testUploadProgressViaListener() { |
| 30 | + let timestamp = String(Date().timeIntervalSince1970) |
| 31 | + let key = "testUploadProgressViaListener-\(timestamp)" |
| 32 | + |
| 33 | + addTeardownBlock { |
| 34 | + self.removeTestFile(withKey: key) |
| 35 | + } |
| 36 | + |
| 37 | + let resultReceived = expectation(description: "resultReceived") |
| 38 | + let progressReceived = expectation(description: "progressReceived") |
| 39 | + progressReceived.assertForOverFulfill = false |
| 40 | + _ = Amplify.Storage.uploadData( |
| 41 | + key: key, |
| 42 | + data: .testDataOfSize(.bytes(100)), |
| 43 | + progressListener: { progress in |
| 44 | + progressReceived.fulfill() |
| 45 | + print("Progress: \(progress.fractionCompleted)") |
| 46 | + }, resultListener: { result in |
| 47 | + resultReceived.fulfill() |
| 48 | + print("Result received: \(result)") |
| 49 | + } |
| 50 | + ) |
| 51 | + |
| 52 | + waitForExpectations(timeout: TestCommonConstants.networkTimeout) |
| 53 | + } |
| 54 | + |
| 55 | + func testUploadProgressViaPublisher() { |
| 56 | + var cancellables = Set<AnyCancellable>() |
| 57 | + |
| 58 | + let timestamp = String(Date().timeIntervalSince1970) |
| 59 | + let key = "testUploadProgressViaPublisher-\(timestamp)" |
| 60 | + |
| 61 | + addTeardownBlock { |
| 62 | + self.removeTestFile(withKey: key) |
| 63 | + } |
| 64 | + |
| 65 | + let completionReceived = expectation(description: "resultReceived") |
| 66 | + let progressReceived = expectation(description: "progressReceived") |
| 67 | + progressReceived.assertForOverFulfill = false |
| 68 | + let uploadOperation = Amplify.Storage.uploadData( |
| 69 | + key: key, |
| 70 | + data: .testDataOfSize(.bytes(100))) |
| 71 | + |
| 72 | + uploadOperation.resultPublisher |
| 73 | + .sink(receiveCompletion: { completion in |
| 74 | + completionReceived.fulfill() |
| 75 | + print("Completion received: \(completion)") |
| 76 | + }, receiveValue: { _ in }) |
| 77 | + .store(in: &cancellables) |
| 78 | + |
| 79 | + uploadOperation.progressPublisher |
| 80 | + .sink { progress in |
| 81 | + progressReceived.fulfill() |
| 82 | + print("Progress: \(progress.fractionCompleted)") |
| 83 | + } |
| 84 | + .store(in: &cancellables) |
| 85 | + |
| 86 | + waitForExpectations(timeout: TestCommonConstants.networkTimeout) |
| 87 | + } |
| 88 | + |
| 89 | + func testPublisherDeliveryAfterUploadCompletes() { |
| 90 | + var cancellables = Set<AnyCancellable>() |
| 91 | + |
| 92 | + let timestamp = String(Date().timeIntervalSince1970) |
| 93 | + let key = "testUploadProgressDeliveryAfterCompletion-\(timestamp)" |
| 94 | + |
| 95 | + addTeardownBlock { |
| 96 | + self.removeTestFile(withKey: key) |
| 97 | + } |
| 98 | + |
| 99 | + // Wait for the upload to complete |
| 100 | + let uploadComplete = expectation(description: "uploadComplete") |
| 101 | + let uploadOperation = Amplify.Storage.uploadData( |
| 102 | + key: key, |
| 103 | + data: .testDataOfSize(.bytes(100)), |
| 104 | + resultListener: { _ in uploadComplete.fulfill() } |
| 105 | + ) |
| 106 | + wait(for: [uploadComplete], timeout: TestCommonConstants.networkTimeout) |
| 107 | + |
| 108 | + // Result publisher should immediately complete, and should deliver a value |
| 109 | + let resultCompletionReceived = expectation(description: "resultCompletionReceived") |
| 110 | + let resultValueReceived = expectation(description: "resultValueReceived") |
| 111 | + uploadOperation.resultPublisher |
| 112 | + .sink( |
| 113 | + receiveCompletion: { _ in resultCompletionReceived.fulfill() }, |
| 114 | + receiveValue: { _ in resultValueReceived.fulfill() } |
| 115 | + ) |
| 116 | + .store(in: &cancellables) |
| 117 | + waitForExpectations(timeout: 0.5) |
| 118 | + |
| 119 | + // Progress listener should immediately complete without delivering a value |
| 120 | + let progressValueReceived = expectation(description: "progressValueReceived") |
| 121 | + progressValueReceived.isInverted = true |
| 122 | + let progressCompletionReceived = expectation(description: "progressCompletionReceived") |
| 123 | + uploadOperation.progressPublisher |
| 124 | + .sink( |
| 125 | + receiveCompletion: { _ in progressCompletionReceived.fulfill() }, |
| 126 | + receiveValue: { _ in progressValueReceived.fulfill() } |
| 127 | + ) |
| 128 | + .store(in: &cancellables) |
| 129 | + waitForExpectations(timeout: 0.5) |
| 130 | + } |
| 131 | + |
| 132 | + // MARK: - Utilities |
| 133 | + |
| 134 | + private func removeTestFile(withKey key: String) { |
| 135 | + // Never do this in prod |
| 136 | + let semaphore = DispatchSemaphore(value: 0) |
| 137 | + _ = Amplify.Storage.remove(key: key) { _ in semaphore.signal() } |
| 138 | + semaphore.wait() |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +private extension Data { |
| 143 | + static func testDataOfSize(_ size: Int) -> Data { |
| 144 | + Data(repeating: 0xff, count: size) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +private extension Int { |
| 149 | + static func bytes(_ size: Int) -> Int { return size } |
| 150 | + |
| 151 | + static func kilobytes(_ size: Int) -> Int { return size * 1_024 } |
| 152 | + static func kilobytes(_ size: Float) -> Int { return Int(size * 1_024) } |
| 153 | + |
| 154 | + static func megabytes(_ size: Int) -> Int { return size * 1_024 * 1_024 } |
| 155 | + static func megabytes(_ size: Float) -> Int { return Int(size * 1_024 * 1_024) } |
| 156 | + |
| 157 | + static func gigabytes(_ size: Int) -> Int { return size * 1_024 * 1_024 * 1_024 } |
| 158 | + static func gigabytes(_ size: Float) -> Int { return Int(size * 1_024 * 1_024 * 1_024) } |
| 159 | +} |
0 commit comments