Skip to content

Commit b68dc8a

Browse files
zhtttylzcnaurothslfan1989
authored
HDFS-12431. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-hdfs Part7. (#7794)
* HDFS-12431. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-hdfs Part7. Co-authored-by: Chris Nauroth <[email protected]> Co-authored-by: Shilun Fan <[email protected]> Reviewed-by: Chris Nauroth <[email protected]> Reviewed-by: Shilun Fan <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent 729f19f commit b68dc8a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+892
-868
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestPipelines.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*/
1818
package org.apache.hadoop.hdfs;
1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertTrue;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

2323
import java.io.IOException;
2424
import java.util.List;
@@ -35,9 +35,9 @@
3535
import org.apache.hadoop.hdfs.server.datanode.DataNode;
3636
import org.apache.hadoop.hdfs.server.datanode.Replica;
3737
import org.apache.hadoop.test.GenericTestUtils;
38-
import org.junit.After;
39-
import org.junit.Before;
40-
import org.junit.Test;
38+
import org.junit.jupiter.api.AfterEach;
39+
import org.junit.jupiter.api.BeforeEach;
40+
import org.junit.jupiter.api.Test;
4141
import org.slf4j.event.Level;
4242

4343
public class TestPipelines {
@@ -57,13 +57,13 @@ public class TestPipelines {
5757
setConfiguration();
5858
}
5959

60-
@Before
60+
@BeforeEach
6161
public void startUpCluster() throws IOException {
6262
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(REPL_FACTOR).build();
6363
fs = cluster.getFileSystem();
6464
}
6565

66-
@After
66+
@AfterEach
6767
public void shutDownCluster() throws IOException {
6868
if (fs != null) {
6969
fs.close();
@@ -108,10 +108,9 @@ public void pipeline_01() throws IOException {
108108
Replica r =
109109
cluster.getFsDatasetTestUtils(dn).fetchReplica(lb.get(0).getBlock());
110110

111-
assertTrue("Replica on DN " + dn + " shouldn't be null", r != null);
112-
assertEquals("Should be RBW replica on " + dn
113-
+ " after sequence of calls append()/write()/hflush()",
114-
HdfsServerConstants.ReplicaState.RBW, r.getState());
111+
assertTrue(r != null, "Replica on DN " + dn + " shouldn't be null");
112+
assertEquals(HdfsServerConstants.ReplicaState.RBW, r.getState(),
113+
"Should be RBW replica on " + dn + " after sequence of calls append()/write()/hflush()");
115114
}
116115
ofs.close();
117116
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestPread.java

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
package org.apache.hadoop.hdfs;
1919

2020
import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.DFS_CLIENT_MAX_BLOCK_ACQUIRE_FAILURES_KEY;
21-
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertTrue;
23-
import static org.junit.Assert.fail;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
import static org.junit.jupiter.api.Assertions.fail;
2424

2525
import java.io.EOFException;
2626
import java.io.IOException;
@@ -53,10 +53,11 @@
5353
import org.apache.hadoop.hdfs.server.datanode.SimulatedFSDataset;
5454
import org.apache.hadoop.io.IOUtils;
5555
import org.apache.hadoop.test.GenericTestUtils;
56-
import org.junit.Assert;
57-
import org.junit.Before;
58-
import org.junit.BeforeClass;
59-
import org.junit.Test;
56+
import org.junit.jupiter.api.Assertions;
57+
import org.junit.jupiter.api.BeforeEach;
58+
import org.junit.jupiter.api.BeforeAll;
59+
import org.junit.jupiter.api.Test;
60+
import org.junit.jupiter.api.Timeout;
6061
import org.mockito.Mockito;
6162
import org.mockito.invocation.InvocationOnMock;
6263
import org.mockito.stubbing.Answer;
@@ -82,11 +83,13 @@ public class TestPread {
8283
LoggerFactory.getLogger(TestPread.class.getName());
8384
private final GenericTestUtils.LogCapturer dfsClientLog =
8485
GenericTestUtils.LogCapturer.captureLogs(DFSClient.LOG);
85-
@BeforeClass
86+
87+
@BeforeAll
8688
public static void setLogLevel() {
8789
GenericTestUtils.setLogLevel(DFSClient.LOG, org.apache.log4j.Level.WARN);
8890
}
89-
@Before
91+
92+
@BeforeEach
9093
public void setup() {
9194
simulatedStorage = false;
9295
isHedgedRead = false;
@@ -107,10 +110,10 @@ private void writeFile(FileSystem fileSys, Path name) throws IOException {
107110
// should throw an exception
108111
res = e;
109112
}
110-
assertTrue("Error reading beyond file boundary.", res != null);
113+
assertTrue(res != null, "Error reading beyond file boundary.");
111114
in.close();
112115
if (!fileSys.delete(name, true))
113-
assertTrue("Cannot delete file", false);
116+
assertTrue(false, "Cannot delete file");
114117

115118
// now create the real file
116119
DFSTestUtil.createFile(fileSys, name, fileSize, fileSize,
@@ -119,9 +122,9 @@ private void writeFile(FileSystem fileSys, Path name) throws IOException {
119122

120123
private void checkAndEraseData(byte[] actual, int from, byte[] expected, String message) {
121124
for (int idx = 0; idx < actual.length; idx++) {
122-
assertEquals(message+" byte "+(from+idx)+" differs. expected "+
123-
expected[from+idx]+" actual "+actual[idx],
124-
actual[idx], expected[from+idx]);
125+
assertEquals(actual[idx], expected[from + idx],
126+
message + " byte " + (from + idx)
127+
+ " differs. expected " + expected[from + idx] + " actual " + actual[idx]);
125128
actual[idx] = 0;
126129
}
127130
}
@@ -140,17 +143,17 @@ private void doPread(FSDataInputStream stm, long position, byte[] buffer,
140143
while (nread < length) {
141144
int nbytes =
142145
stm.read(position + nread, buffer, offset + nread, length - nread);
143-
assertTrue("Error in pread", nbytes > 0);
146+
assertTrue(nbytes > 0, "Error in pread");
144147
nread += nbytes;
145148
}
146149

147150
if (dfstm != null) {
148151
if (isHedgedRead) {
149-
assertTrue("Expected read statistic to be incremented", length <= dfstm
150-
.getReadStatistics().getTotalBytesRead() - totalRead);
152+
assertTrue(length <= dfstm.getReadStatistics().getTotalBytesRead() - totalRead,
153+
"Expected read statistic to be incremented");
151154
} else {
152-
assertEquals("Expected read statistic to be incremented", length, dfstm
153-
.getReadStatistics().getTotalBytesRead() - totalRead);
155+
assertEquals(length, dfstm.getReadStatistics().getTotalBytesRead() - totalRead,
156+
"Expected read statistic to be incremented");
154157
}
155158
}
156159
}
@@ -221,7 +224,7 @@ private void pReadFile(FileSystem fileSys, Path name) throws IOException {
221224
// should throw an exception
222225
res = e;
223226
}
224-
assertTrue("Error reading beyond file boundary.", res != null);
227+
assertTrue(res != null, "Error reading beyond file boundary.");
225228

226229
stm.close();
227230
}
@@ -553,9 +556,9 @@ public Void call() throws IOException {
553556
});
554557
try {
555558
future.get(4, TimeUnit.SECONDS);
556-
Assert.fail();
559+
Assertions.fail();
557560
} catch (ExecutionException ee) {
558-
assertTrue(ee.toString(), ee.getCause() instanceof EOFException);
561+
assertTrue(ee.getCause() instanceof EOFException, ee.toString());
559562
} finally {
560563
future.cancel(true);
561564
executor.shutdown();
@@ -570,7 +573,8 @@ public Void call() throws IOException {
570573
* retrying on a different datanode or by refreshing data nodes and retrying each data node one
571574
* more time.
572575
*/
573-
@Test(timeout=120000)
576+
@Test
577+
@Timeout(value = 120)
574578
public void testGetFromOneDataNodeExceptionLogging() throws IOException {
575579
// With maxBlockAcquireFailures = 0, we would try on each datanode only once and if
576580
// we fail on all three datanodes, we fail the read request.
@@ -647,7 +651,8 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
647651
/**
648652
* Test the case where we always hit IOExceptions, causing the read request to fail.
649653
*/
650-
@Test(timeout=60000)
654+
@Test
655+
@Timeout(value = 60)
651656
public void testFetchFromDataNodeExceptionLoggingFailedRequest()
652657
throws IOException {
653658
testFetchFromDataNodeExceptionLoggingFailedRequest(0);
@@ -723,7 +728,8 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
723728
}
724729
}
725730

726-
@Test(timeout=30000)
731+
@Test
732+
@Timeout(value = 30)
727733
public void testHedgedReadFromAllDNFailed() throws IOException {
728734
Configuration conf = new Configuration();
729735
int numHedgedReadPoolThreads = 5;
@@ -768,7 +774,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
768774
byte[] buffer = new byte[64 * 1024];
769775
input = dfsClient.open(filename);
770776
input.read(0, buffer, 0, 1024);
771-
Assert.fail("Reading the block should have thrown BlockMissingException");
777+
Assertions.fail("Reading the block should have thrown BlockMissingException");
772778
} catch (BlockMissingException e) {
773779
// The result of 9 is due to 2 blocks by 4 iterations plus one because
774780
// hedgedReadOpsLoopNumForTesting is incremented at start of the loop.
@@ -808,7 +814,8 @@ public void testPreadFailureWithChangedBlockLocations() throws Exception {
808814
* 7. Consider next calls to getBlockLocations() always returns DN3 as last
809815
* location.<br>
810816
*/
811-
@Test(timeout = 60000)
817+
@Test
818+
@Timeout(value = 60)
812819
public void testPreadHedgedFailureWithChangedBlockLocations()
813820
throws Exception {
814821
isHedgedRead = true;
@@ -929,10 +936,10 @@ public Boolean get() {
929936
byte[] buf = new byte[1024];
930937
int n = din.read(0, buf, 0, data.length());
931938
assertEquals(data.length(), n);
932-
assertEquals("Data should be read", data, new String(buf, 0, n));
933-
assertTrue("Read should complete with maximum " + maxFailures
934-
+ " failures, but completed with " + din.failures,
935-
din.failures <= maxFailures);
939+
assertEquals(data, new String(buf, 0, n), "Data should be read");
940+
assertTrue(din.failures <= maxFailures,
941+
"Read should complete with maximum " + maxFailures
942+
+ " failures, but completed with " + din.failures);
936943
DFSClient.LOG.info("Read completed");
937944
}
938945
}

0 commit comments

Comments
 (0)