Skip to content

Commit

Permalink
Migrate tests in hdds to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
hemantk-12 committed Nov 22, 2023
1 parent 54a5985 commit 54915eb
Show file tree
Hide file tree
Showing 49 changed files with 756 additions and 803 deletions.
4 changes: 4 additions & 0 deletions hadoop-hdds/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
<name>Apache Ozone HDDS Client</name>
<packaging>jar</packaging>

<properties>
<allow.junit4>false</allow.junit4>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.ozone</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@
import org.apache.hadoop.hdds.client.ReplicationConfig;
import org.apache.hadoop.hdds.scm.pipeline.Pipeline;
import org.apache.hadoop.hdds.scm.pipeline.PipelineID;
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.mockito.Mockito;

import java.util.Collections;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Test ContainerClientMetrics.
*/
public class TestContainerClientMetrics {
@Before
@BeforeEach
public void setup() {
while (ContainerClientMetrics.referenceCount > 0) {
ContainerClientMetrics.release();
Expand Down Expand Up @@ -81,27 +82,27 @@ public void testRecordChunkMetrics() {
metrics.getWriteChunksCallsByLeaders().get(leaderId2).value());
}

@Test(expected = IllegalStateException.class)
@Test
public void testReleaseWithoutUse() {
ContainerClientMetrics.release();
assertThrows(IllegalStateException.class, ContainerClientMetrics::release);
}

@Test
public void testAcquireAndRelease() {
Assertions.assertNotNull(ContainerClientMetrics.acquire());
Assertions.assertEquals(1, ContainerClientMetrics.referenceCount);
assertNotNull(ContainerClientMetrics.acquire());
assertEquals(1, ContainerClientMetrics.referenceCount);
ContainerClientMetrics.release();
Assertions.assertEquals(0, ContainerClientMetrics.referenceCount);
assertEquals(0, ContainerClientMetrics.referenceCount);

Assertions.assertNotNull(ContainerClientMetrics.acquire());
Assertions.assertNotNull(ContainerClientMetrics.acquire());
Assertions.assertEquals(2, ContainerClientMetrics.referenceCount);
assertNotNull(ContainerClientMetrics.acquire());
assertNotNull(ContainerClientMetrics.acquire());
assertEquals(2, ContainerClientMetrics.referenceCount);
ContainerClientMetrics.release();
ContainerClientMetrics.release();
Assertions.assertEquals(0, ContainerClientMetrics.referenceCount);
assertEquals(0, ContainerClientMetrics.referenceCount);

ContainerClientMetrics.acquire();
Assertions.assertNotNull(ContainerClientMetrics.acquire());
assertNotNull(ContainerClientMetrics.acquire());
}

private Pipeline createPipeline(PipelineID piplineId, UUID leaderId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.apache.hadoop.ozone.common.OzoneChecksumException;
import org.apache.ratis.thirdparty.io.grpc.Status;
import org.apache.ratis.thirdparty.io.grpc.StatusException;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -62,6 +61,10 @@

import static org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.Result.CONTAINER_NOT_FOUND;
import static org.apache.hadoop.hdds.scm.storage.TestChunkInputStream.generateRandomData;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -137,8 +140,8 @@ private void createChunkList(int numChunks)

private void seekAndVerify(int pos) throws Exception {
blockStream.seek(pos);
Assert.assertEquals("Current position of buffer does not match with the " +
"seeked position", pos, blockStream.getPos());
assertEquals(pos, blockStream.getPos(),
"Current position of buffer does not match with the sought position");
}

/**
Expand All @@ -152,7 +155,7 @@ private void seekAndVerify(int pos) throws Exception {
private void matchWithInputData(byte[] readData, int inputDataStartIndex,
int length) {
for (int i = inputDataStartIndex; i < inputDataStartIndex + length; i++) {
Assert.assertEquals(blockData[i], readData[i - inputDataStartIndex]);
assertEquals(blockData[i], readData[i - inputDataStartIndex]);
}
}

Expand All @@ -161,36 +164,27 @@ public void testSeek() throws Exception {
// Seek to position 0
int pos = 0;
seekAndVerify(pos);
Assert.assertEquals("ChunkIndex is incorrect", 0,
blockStream.getChunkIndex());
assertEquals(0, blockStream.getChunkIndex(), "ChunkIndex is incorrect");

// Before BlockInputStream is initialized (initialization happens during
// read operation), seek should update the BlockInputStream#blockPosition
pos = CHUNK_SIZE;
seekAndVerify(pos);
Assert.assertEquals("ChunkIndex is incorrect", 0,
blockStream.getChunkIndex());
Assert.assertEquals(pos, blockStream.getBlockPosition());
assertEquals(0, blockStream.getChunkIndex(), "ChunkIndex is incorrect");
assertEquals(pos, blockStream.getBlockPosition());

// Initialize the BlockInputStream. After initializtion, the chunkIndex
// should be updated to correspond to the seeked position.
// Initialize the BlockInputStream. After initialization, the chunkIndex
// should be updated to correspond to the sought position.
blockStream.initialize();
Assert.assertEquals("ChunkIndex is incorrect", 1,
blockStream.getChunkIndex());
assertEquals(1, blockStream.getChunkIndex(), "ChunkIndex is incorrect");

pos = (CHUNK_SIZE * 4) + 5;
seekAndVerify(pos);
Assert.assertEquals("ChunkIndex is incorrect", 4,
blockStream.getChunkIndex());
assertEquals(4, blockStream.getChunkIndex(), "ChunkIndex is incorrect");
pos = blockSize + 10;

try {
// Try seeking beyond the blockSize.
pos = blockSize + 10;
seekAndVerify(pos);
Assert.fail("Seek to position beyond block size should fail.");
} catch (EOFException e) {
System.out.println(e);
}
int finalPos = pos;
assertThrows(EOFException.class, () -> seekAndVerify(finalPos));

// Seek to random positions between 0 and the block size.
Random random = new Random();
Expand All @@ -212,8 +206,8 @@ public void testRead() throws Exception {

// The new position of the blockInputStream should be the last index read
// + 1.
Assert.assertEquals(250, blockStream.getPos());
Assert.assertEquals(2, blockStream.getChunkIndex());
assertEquals(250, blockStream.getPos());
assertEquals(2, blockStream.getChunkIndex());
}

@Test
Expand All @@ -228,8 +222,8 @@ public void testReadWithByteBuffer() throws Exception {

// The new position of the blockInputStream should be the last index read
// + 1.
Assert.assertEquals(250, blockStream.getPos());
Assert.assertEquals(2, blockStream.getChunkIndex());
assertEquals(250, blockStream.getPos());
assertEquals(2, blockStream.getChunkIndex());
}

@Test
Expand All @@ -241,13 +235,13 @@ public void testReadWithDirectByteBuffer() throws Exception {
ByteBuffer buffer = ByteBuffer.allocateDirect(200);
blockStream.read(buffer);
for (int i = 50; i < 50 + 200; i++) {
Assert.assertEquals(blockData[i], buffer.get(i - 50));
assertEquals(blockData[i], buffer.get(i - 50));
}

// The new position of the blockInputStream should be the last index read
// + 1.
Assert.assertEquals(250, blockStream.getPos());
Assert.assertEquals(2, blockStream.getChunkIndex());
assertEquals(250, blockStream.getPos());
assertEquals(2, blockStream.getChunkIndex());
}

@Test
Expand All @@ -269,19 +263,16 @@ public void testRefreshPipelineFunction() throws Exception {
BlockID blockID = new BlockID(new ContainerBlockID(1, 1));
AtomicBoolean isRefreshed = new AtomicBoolean();
createChunkList(5);
BlockInputStream blockInputStreamWithRetry =
new DummyBlockInputStreamWithRetry(blockID, blockSize,
MockPipeline.createSingleNodePipeline(), null,
false, null, chunks, chunkDataMap, isRefreshed, null);

try {
Assert.assertFalse(isRefreshed.get());
try (BlockInputStream blockInputStreamWithRetry =
new DummyBlockInputStreamWithRetry(blockID, blockSize,
MockPipeline.createSingleNodePipeline(), null,
false, null, chunks, chunkDataMap, isRefreshed, null)) {
assertFalse(isRefreshed.get());
seekAndVerify(50);
byte[] b = new byte[200];
blockInputStreamWithRetry.read(b, 0, 200);
Assert.assertTrue(isRefreshed.get());
} finally {
blockInputStreamWithRetry.close();
assertTrue(isRefreshed.get());
}
}

Expand Down Expand Up @@ -329,7 +320,7 @@ private void testRefreshesPipelineOnReadFailure(IOException ex,
int bytesRead = subject.read(b, 0, len);

// THEN
Assert.assertEquals(len, bytesRead);
assertEquals(len, bytesRead);
verify(this.refreshFunction).apply(blockID);
} finally {
reset(this.refreshFunction);
Expand Down Expand Up @@ -442,7 +433,7 @@ protected ChunkInputStream createChunkInputStream(ChunkInfo chunkInfo) {
int bytesRead = subject.read(b, 0, len);

// THEN
Assert.assertEquals(len, bytesRead);
assertEquals(len, bytesRead);
verify(refreshFunction).apply(blockID);
verify(clientFactory).acquireClientForReadData(pipeline);
verify(clientFactory).releaseClientForReadData(client, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.hadoop.hdds.client.BlockID;
Expand All @@ -45,10 +43,11 @@
import org.apache.hadoop.hdds.scm.pipeline.Pipeline;

import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

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

/**
* UNIT test for BlockOutputStream.
* <p>
Expand Down Expand Up @@ -105,15 +104,14 @@ private BlockOutputStream createBlockOutputStream(BufferPool bufferPool)
config.setChecksumType(ChecksumType.NONE);
config.setBytesPerChecksum(256 * 1024);

BlockOutputStream outputStream = new RatisBlockOutputStream(
return new RatisBlockOutputStream(
new BlockID(1L, 1L),
xcm,
pipeline,
bufferPool,
config,
null,
ContainerClientMetrics.acquire());
return outputStream;
}

/**
Expand All @@ -123,22 +121,22 @@ private static class MockXceiverClientSpi extends XceiverClientSpi {

private final Pipeline pipeline;

private Random expectedRandomStream = new Random(SEED);
private final Random expectedRandomStream = new Random(SEED);

private AtomicInteger counter = new AtomicInteger();
private final AtomicInteger counter = new AtomicInteger();

MockXceiverClientSpi(Pipeline pipeline) {
super();
this.pipeline = pipeline;
}

@Override
public void connect() throws Exception {
public void connect() {

}

@Override
public void connect(String encodedToken) throws Exception {
public void connect(String encodedToken) {

}

Expand All @@ -155,9 +153,7 @@ public Pipeline getPipeline() {
@Override
public XceiverClientReply sendCommandAsync(
ContainerCommandRequestProto request
)
throws IOException, ExecutionException, InterruptedException {

) {
final ContainerCommandResponseProto.Builder builder =
ContainerCommandResponseProto.newBuilder()
.setResult(Result.SUCCESS)
Expand All @@ -178,10 +174,9 @@ public XceiverClientReply sendCommandAsync(
case WriteChunk:
ByteString data = request.getWriteChunk().getData();
final byte[] writePayload = data.toByteArray();
for (int i = 0; i < writePayload.length; i++) {
for (byte b : writePayload) {
byte expectedByte = (byte) expectedRandomStream.nextInt();
Assert.assertEquals(expectedByte,
writePayload[i]);
assertEquals(expectedByte, b);
}
break;
default:
Expand All @@ -200,9 +195,7 @@ public ReplicationType getPipelineType() {
}

@Override
public XceiverClientReply watchForCommit(long index)
throws InterruptedException, ExecutionException, TimeoutException,
IOException {
public XceiverClientReply watchForCommit(long index) {
final ContainerCommandResponseProto.Builder builder =
ContainerCommandResponseProto.newBuilder()
.setCmdType(Type.WriteChunk)
Expand All @@ -220,8 +213,7 @@ public long getReplicatedMinCommitIndex() {

@Override
public Map<DatanodeDetails, ContainerCommandResponseProto>
sendCommandOnAllNodes(ContainerCommandRequestProto request
) throws IOException, InterruptedException {
sendCommandOnAllNodes(ContainerCommandRequestProto request) {
return null;
}
}
Expand Down
Loading

0 comments on commit 54915eb

Please sign in to comment.