From 07913319329ff89e03c83f40bc66c20b6e5edf4b Mon Sep 17 00:00:00 2001 From: John Viegas Date: Fri, 8 Sep 2023 09:35:14 -0700 Subject: [PATCH] Added Integ Test for CRT Progress listener updates --- ...3TransferManagerUploadIntegrationTest.java | 58 ++++++++++++++++++- .../progress/TransferProgressUpdater.java | 4 +- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/services-custom/s3-transfer-manager/src/it/java/software/amazon/awssdk/transfer/s3/S3TransferManagerUploadIntegrationTest.java b/services-custom/s3-transfer-manager/src/it/java/software/amazon/awssdk/transfer/s3/S3TransferManagerUploadIntegrationTest.java index a1211b2f9f0b..f87059596732 100644 --- a/services-custom/s3-transfer-manager/src/it/java/software/amazon/awssdk/transfer/s3/S3TransferManagerUploadIntegrationTest.java +++ b/services-custom/s3-transfer-manager/src/it/java/software/amazon/awssdk/transfer/s3/S3TransferManagerUploadIntegrationTest.java @@ -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; @@ -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; @@ -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; @@ -64,11 +68,13 @@ public static void teardown() throws IOException { @Test void upload_file_SentCorrectly() throws IOException { Map 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(); @@ -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(); @@ -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 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 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); } } diff --git a/services-custom/s3-transfer-manager/src/main/java/software/amazon/awssdk/transfer/s3/internal/progress/TransferProgressUpdater.java b/services-custom/s3-transfer-manager/src/main/java/software/amazon/awssdk/transfer/s3/internal/progress/TransferProgressUpdater.java index 8b5f7cce741d..c6c8bb056b10 100644 --- a/services-custom/s3-transfer-manager/src/main/java/software/amazon/awssdk/transfer/s3/internal/progress/TransferProgressUpdater.java +++ b/services-custom/s3-transfer-manager/src/main/java/software/amazon/awssdk/transfer/s3/internal/progress/TransferProgressUpdater.java @@ -107,7 +107,7 @@ public void publisherSubscribe(Subscriber subscri @Override public void subscriberOnNext(S3MetaRequestProgress s3MetaRequestProgress) { - incrementBytesTransferred(Math.toIntExact(s3MetaRequestProgress.getBytesTransferred())); + incrementBytesTransferred(s3MetaRequestProgress.getBytesTransferred()); } @Override @@ -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); });