Search in sources :

Example 1 with SubscriptionToken

use of com.amplifyframework.hub.SubscriptionToken in project amplify-android by aws-amplify.

the class AWSS3StorageDownloadTest method testDownloadFileIsResumable.

/**
 * Tests that file download operation can be paused and resumed
 * while the transfer hasn't completed yet.
 *
 * @throws Exception if download is not paused, resumed, and
 *                   completed successfully before timeout
 */
@Ignore("Contains tests that hang, or hang the suite overall.")
@SuppressWarnings("unchecked")
public void testDownloadFileIsResumable() throws Exception {
    final CountDownLatch completed = new CountDownLatch(1);
    final CountDownLatch resumed = new CountDownLatch(1);
    final AtomicReference<Resumable> opContainer = new AtomicReference<>();
    final AtomicReference<Throwable> errorContainer = new AtomicReference<>();
    // Listen to Hub events to resume when operation has been paused
    SubscriptionToken resumeToken = Amplify.Hub.subscribe(HubChannel.STORAGE, hubEvent -> {
        if (StorageChannelEventName.DOWNLOAD_STATE.toString().equals(hubEvent.getName())) {
            HubEvent<String> stateEvent = (HubEvent<String>) hubEvent;
            TransferState state = TransferState.getState(stateEvent.getData());
            if (TransferState.PAUSED.equals(state)) {
                opContainer.get().resume();
                resumed.countDown();
            }
        }
    });
    subscriptions.add(resumeToken);
    // Begin downloading a large file
    StorageDownloadFileOperation<?> op = storageCategory.downloadFile(LARGE_FILE_NAME, downloadFile, options, progress -> {
        if (progress.getCurrentBytes() > 0 && resumed.getCount() > 0) {
            opContainer.get().pause();
        }
    }, result -> completed.countDown(), errorContainer::set);
    opContainer.set(op);
    // Assert that all the required conditions have been met
    assertTrue(resumed.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertTrue(completed.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertNull(errorContainer.get());
    FileAssert.assertEquals(largeFile, downloadFile);
}
Also used : HubEvent(com.amplifyframework.hub.HubEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TransferState(com.amazonaws.mobileconnectors.s3.transferutility.TransferState) Resumable(com.amplifyframework.core.async.Resumable) SubscriptionToken(com.amplifyframework.hub.SubscriptionToken) Ignore(org.junit.Ignore)

Example 2 with SubscriptionToken

use of com.amplifyframework.hub.SubscriptionToken in project amplify-android by aws-amplify.

the class AWSS3StorageDownloadTest method testDownloadFileIsCancelable.

/**
 * Tests that file download operation can be canceled while the
 * transfer hasn't completed yet.
 *
 * @throws Exception if download is not canceled successfully
 *                   before timeout
 */
@Ignore("Contains tests that hang, or hang the suite overall.")
@SuppressWarnings("unchecked")
public void testDownloadFileIsCancelable() throws Exception {
    final CountDownLatch canceled = new CountDownLatch(1);
    final AtomicReference<Cancelable> opContainer = new AtomicReference<>();
    final AtomicReference<Throwable> errorContainer = new AtomicReference<>();
    // Listen to Hub events for cancel
    SubscriptionToken cancelToken = Amplify.Hub.subscribe(HubChannel.STORAGE, hubEvent -> {
        if (StorageChannelEventName.DOWNLOAD_STATE.toString().equals(hubEvent.getName())) {
            HubEvent<String> stateEvent = (HubEvent<String>) hubEvent;
            TransferState state = TransferState.getState(stateEvent.getData());
            if (TransferState.CANCELED.equals(state)) {
                canceled.countDown();
            }
        }
    });
    subscriptions.add(cancelToken);
    // Begin downloading a large file
    StorageDownloadFileOperation<?> op = storageCategory.downloadFile(LARGE_FILE_NAME, downloadFile, options, progress -> {
        if (progress.getCurrentBytes() > 0 && canceled.getCount() > 0) {
            opContainer.get().cancel();
        }
    }, result -> errorContainer.set(new RuntimeException("Download completed without canceling.")), errorContainer::set);
    opContainer.set(op);
    // Assert that the required conditions have been met
    assertTrue(canceled.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertNull(errorContainer.get());
}
Also used : HubEvent(com.amplifyframework.hub.HubEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TransferState(com.amazonaws.mobileconnectors.s3.transferutility.TransferState) SubscriptionToken(com.amplifyframework.hub.SubscriptionToken) Cancelable(com.amplifyframework.core.async.Cancelable) Ignore(org.junit.Ignore)

Example 3 with SubscriptionToken

use of com.amplifyframework.hub.SubscriptionToken in project amplify-android by aws-amplify.

the class AWSS3StorageUploadTest method testUploadFileIsResumable.

/**
 * Tests that file upload operation can be paused and resumed
 * while the transfer hasn't completed yet.
 *
 * @throws Exception if upload is not paused, resumed, and
 *         completed successfully before timeout
 */
@SuppressWarnings("unchecked")
@Ignore("Contains test which either hang themselves, or hang the suite overall.")
public void testUploadFileIsResumable() throws Exception {
    final CountDownLatch completed = new CountDownLatch(1);
    final CountDownLatch resumed = new CountDownLatch(1);
    final AtomicReference<Resumable> opContainer = new AtomicReference<>();
    final AtomicReference<Throwable> errorContainer = new AtomicReference<>();
    // Create a file large enough that transfer won't finish before being paused
    File uploadFile = new RandomTempFile(LARGE_FILE_SIZE);
    // Listen to Hub events to resume when operation has been paused
    SubscriptionToken resumeToken = Amplify.Hub.subscribe(HubChannel.STORAGE, hubEvent -> {
        if (StorageChannelEventName.UPLOAD_STATE.toString().equals(hubEvent.getName())) {
            HubEvent<String> stateEvent = (HubEvent<String>) hubEvent;
            TransferState state = TransferState.getState(stateEvent.getData());
            if (TransferState.PAUSED.equals(state)) {
                opContainer.get().resume();
                resumed.countDown();
            }
        }
    });
    subscriptions.add(resumeToken);
    // Begin uploading a large file
    StorageUploadFileOperation<?> op = storageCategory.uploadFile(uploadFile.getName(), uploadFile, options, progress -> {
        if (progress.getCurrentBytes() > 0 && resumed.getCount() > 0) {
            opContainer.get().pause();
        }
    }, result -> completed.countDown(), errorContainer::set);
    opContainer.set(op);
    // Assert that all the required conditions have been met
    assertTrue(resumed.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertTrue(completed.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertNull(errorContainer.get());
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) HubEvent(com.amplifyframework.hub.HubEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TransferState(com.amazonaws.mobileconnectors.s3.transferutility.TransferState) Resumable(com.amplifyframework.core.async.Resumable) SubscriptionToken(com.amplifyframework.hub.SubscriptionToken) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Ignore(org.junit.Ignore)

Example 4 with SubscriptionToken

use of com.amplifyframework.hub.SubscriptionToken in project amplify-android by aws-amplify.

the class AWSS3StorageUploadTest method testUploadFileIsCancelable.

/**
 * Tests that file upload operation can be canceled while the
 * transfer hasn't completed yet.
 *
 * @throws Exception if upload is not canceled successfully
 *         before timeout
 */
@SuppressWarnings("unchecked")
@Ignore("Contains test which either hang themselves, or hang the suite overall.")
public void testUploadFileIsCancelable() throws Exception {
    final CountDownLatch canceled = new CountDownLatch(1);
    final AtomicReference<Cancelable> opContainer = new AtomicReference<>();
    final AtomicReference<Throwable> errorContainer = new AtomicReference<>();
    // Create a file large enough that transfer won't finish before being canceled
    File uploadFile = new RandomTempFile(LARGE_FILE_SIZE);
    // Listen to Hub events for cancel
    SubscriptionToken cancelToken = Amplify.Hub.subscribe(HubChannel.STORAGE, hubEvent -> {
        if (StorageChannelEventName.UPLOAD_STATE.toString().equals(hubEvent.getName())) {
            HubEvent<String> stateEvent = (HubEvent<String>) hubEvent;
            TransferState state = TransferState.getState(stateEvent.getData());
            if (TransferState.CANCELED.equals(state)) {
                canceled.countDown();
            }
        }
    });
    subscriptions.add(cancelToken);
    // Begin uploading a large file
    StorageUploadFileOperation<?> op = storageCategory.uploadFile(uploadFile.getName(), uploadFile, options, progress -> {
        if (progress.getCurrentBytes() > 0) {
            opContainer.get().cancel();
        }
    }, result -> errorContainer.set(new RuntimeException("Upload completed without canceling.")), errorContainer::set);
    opContainer.set(op);
    // Assert that the required conditions have been met
    assertTrue(canceled.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertNull(errorContainer.get());
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) HubEvent(com.amplifyframework.hub.HubEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TransferState(com.amazonaws.mobileconnectors.s3.transferutility.TransferState) SubscriptionToken(com.amplifyframework.hub.SubscriptionToken) Cancelable(com.amplifyframework.core.async.Cancelable) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Ignore(org.junit.Ignore)

Example 5 with SubscriptionToken

use of com.amplifyframework.hub.SubscriptionToken in project amplify-android by aws-amplify.

the class RxHubBinding method on.

@NonNull
@Override
public Observable<HubEvent<?>> on(@NonNull HubChannel hubChannel) {
    return Observable.create(emitter -> {
        SubscriptionToken token = hub.subscribe(hubChannel, emitter::onNext);
        emitter.setDisposable(Disposable.fromAction(() -> hub.unsubscribe(token)));
    });
}
Also used : SubscriptionToken(com.amplifyframework.hub.SubscriptionToken) NonNull(androidx.annotation.NonNull)

Aggregations

SubscriptionToken (com.amplifyframework.hub.SubscriptionToken)5 TransferState (com.amazonaws.mobileconnectors.s3.transferutility.TransferState)4 HubEvent (com.amplifyframework.hub.HubEvent)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Ignore (org.junit.Ignore)4 Cancelable (com.amplifyframework.core.async.Cancelable)2 Resumable (com.amplifyframework.core.async.Resumable)2 RandomTempFile (com.amplifyframework.testutils.random.RandomTempFile)2 File (java.io.File)2 NonNull (androidx.annotation.NonNull)1