Skip to content

Commit

Permalink
Added Integ Test for CRT Progress listener updates
Browse files Browse the repository at this point in the history
  • Loading branch information
joviegas committed Sep 8, 2023
1 parent 6af3397 commit 0791331
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.transfer.s3;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName;

import java.io.IOException;
Expand All @@ -24,6 +25,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletionException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -32,6 +35,7 @@
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import software.amazon.awssdk.testutils.RandomTempFile;
import software.amazon.awssdk.transfer.s3.model.CompletedFileUpload;
import software.amazon.awssdk.transfer.s3.model.CompletedUpload;
Expand Down Expand Up @@ -64,11 +68,13 @@ public static void teardown() throws IOException {
@Test
void upload_file_SentCorrectly() throws IOException {
Map<String, String> metadata = new HashMap<>();
metadata.put("x-amz-meta-foobar", "FOO BAR");
CaptureTransferListener transferListener = new CaptureTransferListener();
metadata.put("x-amz-meta-foobar", "FOO BAR");
FileUpload fileUpload =
tm.uploadFile(u -> u.putObjectRequest(p -> p.bucket(TEST_BUCKET).key(TEST_KEY).metadata(metadata).checksumAlgorithm(ChecksumAlgorithm.CRC32))
.source(testFile.toPath())
.addTransferListener(LoggingTransferListener.create())
.addTransferListener(transferListener)
.build());

CompletedFileUpload completedFileUpload = fileUpload.completionFuture().join();
Expand All @@ -83,17 +89,29 @@ void upload_file_SentCorrectly() throws IOException {
assertThat(obj.response().responseMetadata().requestId()).isNotNull();
assertThat(obj.response().metadata()).containsEntry("foobar", "FOO BAR");
assertThat(fileUpload.progress().snapshot().sdkResponse()).isPresent();
assertListenerForSuccessfulTransferComplete(transferListener);
}

private static void assertListenerForSuccessfulTransferComplete(CaptureTransferListener transferListener) {
assertThat(transferListener.isTransferInitiated()).isTrue();
assertThat(transferListener.isTransferInitiated()).isTrue();
assertThat(transferListener.getRatioTransferredList()).isNotEmpty();
assertThat(transferListener.getRatioTransferredList().contains(0.0));
assertThat(transferListener.getRatioTransferredList().contains(100.0));
assertThat(transferListener.getExceptionCaught()).isNull();
}

@Test
void upload_asyncRequestBody_SentCorrectly() throws IOException {
String content = UUID.randomUUID().toString();
CaptureTransferListener transferListener = new CaptureTransferListener();

Upload upload =
tm.upload(UploadRequest.builder()
.putObjectRequest(b -> b.bucket(TEST_BUCKET).key(TEST_KEY))
.requestBody(AsyncRequestBody.fromString(content))
.addTransferListener(LoggingTransferListener.create())
.addTransferListener(transferListener)
.build());

CompletedUpload completedUpload = upload.completionFuture().join();
Expand All @@ -107,5 +125,43 @@ void upload_asyncRequestBody_SentCorrectly() throws IOException {
.isEqualTo(ChecksumUtils.computeCheckSum(obj));
assertThat(obj.response().responseMetadata().requestId()).isNotNull();
assertThat(upload.progress().snapshot().sdkResponse()).isPresent();
assertListenerForSuccessfulTransferComplete(transferListener);

}

@Test
void upload_file_Interupted_CancelsTheListener() throws IOException, InterruptedException {
Map<String, String> metadata = new HashMap<>();
CaptureTransferListener transferListener = new CaptureTransferListener();
metadata.put("x-amz-meta-foobar", "FOO BAR");
FileUpload fileUpload =
tm.uploadFile(u -> u.putObjectRequest(p -> p.bucket(TEST_BUCKET).key(TEST_KEY).metadata(metadata).checksumAlgorithm(ChecksumAlgorithm.CRC32))
.source(testFile.toPath())
.addTransferListener(LoggingTransferListener.create())
.addTransferListener(transferListener)
.build());

fileUpload.completionFuture().cancel(true);
assertThat(transferListener.isTransferInitiated()).isTrue();
assertThat(transferListener.isTransferComplete()).isFalse();
assertThat(transferListener.getExceptionCaught()).isInstanceOf(CancellationException.class);
assertThat(transferListener.getRatioTransferredList().get(transferListener.getRatioTransferredList().size() - 1))
.isNotEqualTo(100.0);
}

@Test
void upload_file_errorPropagatedToListener() throws IOException, InterruptedException {
Map<String, String> metadata = new HashMap<>();
CaptureTransferListener transferListener = new CaptureTransferListener();
metadata.put("x-amz-meta-foobar", "FOO BAR");
FileUpload fileUpload =
tm.uploadFile(u -> u.putObjectRequest(p -> p.bucket(TEST_BUCKET+TEST_BUCKET).key(TEST_KEY).metadata(metadata).checksumAlgorithm(ChecksumAlgorithm.CRC32))
.source(testFile.toPath())
.addTransferListener(LoggingTransferListener.create())
.addTransferListener(transferListener)
.build());
assertThatExceptionOfType(CompletionException.class).isThrownBy(
() -> fileUpload.completionFuture().join());
assertThat(transferListener.getExceptionCaught()).isInstanceOf(NoSuchBucketException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void publisherSubscribe(Subscriber<? super S3MetaRequestProgress> subscri

@Override
public void subscriberOnNext(S3MetaRequestProgress s3MetaRequestProgress) {
incrementBytesTransferred(Math.toIntExact(s3MetaRequestProgress.getBytesTransferred()));
incrementBytesTransferred(s3MetaRequestProgress.getBytesTransferred());
}

@Override
Expand Down Expand Up @@ -165,7 +165,7 @@ private void resetBytesTransferred() {
progress.updateAndGet(b -> b.transferredBytes(0L));
}

private void incrementBytesTransferred(int numBytes) {
private void incrementBytesTransferred(long numBytes) {
TransferProgressSnapshot snapshot = progress.updateAndGet(b -> {
b.transferredBytes(b.getTransferredBytes() + numBytes);
});
Expand Down

0 comments on commit 0791331

Please sign in to comment.