Skip to content

Commit 5528be0

Browse files
authored
RATIS-2303. Migrate ratis-examples tests to Junit 5. (#1267)
1 parent 689d31b commit 5528be0

File tree

8 files changed

+47
-47
lines changed

8 files changed

+47
-47
lines changed

ratis-common/src/main/java/org/apache/ratis/util/ReferenceCountedLeakDetector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public V retain() {
120120
if (count.getAndUpdate(n -> n < 0? n : n + 1) < 0) {
121121
throw new IllegalStateException("Failed to retain: object has already been completely released.");
122122
}
123-
123+
LOG.trace("retain value : {}, count : {}.", value, count);
124124
retainMethod.run();
125125
return value;
126126
}
@@ -136,6 +136,7 @@ public boolean release() {
136136
} else if (previous == 0) {
137137
throw new IllegalStateException("Failed to release: object has not yet been retained.");
138138
}
139+
LOG.trace("release value : {}, count : {}.", value, count);
139140
final boolean completedReleased = previous == 1;
140141
releaseMethod.accept(completedReleased);
141142
return completedReleased;

ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreAsyncBaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.apache.ratis.statemachine.StateMachine;
2626
import org.apache.ratis.util.LogUtils;
2727
import org.apache.ratis.util.SizeInBytes;
28-
import org.junit.Test;
28+
import org.junit.jupiter.api.Test;
2929
import org.slf4j.Logger;
3030
import org.slf4j.LoggerFactory;
3131

ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.apache.ratis.util.SizeInBytes;
2929
import org.apache.ratis.util.TimeDuration;
3030
import org.apache.ratis.util.function.CheckedSupplier;
31-
import org.junit.Assert;
32-
import org.junit.Test;
31+
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.Test;
3333
import org.slf4j.Logger;
3434
import org.slf4j.LoggerFactory;
3535

@@ -96,22 +96,22 @@ void runTestWatch(int n, CLUSTER cluster) throws Exception {
9696
});
9797
firstList.add(f);
9898
final CompletableFuture<ReadReplyProto> s = client.watchAsync(pathSecond + i).whenComplete((reply, e) -> {
99-
Assert.assertNotNull(reply);
100-
Assert.assertNull(e);
101-
Assert.assertTrue(isStarted.get());
99+
Assertions.assertNotNull(reply);
100+
Assertions.assertNull(e);
101+
Assertions.assertTrue(isStarted.get());
102102
completionOrder.add(index);
103103
});
104104
watchSecond.add(s);
105-
Assert.assertFalse(f.isDone());
106-
Assert.assertFalse(s.isDone());
107-
Assert.assertFalse(isStarted.get());
105+
Assertions.assertFalse(f.isDone());
106+
Assertions.assertFalse(s.isDone());
107+
Assertions.assertFalse(isStarted.get());
108108
}
109109

110110
TimeDuration.valueOf(ThreadLocalRandom.current().nextLong(500) + 100, TimeUnit.MILLISECONDS)
111111
.sleep(s -> LOG.info("{}", s));
112-
firstList.stream().map(CompletableFuture::isDone).forEach(Assert::assertFalse);
113-
watchSecond.stream().map(CompletableFuture::isDone).forEach(Assert::assertFalse);
114-
Assert.assertFalse(isStarted.get());
112+
firstList.stream().map(CompletableFuture::isDone).forEach(Assertions::assertFalse);
113+
watchSecond.stream().map(CompletableFuture::isDone).forEach(Assertions::assertFalse);
114+
Assertions.assertFalse(isStarted.get());
115115
isStarted.set(true);
116116

117117
for (int i : randomIndices) {
@@ -121,12 +121,12 @@ void runTestWatch(int n, CLUSTER cluster) throws Exception {
121121
for (int i = 0; i < n; i++) {
122122
final ReadReplyProto reply = watchSecond.get(i).get(100, TimeUnit.MILLISECONDS);
123123
LOG.info("reply {}: {}", i, reply);
124-
Assert.assertNotNull(reply);
125-
Assert.assertEquals(pathSecond + i, reply.getResolvedPath().toStringUtf8());
124+
Assertions.assertNotNull(reply);
125+
Assertions.assertEquals(pathSecond + i, reply.getResolvedPath().toStringUtf8());
126126
}
127127
LOG.info("completionOrder {}", completionOrder);
128-
Assert.assertEquals(randomIndices, completionOrder);
129-
firstList.stream().map(CompletableFuture::isDone).forEach(Assert::assertFalse);
128+
Assertions.assertEquals(randomIndices, completionOrder);
129+
firstList.stream().map(CompletableFuture::isDone).forEach(Assertions::assertFalse);
130130
}
131131
}
132132

ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreStreamingBaseTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import org.apache.ratis.util.LogUtils;
3131
import org.apache.ratis.util.SizeInBytes;
3232
import org.apache.ratis.util.function.CheckedSupplier;
33-
import org.junit.Assert;
34-
import org.junit.Test;
33+
import org.junit.jupiter.api.Assertions;
34+
import org.junit.jupiter.api.Test;
3535
import org.slf4j.Logger;
3636
import org.slf4j.LoggerFactory;
3737

@@ -69,7 +69,7 @@ public void testFileStoreStreamSingleFile() throws Exception {
6969

7070
final RaftGroup raftGroup = cluster.getGroup();
7171
final Collection<RaftPeer> peers = raftGroup.getPeers();
72-
Assert.assertEquals(NUM_PEERS, peers.size());
72+
Assertions.assertEquals(NUM_PEERS, peers.size());
7373
RaftPeer primary = peers.iterator().next();
7474

7575
final CheckedSupplier<FileStoreClient, IOException> newClient =
@@ -91,7 +91,7 @@ public void testFileStoreStreamMultipleFiles() throws Exception {
9191

9292
final RaftGroup raftGroup = cluster.getGroup();
9393
final Collection<RaftPeer> peers = raftGroup.getPeers();
94-
Assert.assertEquals(NUM_PEERS, peers.size());
94+
Assertions.assertEquals(NUM_PEERS, peers.size());
9595
RaftPeer primary = peers.iterator().next();
9696

9797
final CheckedSupplier<FileStoreClient, IOException> newClient =
@@ -114,7 +114,7 @@ private void testSingleFile(
114114
.setFileSize(fileLength)
115115
.setBufferSize(bufferSize)
116116
.setFileStoreClientSupplier(newClient)
117-
.build().streamWriteAndVerify(routingTable);
117+
.build().streamWriteAndVerify(routingTable).close();
118118
}
119119

120120
private void testMultipleFiles(String pathBase, int numFile, SizeInBytes fileLength,
@@ -136,7 +136,7 @@ private void testMultipleFiles(String pathBase, int numFile, SizeInBytes fileLen
136136
writerFutures.add(executor.submit(callable));
137137
}
138138
for (Future<FileStoreWriter> future : writerFutures) {
139-
future.get();
139+
future.get().close();
140140
}
141141
}
142142
}

ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreWriter.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.apache.ratis.util.SizeInBytes;
3030
import org.apache.ratis.util.StringUtils;
3131
import org.apache.ratis.util.function.CheckedSupplier;
32-
import org.junit.Assert;
32+
import org.junit.jupiter.api.Assertions;
3333
import org.slf4j.Logger;
3434
import org.slf4j.LoggerFactory;
3535

@@ -129,7 +129,7 @@ FileStoreWriter write(boolean sync) throws IOException {
129129
LOG.trace("write {}, offset={}, length={}, close? {}",
130130
fileName, offset, length, close);
131131
final long written = client.write(fileName, offset, close, b, sync);
132-
Assert.assertEquals(length, written);
132+
Assertions.assertEquals(length, written);
133133
offset += length;
134134
}
135135
return this;
@@ -156,15 +156,15 @@ public FileStoreWriter streamWriteAndVerify(RoutingTable routingTable) {
156156
}
157157

158158
DataStreamReply reply = dataStreamOutput.closeAsync().join();
159-
Assert.assertTrue(reply.isSuccess());
159+
Assertions.assertTrue(reply.isSuccess());
160160

161161
// TODO: handle when any of the writeAsync has failed.
162162
// check writeAsync requests
163163
for (int i = 0; i < futures.size(); i++) {
164164
reply = futures.get(i).join();
165-
Assert.assertTrue(reply.isSuccess());
166-
Assert.assertEquals(sizes.get(i).longValue(), reply.getBytesWritten());
167-
Assert.assertEquals(reply.getType(), RaftProtos.DataStreamPacketHeaderProto.Type.STREAM_DATA);
165+
Assertions.assertTrue(reply.isSuccess());
166+
Assertions.assertEquals(sizes.get(i).longValue(), reply.getBytesWritten());
167+
Assertions.assertEquals(reply.getType(), RaftProtos.DataStreamPacketHeaderProto.Type.STREAM_DATA);
168168
}
169169

170170
return this;
@@ -192,7 +192,7 @@ CompletableFuture<FileStoreWriter> writeAsync(boolean sync) {
192192
LOG.trace("writeAsync {}, offset={}, length={}, close? {}",
193193
fileName, offset, length, close);
194194
client.writeAsync(fileName, offset, close, b, sync)
195-
.thenAcceptAsync(written -> Assert.assertEquals(length, (long)written), asyncExecutor)
195+
.thenAcceptAsync(written -> Assertions.assertEquals(length, (long)written), asyncExecutor)
196196
.thenRun(() -> {
197197
final int count = callCount.decrementAndGet();
198198
LOG.trace("writeAsync {}, offset={}, length={}, close? {}: n={}, callCount={}",
@@ -256,12 +256,12 @@ CompletableFuture<FileStoreWriter> verifyAsync() {
256256
return null;
257257
});
258258
}
259-
Assert.assertEquals(size, n.get());
259+
Assertions.assertEquals(size, n.get());
260260
return returnFuture;
261261
}
262262

263263
void verify(ByteString read, int offset, int length, ByteBuffer expected) {
264-
Assert.assertEquals(length, read.size());
264+
Assertions.assertEquals(length, read.size());
265265
assertBuffers(offset, length, expected, read.asReadOnlyByteBuffer());
266266
}
267267

@@ -282,7 +282,7 @@ public void close() throws IOException {
282282

283283
static void assertBuffers(int offset, int length, ByteBuffer expected, ByteBuffer computed) {
284284
try {
285-
Assert.assertEquals(expected, computed);
285+
Assertions.assertEquals(expected, computed);
286286
} catch(AssertionError e) {
287287
LOG.error("Buffer mismatched at offset=" + offset + ", length=" + length
288288
+ "\n expected = " + StringUtils.bytes2HexString(expected)

ratis-examples/src/test/java/org/apache/ratis/server/impl/TestReadAfterWrite.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
import org.apache.ratis.statemachine.StateMachine;
3434
import org.apache.ratis.util.CodeInjectionForTesting;
3535
import org.apache.ratis.util.Slf4jUtils;
36-
import org.junit.Assert;
37-
import org.junit.Before;
38-
import org.junit.Test;
36+
import org.junit.jupiter.api.Assertions;
37+
import org.junit.jupiter.api.BeforeEach;
38+
import org.junit.jupiter.api.Test;
3939
import org.slf4j.event.Level;
4040

4141
import java.util.concurrent.CompletableFuture;
@@ -48,7 +48,7 @@ public class TestReadAfterWrite
4848
extends BaseTest
4949
implements MiniRaftClusterWithGrpc.FactoryGet {
5050

51-
@Before
51+
@BeforeEach
5252
public void setup() {
5353
Slf4jUtils.setLogLevel(ArithmeticStateMachine.LOG, Level.DEBUG);
5454
Slf4jUtils.setLogLevel(CodeInjectionForTesting.LOG, Level.DEBUG);
@@ -108,7 +108,7 @@ void runTestReadAfterWrite(RaftClient client) throws Exception {
108108
final AsyncApi async = client.async();
109109
final int initialValue = 10;
110110
final RaftClientReply assign = async.send(a.assign(new DoubleValue(initialValue))).join();
111-
Assert.assertTrue(assign.isSuccess());
111+
Assertions.assertTrue(assign.isSuccess());
112112

113113
final Message query = Expression.Utils.toMessage(a);
114114
assertReply(async.sendReadOnly(query), initialValue);
@@ -131,15 +131,15 @@ void runTestReadAfterWrite(RaftClient client) throws Exception {
131131
final RaftClientReply reply = readAfterWrite.get(100, TimeUnit.MILLISECONDS);
132132
final DoubleValue result = (DoubleValue) Expression.Utils.bytes2Expression(
133133
reply.getMessage().getContent().toByteArray(), 0);
134-
Assert.fail("result=" + result + ", reply=" + reply);
134+
Assertions.fail("result=" + result + ", reply=" + reply);
135135
} catch (TimeoutException e) {
136136
LOG.info("Good", e);
137137
}
138138

139139
// plus2 should still be blocked.
140-
Assert.assertFalse(plus2.isDone());
140+
Assertions.assertFalse(plus2.isDone());
141141
// readAfterWrite should still be blocked.
142-
Assert.assertFalse(readAfterWrite.isDone());
142+
Assertions.assertFalse(readAfterWrite.isDone());
143143

144144
// unblock plus2
145145
blockingCode.complete();
@@ -151,10 +151,10 @@ void runTestReadAfterWrite(RaftClient client) throws Exception {
151151
void assertReply(CompletableFuture<RaftClientReply> future, int expected) {
152152
LOG.info("assertReply, expected {}", expected);
153153
final RaftClientReply reply = future.join();
154-
Assert.assertTrue(reply.isSuccess());
154+
Assertions.assertTrue(reply.isSuccess());
155155
LOG.info("reply {}", reply);
156156
final DoubleValue result = (DoubleValue) Expression.Utils.bytes2Expression(
157157
reply.getMessage().getContent().toByteArray(), 0);
158-
Assert.assertEquals(expected, (int) (double) result.evaluate(null));
158+
Assertions.assertEquals(expected, (int) (double) result.evaluate(null));
159159
}
160160
}

ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,14 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
339339
try {
340340
replyMap.receiveReply(reply);
341341
} catch (Throwable cause) {
342-
LOG.warn(name + ": channelRead error:", cause);
342+
LOG.warn("{} : channelRead error:", name, cause);
343343
replyMap.completeExceptionally(cause);
344344
}
345345
}
346346

347347
@Override
348348
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
349-
LOG.warn(name + ": exceptionCaught", cause);
350-
349+
LOG.warn("{} : exceptionCaught", name, cause);
351350
ctx.close();
352351
}
353352

ratis-test/src/test/java/org/apache/ratis/datastream/DataStreamTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class MultiDataStreamStateMachine extends BaseStateMachine {
154154
@Override
155155
public CompletableFuture<DataStream> stream(RaftClientRequest request) {
156156
final SingleDataStream s = new SingleDataStream(request);
157-
LOG.info("XXX {} put {}, {}", this, ClientInvocationId.valueOf(request), s);
157+
LOG.info("{} put {}, {}", this, ClientInvocationId.valueOf(request), s);
158158
streams.put(ClientInvocationId.valueOf(request), s);
159159
return CompletableFuture.completedFuture(s);
160160
}

0 commit comments

Comments
 (0)