Skip to content

HDFS-17718. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-hdfs-client. #7563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions hadoop-hdfs-project/hadoop-hdfs-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,31 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
package org.apache.hadoop.fs;

import org.apache.hadoop.test.GenericTestUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.io.File;
import java.io.IOException;
Expand All @@ -35,15 +34,13 @@
/**
* Test of the URL stream handler factory.
*/
@Timeout(30)
public class TestUrlStreamHandlerFactory {

private static final int RUNS = 20;
private static final int THREADS = 10;
private static final int TASKS = 200;

@Rule
public Timeout globalTimeout = new Timeout(30000);

@Test
public void testConcurrency() throws Exception {
for (int i = 0; i < RUNS; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@

package org.apache.hadoop.fs;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
* Tests for <code>XAttr</code> objects.
*/
public class TestXAttr {
private static XAttr XATTR, XATTR1, XATTR2, XATTR3, XATTR4, XATTR5;

@BeforeClass
@BeforeAll
public static void setUp() throws Exception {
byte[] value = {0x31, 0x32, 0x33};
XATTR = new XAttr.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@

import org.apache.hadoop.hdfs.DFSOpsCountStatistics.OpType;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import org.junit.rules.ExpectedException;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -44,15 +41,16 @@
import java.util.concurrent.atomic.AtomicReference;

import static org.apache.hadoop.util.concurrent.HadoopExecutors.newFixedThreadPool;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* This tests basic operations of {@link DFSOpsCountStatistics} class.
*/
@Timeout(10)
public class TestDFSOpsCountStatistics {

private static final Logger LOG = LoggerFactory.getLogger(
Expand All @@ -63,12 +61,7 @@ public class TestDFSOpsCountStatistics {
new DFSOpsCountStatistics();
private final Map<OpType, AtomicLong> expectedOpsCountMap = new HashMap<>();

@Rule
public final Timeout globalTimeout = new Timeout(10 * 1000);
@Rule
public final ExpectedException exception = ExpectedException.none();

@Before
@BeforeEach
public void setup() {
for (OpType opType : OpType.values()) {
expectedOpsCountMap.put(opType, new AtomicLong());
Expand Down Expand Up @@ -178,7 +171,7 @@ public void run() {
startBlocker.countDown(); // all threads start making directories
allDone.await(); // wait until all threads are done

assertNull("Child failed with exception.", childError.get());
assertNull(childError.get(), "Child failed with exception.");
verifyStatistics();
} finally {
threadPool.shutdownNow();
Expand Down Expand Up @@ -207,9 +200,9 @@ private void verifyStatistics() {
for (OpType opType : OpType.values()) {
assertNotNull(expectedOpsCountMap.get(opType));
assertNotNull(statistics.getLong(opType.getSymbol()));
assertEquals("Not expected count for operation " + opType.getSymbol(),
expectedOpsCountMap.get(opType).longValue(),
statistics.getLong(opType.getSymbol()).longValue());
assertEquals(expectedOpsCountMap.get(opType).longValue(),
statistics.getLong(opType.getSymbol()).longValue(),
"Not expected count for operation " + opType.getSymbol());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
import java.util.Random;
import org.apache.hadoop.hdfs.protocol.datatransfer.PacketHeader;
import org.apache.hadoop.io.DataOutputBuffer;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.fail;

public class TestDFSPacket {
private static final int chunkSize = 512;
Expand Down Expand Up @@ -59,7 +60,7 @@ public static void assertArrayRegionsEqual(byte []buf1, int off1, byte []buf2,
int off2, int len) {
for (int i = 0; i < len; i++) {
if (buf1[off1 + i] != buf2[off2 + i]) {
Assert.fail("arrays differ at byte " + i + ". " +
fail("arrays differ at byte " + i + ". " +
"The first array has " + (int) buf1[off1 + i] +
", but the second array has " + (int) buf2[off2 + i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.net.InetSocketAddress;
import java.net.URI;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

/** Test NameNode port defaulting code. */
public class TestDefaultNameNodePort {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.hadoop.hdfs.net.Peer;
import org.apache.hadoop.hdfs.protocol.DatanodeID;
import org.apache.hadoop.net.unix.DomainSocket;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
Expand All @@ -33,9 +33,9 @@
import java.io.OutputStream;
import java.nio.channels.ReadableByteChannel;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestPeerCache {
static final Logger LOG = LoggerFactory.getLogger(TestPeerCache.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.util.Time;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
Expand All @@ -37,7 +36,12 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import static org.junit.Assert.assertSame;
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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class TestLeaseRenewer {
private final String FAKE_AUTHORITY="hdfs://nn1/";
Expand All @@ -54,7 +58,7 @@ public class TestLeaseRenewer {
/** Cause renewals often so test runs quickly. */
private static final long FAST_GRACE_PERIOD = 100L;

@Before
@BeforeEach
public void setupMocksAndRenewer() throws IOException {
MOCK_DFSCLIENT = createMockClient();

Expand Down Expand Up @@ -82,19 +86,19 @@ public void testInstanceSharing() throws IOException {
FAKE_AUTHORITY, FAKE_UGI_A, MOCK_DFSCLIENT);
LeaseRenewer lr2 = LeaseRenewer.getInstance(
FAKE_AUTHORITY, FAKE_UGI_A, MOCK_DFSCLIENT);
Assert.assertSame(lr, lr2);
assertSame(lr, lr2);

// But a different UGI should return a different instance
LeaseRenewer lr3 = LeaseRenewer.getInstance(
FAKE_AUTHORITY, FAKE_UGI_B, MOCK_DFSCLIENT);
Assert.assertNotSame(lr, lr3);
assertNotSame(lr, lr3);

// A different authority with same UGI should also be a different
// instance.
LeaseRenewer lr4 = LeaseRenewer.getInstance(
"someOtherAuthority", FAKE_UGI_B, MOCK_DFSCLIENT);
Assert.assertNotSame(lr, lr4);
Assert.assertNotSame(lr3, lr4);
assertNotSame(lr, lr4);
assertNotSame(lr3, lr4);
}

@Test
Expand Down Expand Up @@ -122,7 +126,7 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
Thread.sleep(50);
}
if (leaseRenewalCount.get() == 0) {
Assert.fail("Did not renew lease at all!");
fail("Did not renew lease at all!");
}

renewer.closeClient(MOCK_DFSCLIENT);
Expand Down Expand Up @@ -176,23 +180,21 @@ public Boolean get() {

// Make sure renewer is not running due to expiration.
Thread.sleep(FAST_GRACE_PERIOD * 2);
Assert.assertTrue(!renewer.isRunning());
assertTrue(!renewer.isRunning());
}

@Test
public void testThreadName() throws Exception {
Assert.assertFalse("Renewer not initially running",
renewer.isRunning());
assertFalse(renewer.isRunning(), "Renewer not initially running");

// Pretend to open a file
renewer.put(MOCK_DFSCLIENT);

Assert.assertTrue("Renewer should have started running",
renewer.isRunning());
assertTrue(renewer.isRunning(), "Renewer should have started running");

// Check the thread name is reasonable
String threadName = renewer.getDaemonName();
Assert.assertEquals("LeaseRenewer:myuser@hdfs://nn1/", threadName);
assertEquals("LeaseRenewer:myuser@hdfs://nn1/", threadName);

// Pretend to close the file
renewer.closeClient(MOCK_DFSCLIENT);
Expand All @@ -203,7 +205,7 @@ public void testThreadName() throws Exception {
while (renewer.isRunning() && Time.monotonicNow() < failTime) {
Thread.sleep(50);
}
Assert.assertFalse(renewer.isRunning());
assertFalse(renewer.isRunning());
}

/**
Expand All @@ -213,24 +215,23 @@ public void testThreadName() throws Exception {
*/
@Test
public void testDaemonThreadLeak() throws Exception {
Assert.assertFalse("Renewer not initially running", renewer.isRunning());
assertFalse(renewer.isRunning(), "Renewer not initially running");

// Pretend to create a file#1, daemon#1 starts
renewer.put(MOCK_DFSCLIENT);
Assert.assertTrue("Renewer should have started running",
renewer.isRunning());
assertTrue(renewer.isRunning(), "Renewer should have started running");
Pattern daemonThreadNamePattern = Pattern.compile("LeaseRenewer:\\S+");
Assert.assertEquals(1, countThreadMatching(daemonThreadNamePattern));
assertEquals(1, countThreadMatching(daemonThreadNamePattern));

// Pretend to create file#2, daemon#2 starts due to expiration
LeaseRenewer lastRenewer = renewer;
renewer =
LeaseRenewer.getInstance(FAKE_AUTHORITY, FAKE_UGI_A, MOCK_DFSCLIENT);
Assert.assertEquals(lastRenewer, renewer);
assertEquals(lastRenewer, renewer);

// Pretend to close file#1
renewer.closeClient(MOCK_DFSCLIENT);
Assert.assertEquals(1, countThreadMatching(daemonThreadNamePattern));
assertEquals(1, countThreadMatching(daemonThreadNamePattern));

// Pretend to be expired
renewer.setEmptyTime(0);
Expand All @@ -249,7 +250,7 @@ public void testDaemonThreadLeak() throws Exception {

int threadCount = countThreadMatching(daemonThreadNamePattern);
//Sometimes old LR#Daemon gets closed and lead to count 1 (rare scenario)
Assert.assertTrue(1 == threadCount || 2 == threadCount);
assertTrue(1 == threadCount || 2 == threadCount);

// After grace period, both daemon#1 and renewer#1 will be removed due to
// expiration, then daemon#2 will leak before HDFS-14575.
Expand All @@ -259,14 +260,14 @@ public void testDaemonThreadLeak() throws Exception {
lastRenewer = renewer;
renewer =
LeaseRenewer.getInstance(FAKE_AUTHORITY, FAKE_UGI_A, MOCK_DFSCLIENT);
Assert.assertEquals(lastRenewer, renewer);
assertEquals(lastRenewer, renewer);
renewer.setGraceSleepPeriod(FAST_GRACE_PERIOD);
renewer.closeClient(MOCK_DFSCLIENT);
renewer.setEmptyTime(0);
// Make sure LeaseRenewer#daemon threads will terminate after grace period
Thread.sleep(FAST_GRACE_PERIOD * 2);
Assert.assertEquals("LeaseRenewer#daemon thread leaks", 0,
countThreadMatching(daemonThreadNamePattern));
assertEquals(0, countThreadMatching(daemonThreadNamePattern),
"LeaseRenewer#daemon thread leaks");
}

private static int countThreadMatching(Pattern pattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
*/
package org.apache.hadoop.hdfs.protocol;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.apache.hadoop.hdfs.protocol.BlockType.CONTIGUOUS;
import static org.apache.hadoop.hdfs.protocol.BlockType.STRIPED;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Test the BlockType class.
Expand Down
Loading