|
9 | 9 | import com.azure.core.test.utils.TestUtils; |
10 | 10 | import com.azure.core.util.BinaryData; |
11 | 11 | import com.azure.core.util.Context; |
| 12 | +import com.azure.core.util.ProgressListener; |
12 | 13 | import com.azure.storage.blob.models.BlobDownloadContentResponse; |
13 | 14 | import com.azure.storage.blob.models.BlobDownloadResponse; |
14 | 15 | import com.azure.storage.blob.models.BlobProperties; |
|
42 | 43 | import java.nio.file.Path; |
43 | 44 | import java.util.List; |
44 | 45 | import java.util.concurrent.CopyOnWriteArrayList; |
| 46 | +import java.util.concurrent.atomic.AtomicLong; |
45 | 47 | import java.util.stream.Stream; |
46 | 48 |
|
47 | 49 | import static com.azure.storage.blob.specialized.BlobSeekableByteChannelTests.copy; |
@@ -419,6 +421,58 @@ public void openSeekableByteChannelReadContentValidation(Integer streamBufferSiz |
419 | 421 | assertTrue(hasStructuredMessageDownloadRequestHeaders(recordedRequestHeaders, false)); |
420 | 422 | } |
421 | 423 |
|
| 424 | + @Test |
| 425 | + public void verifyProgressListenerIsCompatibleWithContentValidation(@TempDir Path tempDir) throws IOException { |
| 426 | + byte[] data = getRandomByteArray(10 * Constants.MB); |
| 427 | + |
| 428 | + BlobClient client = cc.getBlobClient(generateBlobName()); |
| 429 | + client.upload(BinaryData.fromBytes(data)); |
| 430 | + |
| 431 | + MockProgressListener mockListenerWithContentVal = new MockProgressListener(); |
| 432 | + MockProgressListener mockListenerWithoutContentVal = new MockProgressListener(); |
| 433 | + |
| 434 | + ParallelTransferOptions parallelOptionsWithContentVal |
| 435 | + = new ParallelTransferOptions().setProgressListener(mockListenerWithContentVal); |
| 436 | + ParallelTransferOptions parallelOptionsWithoutContentVal |
| 437 | + = new ParallelTransferOptions().setProgressListener(mockListenerWithoutContentVal); |
| 438 | + |
| 439 | + File fileWithContentVal = createRandomFile(tempDir, 10 * Constants.MB); |
| 440 | + File outFileWithContentVal = tempDir.resolve("withcontentval.bin").toFile(); |
| 441 | + File fileWithoutContentVal = createRandomFile(tempDir, 10 * Constants.MB); |
| 442 | + File outFileWithoutContentVal = tempDir.resolve("withoutcontentval.bin").toFile(); |
| 443 | + |
| 444 | + Files.deleteIfExists(outFileWithContentVal.toPath()); |
| 445 | + Files.deleteIfExists(outFileWithoutContentVal.toPath()); |
| 446 | + |
| 447 | + BlobDownloadToFileOptions optionsWithContentVal |
| 448 | + = new BlobDownloadToFileOptions(outFileWithContentVal.getAbsolutePath()) |
| 449 | + .setParallelTransferOptions(parallelOptionsWithContentVal) |
| 450 | + .setContentValidationAlgorithm(ContentValidationAlgorithm.CRC64); |
| 451 | + BlobDownloadToFileOptions optionsWithoutContentVal |
| 452 | + = new BlobDownloadToFileOptions(outFileWithoutContentVal.getAbsolutePath()) |
| 453 | + .setParallelTransferOptions(parallelOptionsWithoutContentVal); |
| 454 | + |
| 455 | + client.downloadToFileWithResponse(optionsWithContentVal, null, Context.NONE); |
| 456 | + client.downloadToFileWithResponse(optionsWithoutContentVal, null, Context.NONE); |
| 457 | + |
| 458 | + long expectedBytes = data.length; |
| 459 | + assertEquals(expectedBytes, mockListenerWithContentVal.getReportedByteCount()); |
| 460 | + assertEquals(expectedBytes, mockListenerWithoutContentVal.getReportedByteCount()); |
| 461 | + } |
| 462 | + |
| 463 | + private static final class MockProgressListener implements ProgressListener { |
| 464 | + private final AtomicLong reportedByteCount = new AtomicLong(0L); |
| 465 | + |
| 466 | + @Override |
| 467 | + public void handleProgress(long bytesTransferred) { |
| 468 | + this.reportedByteCount.updateAndGet(current -> Math.max(current, bytesTransferred)); |
| 469 | + } |
| 470 | + |
| 471 | + long getReportedByteCount() { |
| 472 | + return this.reportedByteCount.get(); |
| 473 | + } |
| 474 | + } |
| 475 | + |
422 | 476 | static Stream<Arguments> channelReadDataSupplier() { |
423 | 477 | return Stream.of(Arguments.of(50, 40, Constants.KB), Arguments.of(Constants.KB + 50, 40, Constants.KB), |
424 | 478 | Arguments.of(null, Constants.MB, TEN_MB)); |
|
0 commit comments