From fd62a1cf5977ee72bcf763ce3564a9f69c1aac32 Mon Sep 17 00:00:00 2001 From: Eduard Tudenhoefner Date: Wed, 2 Dec 2026 19:12:37 +0100 Subject: [PATCH] MINOR: Use AssertJ instead of JUnit assertions in vectored IO tests Replace org.junit.jupiter.api.Assertions with AssertJ in the vectored IO tests of parquet-hadoop, following agents/docs/writing-tests.md. Add a RestrictImports enforcer rule that bans imports of org.junit.jupiter.api.Assertions and org.junit.jupiter.api.Assumptions in test code, so new tests use AssertJ. Co-authored-by: Isaac --- .../TestParquetFileReaderBufferLeak.java | 61 ++++--- .../TestParquetFileReaderVectoredIO.java | 149 +++++++++--------- ...estParquetFileReaderVectoredOwnership.java | 49 +++--- .../TestVectoredReadBufferAllocator.java | 105 ++++++------ .../hadoop/TestVectoredReadOperation.java | 128 ++++++++------- pom.xml | 22 +++ 6 files changed, 287 insertions(+), 227 deletions(-) diff --git a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderBufferLeak.java b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderBufferLeak.java index a834237bab..2bda4df411 100644 --- a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderBufferLeak.java +++ b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderBufferLeak.java @@ -20,10 +20,7 @@ import static org.apache.parquet.hadoop.ParquetFileWriter.Mode.OVERWRITE; import static org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT64; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import java.io.File; import java.io.IOException; @@ -121,7 +118,9 @@ private int getRowGroupCount(Path path) throws IOException { public void testReadNextRowGroupReleasesBuffersOfPreviousRowGroup() throws Exception { Path path = writeMultiRowGroupFile(500); int expectedRowGroups = getRowGroupCount(path); - assertTrue(expectedRowGroups > 1, "Expected multiple row groups but got " + expectedRowGroups); + assertThat(expectedRowGroups) + .as("Expected multiple row groups but got " + expectedRowGroups) + .isGreaterThan(1); try (TrackingByteBufferAllocator readAllocator = TrackingByteBufferAllocator.wrap(new HeapByteBufferAllocator())) { @@ -133,10 +132,10 @@ public void testReadNextRowGroupReleasesBuffersOfPreviousRowGroup() throws Excep PageReadStore pages; int rowGroupCount = 0; while ((pages = reader.readNextRowGroup()) != null) { - assertNotNull(pages); + assertThat(pages).isNotNull(); rowGroupCount++; } - assertEquals(expectedRowGroups, rowGroupCount); + assertThat(rowGroupCount).isEqualTo(expectedRowGroups); } // After reader.close(), all buffers including the last row group's should be released. // readAllocator.close() will throw LeakedByteBufferException if any leak remains. @@ -151,7 +150,9 @@ public void testReadNextRowGroupReleasesBuffersOfPreviousRowGroup() throws Excep public void testCloseReleasesCurrentRowGroupBuffers() throws Exception { Path path = writeMultiRowGroupFile(500); int expectedRowGroups = getRowGroupCount(path); - assertTrue(expectedRowGroups > 1, "Expected multiple row groups but got " + expectedRowGroups); + assertThat(expectedRowGroups) + .as("Expected multiple row groups but got " + expectedRowGroups) + .isGreaterThan(1); try (TrackingByteBufferAllocator readAllocator = TrackingByteBufferAllocator.wrap(new HeapByteBufferAllocator())) { @@ -162,8 +163,8 @@ public void testCloseReleasesCurrentRowGroupBuffers() throws Exception { try (ParquetFileReader reader = new ParquetFileReader(inputFile, options)) { // Read only the first row group PageReadStore pages = reader.readNextRowGroup(); - assertNotNull(pages); - assertTrue(pages.getRowCount() > 0); + assertThat(pages).isNotNull(); + assertThat(pages.getRowCount()).isPositive(); // Don't read remaining row groups - close should release the current row group's buffers } } @@ -177,7 +178,9 @@ public void testCloseReleasesCurrentRowGroupBuffers() throws Exception { public void testReadNextFilteredRowGroupReleasesBuffersWhenNoFilter() throws Exception { Path path = writeMultiRowGroupFile(500); int expectedRowGroups = getRowGroupCount(path); - assertTrue(expectedRowGroups > 1, "Expected multiple row groups but got " + expectedRowGroups); + assertThat(expectedRowGroups) + .as("Expected multiple row groups but got " + expectedRowGroups) + .isGreaterThan(1); try (TrackingByteBufferAllocator readAllocator = TrackingByteBufferAllocator.wrap(new HeapByteBufferAllocator())) { @@ -190,10 +193,10 @@ public void testReadNextFilteredRowGroupReleasesBuffersWhenNoFilter() throws Exc PageReadStore pages; int rowGroupCount = 0; while ((pages = reader.readNextFilteredRowGroup()) != null) { - assertNotNull(pages); + assertThat(pages).isNotNull(); rowGroupCount++; } - assertEquals(expectedRowGroups, rowGroupCount); + assertThat(rowGroupCount).isEqualTo(expectedRowGroups); } } } @@ -207,7 +210,9 @@ public void testReadNextFilteredRowGroupReleasesBuffersWhenNoFilter() throws Exc public void testReadNextFilteredRowGroupReleasesBuffersWithFilter() throws Exception { Path path = writeMultiRowGroupFile(500); int expectedRowGroups = getRowGroupCount(path); - assertTrue(expectedRowGroups > 1, "Expected multiple row groups but got " + expectedRowGroups); + assertThat(expectedRowGroups) + .as("Expected multiple row groups but got " + expectedRowGroups) + .isGreaterThan(1); try (TrackingByteBufferAllocator readAllocator = TrackingByteBufferAllocator.wrap(new HeapByteBufferAllocator())) { @@ -225,11 +230,11 @@ public void testReadNextFilteredRowGroupReleasesBuffersWithFilter() throws Excep PageReadStore pages; int rowGroupCount = 0; while ((pages = reader.readNextFilteredRowGroup()) != null) { - assertNotNull(pages); + assertThat(pages).isNotNull(); rowGroupCount++; } // All row groups should be read since the filter matches everything - assertEquals(expectedRowGroups, rowGroupCount); + assertThat(rowGroupCount).isEqualTo(expectedRowGroups); } } } @@ -243,7 +248,9 @@ public void testReadNextFilteredRowGroupReleasesBuffersWithFilter() throws Excep public void testReadNextFilteredRowGroupWithColumnIndexFilteringReleasesBuffers() throws Exception { Path path = writeMultiRowGroupFile(500); int totalRowGroups = getRowGroupCount(path); - assertTrue(totalRowGroups > 1, "Expected multiple row groups but got " + totalRowGroups); + assertThat(totalRowGroups) + .as("Expected multiple row groups but got " + totalRowGroups) + .isGreaterThan(1); try (TrackingByteBufferAllocator readAllocator = TrackingByteBufferAllocator.wrap(new HeapByteBufferAllocator())) { @@ -262,12 +269,14 @@ public void testReadNextFilteredRowGroupWithColumnIndexFilteringReleasesBuffers( PageReadStore pages; int rowGroupCount = 0; while ((pages = reader.readNextFilteredRowGroup()) != null) { - assertNotNull(pages); - assertTrue(pages.getRowCount() > 0); + assertThat(pages).isNotNull(); + assertThat(pages.getRowCount()).isPositive(); rowGroupCount++; } // At least some row groups should be returned (those containing id < 10) - assertTrue(rowGroupCount >= 1, "Expected at least 1 row group returned"); + assertThat(rowGroupCount) + .as("Expected at least 1 row group returned") + .isGreaterThanOrEqualTo(1); } } } @@ -280,7 +289,9 @@ public void testReadNextFilteredRowGroupWithColumnIndexFilteringReleasesBuffers( public void testPartialReadThenCloseReleasesBuffers() throws Exception { Path path = writeMultiRowGroupFile(500); int expectedRowGroups = getRowGroupCount(path); - assertTrue(expectedRowGroups >= 3, "Expected at least 3 row groups but got " + expectedRowGroups); + assertThat(expectedRowGroups) + .as("Expected at least 3 row groups but got " + expectedRowGroups) + .isGreaterThanOrEqualTo(3); try (TrackingByteBufferAllocator readAllocator = TrackingByteBufferAllocator.wrap(new HeapByteBufferAllocator())) { @@ -291,11 +302,11 @@ public void testPartialReadThenCloseReleasesBuffers() throws Exception { try (ParquetFileReader reader = new ParquetFileReader(inputFile, options)) { // Read row group 1 PageReadStore pages = reader.readNextRowGroup(); - assertNotNull(pages); + assertThat(pages).isNotNull(); // Read row group 2 (should release row group 1 buffers) pages = reader.readNextRowGroup(); - assertNotNull(pages); + assertThat(pages).isNotNull(); // Close without reading remaining row groups - close() should release row group 2 buffers } @@ -322,8 +333,8 @@ public void testExhaustedReaderNoLeak() throws Exception { // drain } // Additional calls should return null without leaking - assertNull(reader.readNextRowGroup()); - assertNull(reader.readNextRowGroup()); + assertThat(reader.readNextRowGroup()).isNull(); + assertThat(reader.readNextRowGroup()).isNull(); } } } diff --git a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderVectoredIO.java b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderVectoredIO.java index 41d65591c9..e57e00447c 100644 --- a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderVectoredIO.java +++ b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderVectoredIO.java @@ -24,8 +24,7 @@ import static org.apache.parquet.filter2.predicate.FilterApi.ltEq; import static org.apache.parquet.filter2.predicate.FilterApi.or; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.IOException; import java.io.InterruptedIOException; @@ -130,13 +129,13 @@ private void assertCachesFileLengthAcrossVectoredReads(boolean supplyFooter) thr : ParquetFileReader.open(countingFile, options, stream)) { // Reading the footer needs the length, but a supplied footer does not. int initialLengthCalls = supplyFooter ? 0 : 1; - assertEquals(initialLengthCalls, countingFile.lengthCalls); + assertThat(countingFile.lengthCalls).isEqualTo(initialLengthCalls); reader.setRequestedSchema(PROJECTED_SCHEMA); try (PageReadStore pages = reader.readRowGroup(0)) { assertRows(pages, PROJECTED_SCHEMA, false); } - assertEquals(initialLengthCalls + 1, countingFile.lengthCalls); + assertThat(countingFile.lengthCalls).isEqualTo(initialLengthCalls + 1); try (PageReadStore pages = reader.readFilteredRowGroup(0)) { assertRows(pages, PROJECTED_SCHEMA, true); @@ -144,10 +143,10 @@ private void assertCachesFileLengthAcrossVectoredReads(boolean supplyFooter) thr try (PageReadStore pages = reader.readRowGroup(0)) { assertRows(pages, PROJECTED_SCHEMA, false); } - assertEquals(initialLengthCalls + 1, countingFile.lengthCalls); + assertThat(countingFile.lengthCalls).isEqualTo(initialLengthCalls + 1); } - assertEquals(3, stream.vectorCalls); + assertThat(stream.vectorCalls).isEqualTo(3); } @Test @@ -162,9 +161,9 @@ public void testSuppliedFooterAvoidsLengthLookupForOrdinaryReads() throws Except try (PageReadStore pages = reader.readRowGroup(0)) { assertRows(pages, SCHEMA, false); } - assertEquals(0, countingFile.lengthCalls); + assertThat(countingFile.lengthCalls).isZero(); } - assertEquals(0, stream.vectorCalls); + assertThat(stream.vectorCalls).isZero(); } @Test @@ -195,8 +194,8 @@ private void assertLengthLookupFailureFallsBack(boolean supplyFooter) throws Exc } // Once this reader has selected ordinary IO, it must not repeat a failed // metadata lookup on each subsequent row-group read. - assertEquals(countingFile.successfulLengthCalls + 1, countingFile.lengthCalls); - assertEquals(0, stream.vectorCalls); + assertThat(countingFile.lengthCalls).isEqualTo(countingFile.successfulLengthCalls + 1); + assertThat(stream.vectorCalls).isZero(); } } @@ -211,11 +210,13 @@ public void testInterruptedLengthLookupDoesNotFallBack() throws Exception { try (ParquetFileReader reader = ParquetFileReader.open( countingFile, footer, readOptions(new RecordingAllocator(), 128, false), stream)) { stream.resetOrdinaryReads(); - IOException failure = assertThrows(InterruptedIOException.class, () -> reader.readRowGroup(0)); - assertThat(failure).isSameAs(countingFile.lengthFailure); - assertEquals(0, stream.vectorCalls); - assertEquals(0, stream.normalSeekCalls); - assertEquals(0, stream.normalReadCalls); + assertThatThrownBy(() -> reader.readRowGroup(0)) + .isSameAs(countingFile.lengthFailure) + .isInstanceOf(InterruptedIOException.class) + .hasMessage("interrupted metadata lookup"); + assertThat(stream.vectorCalls).isZero(); + assertThat(stream.normalSeekCalls).isZero(); + assertThat(stream.normalReadCalls).isZero(); } } @@ -243,7 +244,7 @@ public void testSplitsAdjacentColumnsAtMaximumAllocation() throws Exception { } } - assertEquals(1, stream.vectorCalls); + assertThat(stream.vectorCalls).isEqualTo(1); assertThat(stream.rangeLengths.size() > 1).isTrue(); for (int rangeLength : stream.rangeLengths) { assertThat(rangeLength <= maximumAllocation).isTrue(); @@ -272,16 +273,17 @@ public void testSplitsSingleOversizedColumnIntoBoundedVectoredRanges() throws Ex } } - assertEquals(1, stream.vectorCalls); - assertEquals((column.getTotalSize() + maximumAllocation - 1) / maximumAllocation, stream.rangeLengths.size()); + assertThat(stream.vectorCalls).isEqualTo(1); + assertThat((long) stream.rangeLengths.size()) + .isEqualTo((column.getTotalSize() + maximumAllocation - 1) / maximumAllocation); long nextOffset = column.getStartingPos(); for (int rangeIndex = 0; rangeIndex < stream.rangeLengths.size(); rangeIndex++) { - assertEquals(nextOffset, stream.rangeOffsets.get(rangeIndex).longValue()); + assertThat(stream.rangeOffsets.get(rangeIndex).longValue()).isEqualTo(nextOffset); int rangeLength = stream.rangeLengths.get(rangeIndex); assertThat(rangeLength <= maximumAllocation).isTrue(); nextOffset += rangeLength; } - assertEquals(column.getStartingPos() + column.getTotalSize(), nextOffset); + assertThat(nextOffset).isEqualTo(column.getStartingPos() + column.getTotalSize()); assertThat(allocator.maximumAllocation <= maximumAllocation).isTrue(); } @@ -329,11 +331,11 @@ public void testSplitsProductionSizedColumnIntoEightMegabyteVectoredRanges() thr try (PageReadStore pages = reader.readNextRowGroup()) { assertRows(pages, SCHEMA, false, ROW_COUNT, padding); } - assertEquals(0, stream.normalSeekCalls); - assertEquals(0, stream.normalReadCalls); + assertThat(stream.normalSeekCalls).isZero(); + assertThat(stream.normalReadCalls).isZero(); } - assertEquals(1, stream.vectorCalls); + assertThat(stream.vectorCalls).isEqualTo(1); assertThat(stream.rangeLengths.size() >= 3).isTrue(); for (int rangeLength : stream.rangeLengths) { assertThat(rangeLength <= maximumAllocation).isTrue(); @@ -355,7 +357,7 @@ public void testFilteredVectoredRangesRespectMaximumAllocation() throws Exceptio } } - assertEquals(1, stream.vectorCalls); + assertThat(stream.vectorCalls).isEqualTo(1); assertThat(allocator.maximumAllocation <= maximumAllocation).isTrue(); } @@ -382,7 +384,7 @@ public void testSplitsOversizedFilteredPageIntoBoundedVectoredRanges() throws Ex } } - assertEquals(1, stream.vectorCalls); + assertThat(stream.vectorCalls).isEqualTo(1); assertThat(stream.rangeLengths.size() > 2).isTrue(); for (int rangeLength : stream.rangeLengths) { assertThat(rangeLength <= maximumAllocation).isTrue(); @@ -411,7 +413,7 @@ public void testSplitsAdjacentFilteredPageRangesAtMaximumAllocation() throws Exc } } - assertEquals(1, stream.vectorCalls); + assertThat(stream.vectorCalls).isEqualTo(1); assertThat(stream.rangeLengths.size() > 1).isTrue(); for (int rangeLength : stream.rangeLengths) { assertThat(rangeLength <= maximumAllocation).isTrue(); @@ -436,10 +438,10 @@ public void testLeavesNonVectoredReadsUnchanged() throws Exception { try (PageReadStore pages = reader.readNextRowGroup()) { assertRows(pages, SCHEMA, false); } - assertEquals(1, stream.normalSeekCalls); + assertThat(stream.normalSeekCalls).isEqualTo(1); } - assertEquals(0, stream.vectorCalls); + assertThat(stream.vectorCalls).isZero(); assertThat(allocator.maximumAllocation <= maximumAllocation).isTrue(); } @@ -455,10 +457,10 @@ public void testUnsupportedBackendPreservesContiguousOrdinaryRead() throws Excep try (PageReadStore pages = reader.readNextRowGroup()) { assertRows(pages, SCHEMA, false); } - assertEquals(1, stream.normalSeekCalls); + assertThat(stream.normalSeekCalls).isEqualTo(1); } - assertEquals(0, stream.vectorCalls); + assertThat(stream.vectorCalls).isZero(); assertThat(allocator.maximumAllocation <= maximumAllocation).isTrue(); } @@ -477,10 +479,10 @@ public void testUnsupportedBackendPreservesContiguousFilteredOrdinaryRead() thro assertRows(pages, ID_ONLY_SCHEMA, true); } // The predicate selects two disjoint page spans; each span should require exactly one seek. - assertEquals(2, stream.normalSeekCalls); + assertThat(stream.normalSeekCalls).isEqualTo(2); } - assertEquals(0, stream.vectorCalls); + assertThat(stream.vectorCalls).isZero(); assertThat(allocator.maximumAllocation <= maximumAllocation).isTrue(); } @@ -566,17 +568,17 @@ private void assertDrainsPendingVectoredReadsBeforeClosing( if (failureMode == FailureMode.FIRST_IO_EXCEPTION_THEN_SOCKET_TIMEOUT_PENDING_SIBLING_NO_CLOSE_CANCELLATION) { assertThat(stream.rangeLengths.size() >= 3).isTrue(); - assertEquals(1, failure.getSuppressed().length); + assertThat(failure.getSuppressed()).hasSize(1); assertThat(failure.getSuppressed()[0] instanceof SocketTimeoutException) .isTrue(); } - assertEquals(0, stream.pendingFutureCount()); - assertEquals(0, stream.normalSeekCalls); - assertEquals(0, stream.normalReadCalls); + assertThat(stream.pendingFutureCount()).isZero(); + assertThat(stream.normalSeekCalls).isZero(); + assertThat(stream.normalReadCalls).isZero(); } - assertEquals(1, stream.vectorCalls); - assertEquals(0, stream.postClosePhysicalReads.get()); + assertThat(stream.vectorCalls).isEqualTo(1); + assertThat(stream.postClosePhysicalReads.get()).isZero(); assertThat(stream.completedPendingPhysicalReads.get() > 0).isTrue(); } @@ -619,26 +621,28 @@ private void assertFailsAfterVectoredSubmission(FailureMode failureMode, Class reader.readRowGroup(0)); - assertThat(retryFailure).hasMessageContaining("Cannot reuse a reader after a vectored read failure"); - assertEquals(1, stream.vectorCalls); - assertEquals(0, stream.normalSeekCalls); - assertEquals(0, stream.normalReadCalls); + assertThatThrownBy(reader::readNextFilteredRowGroup) + .isInstanceOf(IOException.class) + .hasMessageContaining("asynchronous reads may have been submitted") + .cause() + .isInstanceOf(causeType); + assertThat(stream.normalSeekCalls).isZero(); + assertThat(stream.normalReadCalls).isZero(); + assertThatThrownBy(() -> reader.readRowGroup(0)) + .isInstanceOf(IOException.class) + .hasMessageContaining("Cannot reuse a reader after a vectored read failure"); + assertThat(stream.vectorCalls).isEqualTo(1); + assertThat(stream.normalSeekCalls).isZero(); + assertThat(stream.normalReadCalls).isZero(); if (failureMode.hasPendingRead()) { if (failureMode.hasPublishedPendingRead()) { - assertEquals(0, stream.pendingFutureCount()); + assertThat(stream.pendingFutureCount()).isZero(); } } } stream.closeCompleted.get(10, TimeUnit.SECONDS); - assertEquals(1, stream.vectorCalls); - assertEquals(0, stream.pendingFutureCount()); + assertThat(stream.vectorCalls).isEqualTo(1); + assertThat(stream.pendingFutureCount()).isZero(); } private void assertFailsAfterSplitColumnSubmission(FailureMode failureMode, Class causeType) throws Exception { @@ -650,15 +654,16 @@ private void assertFailsAfterSplitColumnSubmission(FailureMode failureMode, Clas allocator.reset(); reader.setRequestedSchema(ID_ONLY_SCHEMA); stream.resetOrdinaryReads(); - IOException failure = assertThrows(IOException.class, reader::readNextRowGroup); - assertThat(failure.getMessage().contains("asynchronous reads may have been submitted")) - .isTrue(); - assertThat(causeType.isInstance(failure.getCause())).isTrue(); - assertEquals(0, stream.normalSeekCalls); - assertEquals(0, stream.normalReadCalls); + assertThatThrownBy(reader::readNextRowGroup) + .isInstanceOf(IOException.class) + .hasMessageContaining("asynchronous reads may have been submitted") + .cause() + .isInstanceOf(causeType); + assertThat(stream.normalSeekCalls).isZero(); + assertThat(stream.normalReadCalls).isZero(); } - assertEquals(1, stream.vectorCalls); + assertThat(stream.vectorCalls).isEqualTo(1); assertThat(stream.rangeLengths.size() > 1).isTrue(); assertThat(allocator.maximumAllocation <= maximumAllocation).isTrue(); } @@ -677,14 +682,15 @@ private void assertFailsAfterConsumingColumnBeforeSplitRange(FailureMode failure try (ParquetFileReader reader = ParquetFileReader.open(inputFile, readOptions(allocator, maximumAllocation, false), stream)) { stream.resetOrdinaryReads(); - IOException failure = assertThrows(IOException.class, reader::readNextRowGroup); - assertThat(failure.getMessage().contains("asynchronous reads may have been submitted")) - .isTrue(); - assertThat(causeType.isInstance(failure.getCause())).isTrue(); - assertEquals(0, stream.normalSeekCalls); - assertEquals(0, stream.normalReadCalls); + assertThatThrownBy(reader::readNextRowGroup) + .isInstanceOf(IOException.class) + .hasMessageContaining("asynchronous reads may have been submitted") + .cause() + .isInstanceOf(causeType); + assertThat(stream.normalSeekCalls).isZero(); + assertThat(stream.normalReadCalls).isZero(); } - assertEquals(1, stream.vectorCalls); + assertThat(stream.vectorCalls).isEqualTo(1); } private static void preloadFilteredIndexes(ParquetFileReader reader, MessageType projection) { @@ -724,7 +730,7 @@ private static void assertRows(PageReadStore pages, MessageType projection, bool private static void assertRows( PageReadStore pages, MessageType projection, boolean filtered, long expectedRows, String expectedPadding) { - assertEquals(expectedRows, pages.getRowCount()); + assertThat(pages.getRowCount()).isEqualTo(expectedRows); PrimitiveIterator.OfLong rowIndexes = filtered ? pages.getRowIndexes().get() : null; MessageColumnIO columns = new ColumnIOFactory().getColumnIO(projection, SCHEMA); @@ -732,14 +738,13 @@ private static void assertRows( for (long row = 0; row < expectedRows; row++) { long expectedIndex = filtered ? rowIndexes.nextLong() : row; Group record = records.read(); - assertEquals(expectedIndex, record.getInteger("id", 0)); + assertThat(record.getInteger("id", 0)).isEqualTo(expectedIndex); if (projection.containsField("padding")) { - assertEquals( - expectedPadding == null ? "padding_" + expectedIndex : expectedPadding, - record.getString("padding", 0)); + assertThat(record.getString("padding", 0)) + .isEqualTo(expectedPadding == null ? "padding_" + expectedIndex : expectedPadding); } if (projection.containsField("other")) { - assertEquals(OTHER_COLUMN_BASE + expectedIndex, record.getInteger("other", 0)); + assertThat(record.getInteger("other", 0)).isEqualTo(OTHER_COLUMN_BASE + expectedIndex); } } } diff --git a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderVectoredOwnership.java b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderVectoredOwnership.java index bb6e0ff0dc..4bd266c169 100644 --- a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderVectoredOwnership.java +++ b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetFileReaderVectoredOwnership.java @@ -22,9 +22,6 @@ import static org.apache.parquet.filter2.predicate.FilterApi.ltEq; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; import java.io.InterruptedIOException; @@ -145,8 +142,8 @@ void testReleasesSlicedBuffersForFilteredRowGroup() throws Exception { try (ParquetFileReader reader = ParquetFileReader.open( inputFile, footer, readOptions, new OwnedStream(inputFile.newStream(), ReadMode.SLICES))) { try (PageReadStore pages = reader.readFilteredRowGroup(0)) { - assertTrue(pages.getRowCount() > 0 && pages.getRowCount() < ROWS); - assertTrue(pages.getRowIndexes().isPresent()); + assertThat(pages.getRowCount()).isGreaterThan(0).isLessThan(ROWS); + assertThat(pages.getRowIndexes()).isPresent(); PrimitiveIterator.OfLong indexes = pages.getRowIndexes().get(); RecordReader records = new ColumnIOFactory() .getColumnIO(SCHEMA) @@ -154,11 +151,11 @@ inputFile, footer, readOptions, new OwnedStream(inputFile.newStream(), ReadMode. for (long row = 0; row < pages.getRowCount(); row++) { long expectedIndex = indexes.nextLong(); Group record = records.read(); - assertEquals(expectedIndex, record.getInteger("id", 0)); - assertEquals("padding_" + expectedIndex, record.getString("padding", 0)); + assertThat(record.getInteger("id", 0)).isEqualTo(expectedIndex); + assertThat(record.getString("padding", 0)).isEqualTo("padding_" + expectedIndex); } } - assertEquals(delegate.allocations.get(), delegate.releases.get()); + assertThat(delegate.releases.get()).isEqualTo(delegate.allocations.get()); } } } @@ -169,27 +166,27 @@ private void assertSuccessfulReadReleasesBuffers(ReadMode mode) throws Exception OwnedStream stream = new OwnedStream(inputFile.newStream(), mode); try (ParquetFileReader reader = ParquetFileReader.open(inputFile, footer, options(tracking), stream)) { try (PageReadStore pages = reader.readRowGroup(0)) { - assertEquals(ROWS, pages.getRowCount()); + assertThat(pages.getRowCount()).isEqualTo(ROWS); RecordReader records = new ColumnIOFactory() .getColumnIO(SCHEMA) .getRecordReader(pages, new GroupRecordConverter(SCHEMA)); for (int row = 0; row < ROWS; row++) { Group record = records.read(); - assertEquals(row, record.getInteger("id", 0)); - assertEquals("padding_" + row, record.getString("padding", 0)); + assertThat(record.getInteger("id", 0)).isEqualTo(row); + assertThat(record.getString("padding", 0)).isEqualTo("padding_" + row); } - assertEquals(0, delegate.releases.get()); + assertThat(delegate.releases.get()).isZero(); } - assertTrue(stream.rangeCount > 1); + assertThat(stream.rangeCount).isGreaterThan(1); assertThat(stream.maximumRangeLength).isLessThanOrEqualTo(MAXIMUM_ALLOCATION); if (mode == ReadMode.SLICES) { - assertEquals(2, delegate.allocations.get()); + assertThat(delegate.allocations.get()).isEqualTo(2); assertThat(delegate.maximumAllocation.get()).isGreaterThan(MAXIMUM_ALLOCATION); } else { - assertEquals(stream.rangeCount, delegate.allocations.get()); + assertThat(delegate.allocations.get()).isEqualTo(stream.rangeCount); assertThat(delegate.maximumAllocation.get()).isLessThanOrEqualTo(MAXIMUM_ALLOCATION); } - assertEquals(delegate.allocations.get(), delegate.releases.get()); + assertThat(delegate.releases.get()).isEqualTo(delegate.allocations.get()); } } } @@ -200,11 +197,13 @@ void testReleasesTransferredBuffersWhenPageHeaderParsingFails() throws Exception try (TrackingByteBufferAllocator tracking = TrackingByteBufferAllocator.wrap(delegate)) { try (ParquetFileReader reader = ParquetFileReader.open( inputFile, footer, options(tracking), new OwnedStream(inputFile.newStream(), ReadMode.CORRUPT))) { - assertThrows(IOException.class, () -> reader.readRowGroup(0)); - assertEquals(delegate.allocations.get(), delegate.releases.get()); + assertThatThrownBy(() -> reader.readRowGroup(0)) + .isInstanceOf(IOException.class) + .hasMessageStartingWith("can not read class org.apache.parquet.format.PageHeader"); + assertThat(delegate.releases.get()).isEqualTo(delegate.allocations.get()); } - assertTrue(delegate.allocations.get() > 0); - assertEquals(delegate.allocations.get(), delegate.releases.get()); + assertThat(delegate.allocations.get()).isPositive(); + assertThat(delegate.releases.get()).isEqualTo(delegate.allocations.get()); } } @@ -215,16 +214,18 @@ void testReleasesFailedAndSuccessfulAllocationsAfterAllRangesFinish() throws Exc OwnedStream stream = new OwnedStream(inputFile.newStream(), ReadMode.FAILED_RANGE); try (ParquetFileReader reader = ParquetFileReader.open(inputFile, footer, options(tracking), stream)) { try { - assertThrows(IOException.class, () -> reader.readRowGroup(0)); + assertThatThrownBy(() -> reader.readRowGroup(0)) + .isInstanceOf(IOException.class) + .hasMessage("injected range failure after allocation"); // All range futures already finished. Reclamation must not wait for deferred stream closure, // otherwise an outer allocator.close() can race the delayed release. - assertEquals(delegate.allocations.get(), delegate.releases.get()); + assertThat(delegate.releases.get()).isEqualTo(delegate.allocations.get()); } finally { stream.allowClose.countDown(); } } - assertTrue(delegate.allocations.get() > 1); - assertEquals(delegate.allocations.get(), delegate.releases.get()); + assertThat(delegate.allocations.get()).isGreaterThan(1); + assertThat(delegate.releases.get()).isEqualTo(delegate.allocations.get()); } } diff --git a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestVectoredReadBufferAllocator.java b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestVectoredReadBufferAllocator.java index 0f3dfcb815..2c03e0f31d 100644 --- a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestVectoredReadBufferAllocator.java +++ b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestVectoredReadBufferAllocator.java @@ -18,12 +18,8 @@ */ package org.apache.parquet.hadoop; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -51,24 +47,28 @@ void testTransfersOriginalBuffersAndAdditionalChecksumAllocations() { ByteBuffer first = owner.allocate(32); ByteBuffer second = owner.allocate(32); owner.allocate(8); // A filesystem checksum allocation does not appear in the range results. - assertEquals(first, second); // Content equality must not collapse distinct original allocations. + assertThat(second).isEqualTo(first); // Content equality must not collapse distinct original allocations. ByteBuffer firstResult = first.slice(); ByteBuffer secondResult = second.slice(); - assertNotSame(first, firstResult); - assertNotSame(second, secondResult); + assertThat(firstResult).isNotSameAs(first); + assertThat(secondResult).isNotSameAs(second); owner.transferTo(rowGroup); owner.close(); - assertEquals(0, delegate.releases.get()); + assertThat(delegate.releases.get()).isZero(); firstResult.put(0, (byte) 37); - assertEquals(37, first.get(0)); - assertThrows(IllegalStateException.class, () -> owner.allocate(1)); - assertThrows(IllegalStateException.class, () -> owner.transferTo(rowGroup)); + assertThat(first.get(0)).isEqualTo((byte) 37); + assertThatThrownBy(() -> owner.allocate(1)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Vectored read is no longer accepting buffer allocations"); + assertThatThrownBy(() -> owner.transferTo(rowGroup)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Vectored read buffer ownership has already been stopped or transferred"); rowGroup.close(); - assertEquals(3, delegate.releases.get()); + assertThat(delegate.releases.get()).isEqualTo(3); } - assertEquals(3, delegate.releases.get()); + assertThat(delegate.releases.get()).isEqualTo(3); } @Test @@ -80,16 +80,20 @@ void testStopAllocatingDoesNotReleaseBuffersStillUsedByIo() { ByteBuffer buffer = owner.allocate(32); owner.stopAllocating(); owner.stopAllocating(); - assertThrows(IllegalStateException.class, () -> owner.allocate(16)); - assertThrows(IllegalStateException.class, () -> owner.transferTo(rowGroup)); - assertEquals(1, delegate.allocations.get()); - assertEquals(0, delegate.releases.get()); + assertThatThrownBy(() -> owner.allocate(16)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Vectored read is no longer accepting buffer allocations"); + assertThatThrownBy(() -> owner.transferTo(rowGroup)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Vectored read buffer ownership has already been stopped or transferred"); + assertThat(delegate.allocations.get()).isEqualTo(1); + assertThat(delegate.releases.get()).isZero(); buffer.putInt(0, 1234); // An already accepted read may still finish after abort. - assertEquals(1234, buffer.getInt(0)); + assertThat(buffer.getInt(0)).isEqualTo(1234); owner.close(); // The caller has now established that the read finished. owner.close(); - assertEquals(1, delegate.releases.get()); + assertThat(delegate.releases.get()).isEqualTo(1); } } @@ -101,16 +105,20 @@ void testBackendReleasedOriginalIsNotReleasedAgain() { ByteBufferReleaser rowGroup = new ByteBufferReleaser(tracking)) { ByteBuffer original = owner.allocate(32); owner.allocate(32); - assertThrows(IllegalArgumentException.class, () -> owner.release(original.slice())); - assertEquals(0, delegate.releases.get()); + assertThatThrownBy(() -> owner.release(original.slice())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Buffer is not owned by this vectored read"); + assertThat(delegate.releases.get()).isZero(); owner.release(original); - assertEquals(1, delegate.releases.get()); - assertThrows(IllegalArgumentException.class, () -> owner.release(original)); + assertThat(delegate.releases.get()).isEqualTo(1); + assertThatThrownBy(() -> owner.release(original)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Buffer is not owned by this vectored read"); owner.transferTo(rowGroup); owner.close(); rowGroup.close(); - assertEquals(2, delegate.releases.get()); + assertThat(delegate.releases.get()).isEqualTo(2); } } @@ -144,24 +152,26 @@ public void release(ByteBuffer buffer) { ExecutorService executor = Executors.newFixedThreadPool(2); try { Future allocation = executor.submit(() -> owner.allocate(32)); - assertTrue(allocationStarted.await(10, TimeUnit.SECONDS)); + assertThat(allocationStarted.await(10, TimeUnit.SECONDS)).isTrue(); executor.submit(owner::stopAllocating).get(5, TimeUnit.SECONDS); - assertFalse(allocation.isDone()); - assertEquals(0, releases.get()); + assertThat(allocation).isNotDone(); + assertThat(releases.get()).isZero(); allowAllocation.countDown(); ByteBuffer buffer = allocation.get(10, TimeUnit.SECONDS); buffer.putInt(0, 1234); - assertEquals(1234, buffer.getInt(0)); - assertEquals(0, releases.get()); - assertThrows(IllegalStateException.class, () -> owner.allocate(32)); + assertThat(buffer.getInt(0)).isEqualTo(1234); + assertThat(releases.get()).isZero(); + assertThatThrownBy(() -> owner.allocate(32)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Vectored read is no longer accepting buffer allocations"); owner.close(); - assertEquals(1, releases.get()); + assertThat(releases.get()).isEqualTo(1); } finally { allowAllocation.countDown(); owner.stopAllocating(); executor.shutdownNow(); - assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS)); + assertThat(executor.awaitTermination(10, TimeUnit.SECONDS)).isTrue(); } } } @@ -178,7 +188,7 @@ void testConcurrentAllocationsAndAbortRetainEveryAcceptedOriginal() throws Excep List> workers = new ArrayList<>(); for (int worker = 0; worker < 4; worker++) { workers.add(executor.submit(() -> { - assertTrue(start.await(10, TimeUnit.SECONDS)); + assertThat(start.await(10, TimeUnit.SECONDS)).isTrue(); for (int allocation = 0; allocation < 100; allocation++) { try { owner.allocate(32); @@ -191,7 +201,7 @@ void testConcurrentAllocationsAndAbortRetainEveryAcceptedOriginal() throws Excep })); } Future abort = executor.submit(() -> { - assertTrue(firstAllocation.await(10, TimeUnit.SECONDS)); + assertThat(firstAllocation.await(10, TimeUnit.SECONDS)).isTrue(); owner.stopAllocating(); return null; }); @@ -200,16 +210,18 @@ void testConcurrentAllocationsAndAbortRetainEveryAcceptedOriginal() throws Excep worker.get(10, TimeUnit.SECONDS); } abort.get(10, TimeUnit.SECONDS); - assertTrue(delegate.allocations.get() > 0); - assertEquals(0, delegate.releases.get()); - assertThrows(IllegalStateException.class, () -> owner.allocate(32)); + assertThat(delegate.allocations.get()).isPositive(); + assertThat(delegate.releases.get()).isZero(); + assertThatThrownBy(() -> owner.allocate(32)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Vectored read is no longer accepting buffer allocations"); owner.close(); - assertEquals(delegate.allocations.get(), delegate.releases.get()); + assertThat(delegate.releases.get()).isEqualTo(delegate.allocations.get()); } finally { start.countDown(); owner.stopAllocating(); executor.shutdownNow(); - assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS)); + assertThat(executor.awaitTermination(10, TimeUnit.SECONDS)).isTrue(); } } } @@ -230,12 +242,13 @@ public void release(ByteBuffer buffer) { owner.allocate(8); owner.allocate(8); owner.allocate(8); - AutoCloseables.ParquetCloseResourceException error = - assertThrows(AutoCloseables.ParquetCloseResourceException.class, owner::close); - assertSame(failure, error.getCause()); - assertEquals(3, releases.get()); + assertThatThrownBy(owner::close) + .isInstanceOf(AutoCloseables.ParquetCloseResourceException.class) + .hasMessage("Unable to close resource") + .hasCauseReference(failure); + assertThat(releases.get()).isEqualTo(3); owner.close(); - assertEquals(3, releases.get()); + assertThat(releases.get()).isEqualTo(3); } private static final class CountingAllocator extends HeapByteBufferAllocator { diff --git a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestVectoredReadOperation.java b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestVectoredReadOperation.java index 7734aee200..4cc20e99d7 100644 --- a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestVectoredReadOperation.java +++ b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestVectoredReadOperation.java @@ -20,13 +20,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -66,7 +59,9 @@ public class TestVectoredReadOperation { public void stopExecutors() throws InterruptedException { for (ExecutorService executor : executors) { executor.shutdownNow(); - assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS), "Submission worker did not stop"); + assertThat(executor.awaitTermination(5, TimeUnit.SECONDS)) + .as("Submission worker did not stop") + .isTrue(); } } @@ -128,15 +123,15 @@ public void testTimeoutIncludesBlockedSubmission() throws Exception { try { TimeoutException failure = awaitSubmissionTimeout(operation); await(readsPublished); - assertFalse(operation.submissionSucceeded()); - assertEquals(0L, operation.remainingNanos()); + assertThat(operation.submissionSucceeded()).isFalse(); + assertThat(operation.remainingNanos()).isZero(); operation.abort(failure); await(interrupted); await(stream.closed); - assertEquals(1, stream.closes.get()); - assertFalse(stream.closedDuringSubmission); - assertEquals(1, allocator.released.size()); + assertThat(stream.closes.get()).isEqualTo(1); + assertThat(stream.closedDuringSubmission).isFalse(); + assertThat(allocator.released).hasSize(1); } finally { finishSubmission.countDown(); } @@ -163,19 +158,20 @@ public void testCancelledSubmissionDoesNotMeanBackendHasStopped() throws Excepti try { TimeoutException failure = awaitSubmissionTimeout(operation); await(readsPublished); - assertTimeoutPreemptively(Duration.ofSeconds(2), () -> operation.abort(failure)); + assertThat(CompletableFuture.runAsync(() -> operation.abort(failure))) + .succeedsWithin(Duration.ofSeconds(2)); await(interrupted); - assertTrue(stream.submitting); - assertEquals(0, stream.closes.get()); - assertTrue(allocator.released.isEmpty()); + assertThat(stream.submitting).isTrue(); + assertThat(stream.closes.get()).isZero(); + assertThat(allocator.released).isEmpty(); finishSubmission.countDown(); await(stream.closed); - assertFalse(stream.closedDuringSubmission); - assertEquals(1, stream.closes.get()); - assertEquals(1, allocator.released.size()); - assertEquals((byte) 37, allocator.released.get(0).get(0)); + assertThat(stream.closedDuringSubmission).isFalse(); + assertThat(stream.closes.get()).isEqualTo(1); + assertThat(allocator.released).hasSize(1); + assertThat(allocator.released.get(0).get(0)).isEqualTo((byte) 37); } finally { finishSubmission.countDown(); } @@ -208,15 +204,15 @@ public void testAbortRejectsAllocationsRequestedByLateSubmission() throws Except await(readsPublished); operation.abort(failure); await(interrupted); - assertEquals(0, stream.closes.get()); + assertThat(stream.closes.get()).isZero(); finishSubmission.countDown(); await(stream.closed); - assertNotNull(allocationFailure.get()); - assertTrue(allocator.allocated.isEmpty()); - assertTrue(allocator.released.isEmpty()); - assertEquals(1, stream.closes.get()); - assertFalse(stream.closedDuringSubmission); + assertThat(allocationFailure.get()).isNotNull(); + assertThat(allocator.allocated).isEmpty(); + assertThat(allocator.released).isEmpty(); + assertThat(stream.closes.get()).isEqualTo(1); + assertThat(stream.closedDuringSubmission).isFalse(); } finally { finishSubmission.countDown(); } @@ -254,18 +250,19 @@ public ByteBuffer allocate(int size) { try { TimeoutException failure = awaitSubmissionTimeout(operation); await(allocationStarted); - assertTimeoutPreemptively(Duration.ofSeconds(2), () -> operation.abort(failure)); + assertThat(CompletableFuture.runAsync(() -> operation.abort(failure))) + .succeedsWithin(Duration.ofSeconds(2)); await(interrupted); - assertTrue(stream.submitting); - assertEquals(0, stream.closes.get()); + assertThat(stream.submitting).isTrue(); + assertThat(stream.closes.get()).isZero(); finishAllocation.countDown(); await(stream.closed); - assertEquals(1, allocator.allocated.size()); - assertEquals(1, allocator.released.size()); - assertSame(allocator.allocated.get(0), allocator.released.get(0)); - assertEquals(1, stream.closes.get()); - assertFalse(stream.closedDuringSubmission); + assertThat(allocator.allocated).hasSize(1); + assertThat(allocator.released).hasSize(1); + assertThat(allocator.released.get(0)).isSameAs(allocator.allocated.get(0)); + assertThat(stream.closes.get()).isEqualTo(1); + assertThat(stream.closedDuringSubmission).isFalse(); } finally { finishAllocation.countDown(); } @@ -291,14 +288,15 @@ public void testSubmissionUsesTheConsumptionDeadline() throws Exception { operation.awaitSubmission(); await(submissionStarted); - assertTrue(operation.submissionSucceeded()); - assertTrue(operation.remainingNanos() <= remainingBeforeSubmission - TimeUnit.MILLISECONDS.toNanos(100)); + assertThat(operation.submissionSucceeded()).isTrue(); + assertThat(operation.remainingNanos()) + .isLessThanOrEqualTo(remainingBeforeSubmission - TimeUnit.MILLISECONDS.toNanos(100)); operation.transferTo(releaser); } finally { finishSubmission.countDown(); } - assertEquals(1, allocator.released.size()); - assertEquals(0, stream.closes.get()); + assertThat(allocator.released).hasSize(1); + assertThat(stream.closes.get()).isZero(); } @Test @@ -316,12 +314,12 @@ public void testSuccessTransfersOriginalBuffersInsteadOfFutureViews() throws Exc try (ByteBufferReleaser releaser = new ByteBufferReleaser(allocator)) { operation.awaitSubmission(); operation.transferTo(releaser); - assertTrue(allocator.released.isEmpty()); - assertFalse(futureView.get() == allocator.allocated.get(0)); + assertThat(allocator.released).isEmpty(); + assertThat(futureView.get()).isNotSameAs(allocator.allocated.get(0)); } - assertEquals(1, allocator.released.size()); - assertSame(allocator.allocated.get(0), allocator.released.get(0)); - assertEquals(0, stream.closes.get()); + assertThat(allocator.released).hasSize(1); + assertThat(allocator.released.get(0)).isSameAs(allocator.allocated.get(0)); + assertThat(stream.closes.get()).isZero(); } @Test @@ -340,9 +338,9 @@ public void testCancelledReadFutureDoesNotPermitBufferRelease() throws Exception operation.abort(new IOException("Read was cancelled without stopping backend IO")); await(stream.closed); - assertEquals(1, stream.closes.get()); - assertEquals(1, allocator.allocated.size()); - assertTrue(allocator.released.isEmpty()); + assertThat(stream.closes.get()).isEqualTo(1); + assertThat(allocator.allocated).hasSize(1); + assertThat(allocator.released).isEmpty(); } @Test @@ -356,14 +354,17 @@ public void testMissingReadFutureDoesNotPermitBufferRelease() throws Exception { VectoredReadExecutor executor = newExecutor(); VectoredReadOperation operation = new VectoredReadOperation(stream, ranges(2), allocator, executor, 5, TimeUnit.SECONDS); - IOException failure = assertThrows(IOException.class, operation::awaitSubmission); - assertFalse(operation.submissionSucceeded()); + Throwable failure = assertThatThrownBy(operation::awaitSubmission) + .isInstanceOf(IOException.class) + .hasMessage("Rejected before publishing the second range future") + .actual(); + assertThat(operation.submissionSucceeded()).isFalse(); operation.abort(failure); await(stream.closed); - assertEquals(1, stream.closes.get()); - assertEquals(1, allocator.allocated.size()); - assertTrue(allocator.released.isEmpty()); + assertThat(stream.closes.get()).isEqualTo(1); + assertThat(allocator.allocated).hasSize(1); + assertThat(allocator.released).isEmpty(); } @Test @@ -381,16 +382,16 @@ public void testPendingReadReleasesBuffersWhenItActuallyFinishes() throws Except operation.abort(new IOException("Another read failed")); await(stream.closed); - assertEquals(1, stream.closes.get()); - assertTrue(allocator.released.isEmpty()); - assertFalse(read.isCancelled()); + assertThat(stream.closes.get()).isEqualTo(1); + assertThat(allocator.released).isEmpty(); + assertThat(read).isNotCancelled(); ByteBuffer original = allocator.allocated.get(0); original.put(0, (byte) 41); read.complete(original); await(allocator.bufferReleased); - assertEquals(1, allocator.released.size()); - assertSame(original, allocator.released.get(0)); + assertThat(allocator.released).hasSize(1); + assertThat(allocator.released.get(0)).isSameAs(original); } @Test @@ -658,12 +659,19 @@ private static List ranges(int count) { } private static TimeoutException awaitSubmissionTimeout(VectoredReadOperation operation) { - return assertTimeoutPreemptively( - Duration.ofSeconds(2), () -> assertThrows(TimeoutException.class, operation::awaitSubmission)); + // The timeout has two sources with different messages (null from Future.get, and the executor's capacity wait), + // so only its type is asserted, as before. + CompletableFuture timeout = + CompletableFuture.supplyAsync(() -> (TimeoutException) assertThatThrownBy(operation::awaitSubmission) + .isInstanceOf(TimeoutException.class) + .actual()); + return assertThat(timeout).succeedsWithin(Duration.ofSeconds(2)).actual(); } private static void await(CountDownLatch latch) throws InterruptedException { - assertTrue(latch.await(5, TimeUnit.SECONDS), "Backend did not reach the expected state"); + assertThat(latch.await(5, TimeUnit.SECONDS)) + .as("Backend did not reach the expected state") + .isTrue(); } private static void awaitIgnoringInterrupts(CountDownLatch latch, CountDownLatch interrupted) throws IOException { diff --git a/pom.xml b/pom.xml index 9b57aa9636..0b4dcab0f9 100644 --- a/pom.xml +++ b/pom.xml @@ -361,6 +361,28 @@ true + + enforce-assertj-imports + process-test-sources + + enforce + + + + + Use AssertJ (assertThat, assertThatThrownBy, assumeThat) instead of JUnit assertions and assumptions + true + + org.junit.jupiter.api.Assertions + static org.junit.jupiter.api.Assertions.** + org.junit.jupiter.api.Assumptions + static org.junit.jupiter.api.Assumptions.** + + + + true + +