This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git
The following commit(s) were added to refs/heads/master by this push:
new 5528be0d1 RATIS-2303. Migrate ratis-examples tests to Junit 5. (#1267)
5528be0d1 is described below
commit 5528be0d13f7ac2f38672e2eaf60fd3a7a561043
Author: slfan1989 <[email protected]>
AuthorDate: Fri May 23 00:21:14 2025 +0800
RATIS-2303. Migrate ratis-examples tests to Junit 5. (#1267)
---
.../ratis/util/ReferenceCountedLeakDetector.java | 3 ++-
.../examples/filestore/FileStoreAsyncBaseTest.java | 2 +-
.../examples/filestore/FileStoreBaseTest.java | 30 +++++++++++-----------
.../filestore/FileStoreStreamingBaseTest.java | 12 ++++-----
.../ratis/examples/filestore/FileStoreWriter.java | 20 +++++++--------
.../ratis/server/impl/TestReadAfterWrite.java | 20 +++++++--------
.../ratis/netty/client/NettyClientStreamRpc.java | 5 ++--
.../ratis/datastream/DataStreamTestUtils.java | 2 +-
8 files changed, 47 insertions(+), 47 deletions(-)
diff --git
a/ratis-common/src/main/java/org/apache/ratis/util/ReferenceCountedLeakDetector.java
b/ratis-common/src/main/java/org/apache/ratis/util/ReferenceCountedLeakDetector.java
index 271058e54..330060a90 100644
---
a/ratis-common/src/main/java/org/apache/ratis/util/ReferenceCountedLeakDetector.java
+++
b/ratis-common/src/main/java/org/apache/ratis/util/ReferenceCountedLeakDetector.java
@@ -120,7 +120,7 @@ public final class ReferenceCountedLeakDetector {
if (count.getAndUpdate(n -> n < 0? n : n + 1) < 0) {
throw new IllegalStateException("Failed to retain: object has already
been completely released.");
}
-
+ LOG.trace("retain value : {}, count : {}.", value, count);
retainMethod.run();
return value;
}
@@ -136,6 +136,7 @@ public final class ReferenceCountedLeakDetector {
} else if (previous == 0) {
throw new IllegalStateException("Failed to release: object has not yet
been retained.");
}
+ LOG.trace("release value : {}, count : {}.", value, count);
final boolean completedReleased = previous == 1;
releaseMethod.accept(completedReleased);
return completedReleased;
diff --git
a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreAsyncBaseTest.java
b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreAsyncBaseTest.java
index 0b5071c44..ae3d7fc7b 100644
---
a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreAsyncBaseTest.java
+++
b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreAsyncBaseTest.java
@@ -25,7 +25,7 @@ import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.statemachine.StateMachine;
import org.apache.ratis.util.LogUtils;
import org.apache.ratis.util.SizeInBytes;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
diff --git
a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java
b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java
index 5bcc50039..07668e0f2 100644
---
a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java
+++
b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java
@@ -28,8 +28,8 @@ import org.apache.ratis.util.LogUtils;
import org.apache.ratis.util.SizeInBytes;
import org.apache.ratis.util.TimeDuration;
import org.apache.ratis.util.function.CheckedSupplier;
-import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -96,22 +96,22 @@ public abstract class FileStoreBaseTest<CLUSTER extends
MiniRaftCluster>
});
firstList.add(f);
final CompletableFuture<ReadReplyProto> s =
client.watchAsync(pathSecond + i).whenComplete((reply, e) -> {
- Assert.assertNotNull(reply);
- Assert.assertNull(e);
- Assert.assertTrue(isStarted.get());
+ Assertions.assertNotNull(reply);
+ Assertions.assertNull(e);
+ Assertions.assertTrue(isStarted.get());
completionOrder.add(index);
});
watchSecond.add(s);
- Assert.assertFalse(f.isDone());
- Assert.assertFalse(s.isDone());
- Assert.assertFalse(isStarted.get());
+ Assertions.assertFalse(f.isDone());
+ Assertions.assertFalse(s.isDone());
+ Assertions.assertFalse(isStarted.get());
}
TimeDuration.valueOf(ThreadLocalRandom.current().nextLong(500) + 100,
TimeUnit.MILLISECONDS)
.sleep(s -> LOG.info("{}", s));
-
firstList.stream().map(CompletableFuture::isDone).forEach(Assert::assertFalse);
-
watchSecond.stream().map(CompletableFuture::isDone).forEach(Assert::assertFalse);
- Assert.assertFalse(isStarted.get());
+
firstList.stream().map(CompletableFuture::isDone).forEach(Assertions::assertFalse);
+
watchSecond.stream().map(CompletableFuture::isDone).forEach(Assertions::assertFalse);
+ Assertions.assertFalse(isStarted.get());
isStarted.set(true);
for (int i : randomIndices) {
@@ -121,12 +121,12 @@ public abstract class FileStoreBaseTest<CLUSTER extends
MiniRaftCluster>
for (int i = 0; i < n; i++) {
final ReadReplyProto reply = watchSecond.get(i).get(100,
TimeUnit.MILLISECONDS);
LOG.info("reply {}: {}", i, reply);
- Assert.assertNotNull(reply);
- Assert.assertEquals(pathSecond + i,
reply.getResolvedPath().toStringUtf8());
+ Assertions.assertNotNull(reply);
+ Assertions.assertEquals(pathSecond + i,
reply.getResolvedPath().toStringUtf8());
}
LOG.info("completionOrder {}", completionOrder);
- Assert.assertEquals(randomIndices, completionOrder);
-
firstList.stream().map(CompletableFuture::isDone).forEach(Assert::assertFalse);
+ Assertions.assertEquals(randomIndices, completionOrder);
+
firstList.stream().map(CompletableFuture::isDone).forEach(Assertions::assertFalse);
}
}
diff --git
a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreStreamingBaseTest.java
b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreStreamingBaseTest.java
index 490f12c8d..cdcee0ef0 100644
---
a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreStreamingBaseTest.java
+++
b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreStreamingBaseTest.java
@@ -30,8 +30,8 @@ import org.apache.ratis.statemachine.StateMachine;
import org.apache.ratis.util.LogUtils;
import org.apache.ratis.util.SizeInBytes;
import org.apache.ratis.util.function.CheckedSupplier;
-import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -69,7 +69,7 @@ public abstract class FileStoreStreamingBaseTest <CLUSTER
extends MiniRaftCluste
final RaftGroup raftGroup = cluster.getGroup();
final Collection<RaftPeer> peers = raftGroup.getPeers();
- Assert.assertEquals(NUM_PEERS, peers.size());
+ Assertions.assertEquals(NUM_PEERS, peers.size());
RaftPeer primary = peers.iterator().next();
final CheckedSupplier<FileStoreClient, IOException> newClient =
@@ -91,7 +91,7 @@ public abstract class FileStoreStreamingBaseTest <CLUSTER
extends MiniRaftCluste
final RaftGroup raftGroup = cluster.getGroup();
final Collection<RaftPeer> peers = raftGroup.getPeers();
- Assert.assertEquals(NUM_PEERS, peers.size());
+ Assertions.assertEquals(NUM_PEERS, peers.size());
RaftPeer primary = peers.iterator().next();
final CheckedSupplier<FileStoreClient, IOException> newClient =
@@ -114,7 +114,7 @@ public abstract class FileStoreStreamingBaseTest <CLUSTER
extends MiniRaftCluste
.setFileSize(fileLength)
.setBufferSize(bufferSize)
.setFileStoreClientSupplier(newClient)
- .build().streamWriteAndVerify(routingTable);
+ .build().streamWriteAndVerify(routingTable).close();
}
private void testMultipleFiles(String pathBase, int numFile, SizeInBytes
fileLength,
@@ -136,7 +136,7 @@ public abstract class FileStoreStreamingBaseTest <CLUSTER
extends MiniRaftCluste
writerFutures.add(executor.submit(callable));
}
for (Future<FileStoreWriter> future : writerFutures) {
- future.get();
+ future.get().close();
}
}
}
diff --git
a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreWriter.java
b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreWriter.java
index c2b52fa63..c0f7d0809 100644
---
a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreWriter.java
+++
b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreWriter.java
@@ -29,7 +29,7 @@ import org.apache.ratis.util.Preconditions;
import org.apache.ratis.util.SizeInBytes;
import org.apache.ratis.util.StringUtils;
import org.apache.ratis.util.function.CheckedSupplier;
-import org.junit.Assert;
+import org.junit.jupiter.api.Assertions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -129,7 +129,7 @@ final class FileStoreWriter implements Closeable {
LOG.trace("write {}, offset={}, length={}, close? {}",
fileName, offset, length, close);
final long written = client.write(fileName, offset, close, b, sync);
- Assert.assertEquals(length, written);
+ Assertions.assertEquals(length, written);
offset += length;
}
return this;
@@ -156,15 +156,15 @@ final class FileStoreWriter implements Closeable {
}
DataStreamReply reply = dataStreamOutput.closeAsync().join();
- Assert.assertTrue(reply.isSuccess());
+ Assertions.assertTrue(reply.isSuccess());
// TODO: handle when any of the writeAsync has failed.
// check writeAsync requests
for (int i = 0; i < futures.size(); i++) {
reply = futures.get(i).join();
- Assert.assertTrue(reply.isSuccess());
- Assert.assertEquals(sizes.get(i).longValue(), reply.getBytesWritten());
- Assert.assertEquals(reply.getType(),
RaftProtos.DataStreamPacketHeaderProto.Type.STREAM_DATA);
+ Assertions.assertTrue(reply.isSuccess());
+ Assertions.assertEquals(sizes.get(i).longValue(),
reply.getBytesWritten());
+ Assertions.assertEquals(reply.getType(),
RaftProtos.DataStreamPacketHeaderProto.Type.STREAM_DATA);
}
return this;
@@ -192,7 +192,7 @@ final class FileStoreWriter implements Closeable {
LOG.trace("writeAsync {}, offset={}, length={}, close? {}",
fileName, offset, length, close);
client.writeAsync(fileName, offset, close, b, sync)
- .thenAcceptAsync(written -> Assert.assertEquals(length,
(long)written), asyncExecutor)
+ .thenAcceptAsync(written -> Assertions.assertEquals(length,
(long)written), asyncExecutor)
.thenRun(() -> {
final int count = callCount.decrementAndGet();
LOG.trace("writeAsync {}, offset={}, length={}, close? {}: n={},
callCount={}",
@@ -256,12 +256,12 @@ final class FileStoreWriter implements Closeable {
return null;
});
}
- Assert.assertEquals(size, n.get());
+ Assertions.assertEquals(size, n.get());
return returnFuture;
}
void verify(ByteString read, int offset, int length, ByteBuffer expected) {
- Assert.assertEquals(length, read.size());
+ Assertions.assertEquals(length, read.size());
assertBuffers(offset, length, expected, read.asReadOnlyByteBuffer());
}
@@ -282,7 +282,7 @@ final class FileStoreWriter implements Closeable {
static void assertBuffers(int offset, int length, ByteBuffer expected,
ByteBuffer computed) {
try {
- Assert.assertEquals(expected, computed);
+ Assertions.assertEquals(expected, computed);
} catch(AssertionError e) {
LOG.error("Buffer mismatched at offset=" + offset + ", length=" + length
+ "\n expected = " + StringUtils.bytes2HexString(expected)
diff --git
a/ratis-examples/src/test/java/org/apache/ratis/server/impl/TestReadAfterWrite.java
b/ratis-examples/src/test/java/org/apache/ratis/server/impl/TestReadAfterWrite.java
index f515628c9..025c7ed0d 100644
---
a/ratis-examples/src/test/java/org/apache/ratis/server/impl/TestReadAfterWrite.java
+++
b/ratis-examples/src/test/java/org/apache/ratis/server/impl/TestReadAfterWrite.java
@@ -33,9 +33,9 @@ import org.apache.ratis.server.RaftServerConfigKeys;
import org.apache.ratis.statemachine.StateMachine;
import org.apache.ratis.util.CodeInjectionForTesting;
import org.apache.ratis.util.Slf4jUtils;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.slf4j.event.Level;
import java.util.concurrent.CompletableFuture;
@@ -48,7 +48,7 @@ public class TestReadAfterWrite
extends BaseTest
implements MiniRaftClusterWithGrpc.FactoryGet {
- @Before
+ @BeforeEach
public void setup() {
Slf4jUtils.setLogLevel(ArithmeticStateMachine.LOG, Level.DEBUG);
Slf4jUtils.setLogLevel(CodeInjectionForTesting.LOG, Level.DEBUG);
@@ -108,7 +108,7 @@ public class TestReadAfterWrite
final AsyncApi async = client.async();
final int initialValue = 10;
final RaftClientReply assign = async.send(a.assign(new
DoubleValue(initialValue))).join();
- Assert.assertTrue(assign.isSuccess());
+ Assertions.assertTrue(assign.isSuccess());
final Message query = Expression.Utils.toMessage(a);
assertReply(async.sendReadOnly(query), initialValue);
@@ -131,15 +131,15 @@ public class TestReadAfterWrite
final RaftClientReply reply = readAfterWrite.get(100,
TimeUnit.MILLISECONDS);
final DoubleValue result = (DoubleValue)
Expression.Utils.bytes2Expression(
reply.getMessage().getContent().toByteArray(), 0);
- Assert.fail("result=" + result + ", reply=" + reply);
+ Assertions.fail("result=" + result + ", reply=" + reply);
} catch (TimeoutException e) {
LOG.info("Good", e);
}
// plus2 should still be blocked.
- Assert.assertFalse(plus2.isDone());
+ Assertions.assertFalse(plus2.isDone());
// readAfterWrite should still be blocked.
- Assert.assertFalse(readAfterWrite.isDone());
+ Assertions.assertFalse(readAfterWrite.isDone());
// unblock plus2
blockingCode.complete();
@@ -151,10 +151,10 @@ public class TestReadAfterWrite
void assertReply(CompletableFuture<RaftClientReply> future, int expected) {
LOG.info("assertReply, expected {}", expected);
final RaftClientReply reply = future.join();
- Assert.assertTrue(reply.isSuccess());
+ Assertions.assertTrue(reply.isSuccess());
LOG.info("reply {}", reply);
final DoubleValue result = (DoubleValue) Expression.Utils.bytes2Expression(
reply.getMessage().getContent().toByteArray(), 0);
- Assert.assertEquals(expected, (int) (double) result.evaluate(null));
+ Assertions.assertEquals(expected, (int) (double) result.evaluate(null));
}
}
diff --git
a/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java
b/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java
index 534fcc581..5e111daff 100644
---
a/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java
+++
b/ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java
@@ -339,15 +339,14 @@ public class NettyClientStreamRpc implements
DataStreamClientRpc {
try {
replyMap.receiveReply(reply);
} catch (Throwable cause) {
- LOG.warn(name + ": channelRead error:", cause);
+ LOG.warn("{} : channelRead error:", name, cause);
replyMap.completeExceptionally(cause);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- LOG.warn(name + ": exceptionCaught", cause);
-
+ LOG.warn("{} : exceptionCaught", name, cause);
ctx.close();
}
diff --git
a/ratis-test/src/test/java/org/apache/ratis/datastream/DataStreamTestUtils.java
b/ratis-test/src/test/java/org/apache/ratis/datastream/DataStreamTestUtils.java
index 4909f436d..c2d26161d 100644
---
a/ratis-test/src/test/java/org/apache/ratis/datastream/DataStreamTestUtils.java
+++
b/ratis-test/src/test/java/org/apache/ratis/datastream/DataStreamTestUtils.java
@@ -154,7 +154,7 @@ public interface DataStreamTestUtils {
@Override
public CompletableFuture<DataStream> stream(RaftClientRequest request) {
final SingleDataStream s = new SingleDataStream(request);
- LOG.info("XXX {} put {}, {}", this, ClientInvocationId.valueOf(request),
s);
+ LOG.info("{} put {}, {}", this, ClientInvocationId.valueOf(request), s);
streams.put(ClientInvocationId.valueOf(request), s);
return CompletableFuture.completedFuture(s);
}