Skip to content

Commit

Permalink
Migrate codec-redis tests to JUnit 5 (netty#11318)
Browse files Browse the repository at this point in the history
Motivation:

JUnit 5 is more expressive, extensible, and composable in many ways, and it's better able to run tests in parallel.

Modifications:

Use JUnit5 in tests

Result:

Related to netty#10757
  • Loading branch information
kashike authored and normanmaurer committed May 27, 2021
1 parent 3efd964 commit 84826a4
Showing 1 changed file with 49 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@
import io.netty.handler.codec.DecoderException;
import io.netty.util.IllegalReferenceCountException;
import io.netty.util.ReferenceCountUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.util.List;

import static io.netty.handler.codec.redis.RedisCodecTestUtil.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Verifies the correct functionality of the {@link RedisDecoder} and {@link RedisArrayAggregator}.
Expand All @@ -40,7 +44,7 @@ public class RedisDecoderTest {

private EmbeddedChannel channel;

@Before
@BeforeEach
public void setup() throws Exception {
channel = newChannel(false);
}
Expand All @@ -52,7 +56,7 @@ private static EmbeddedChannel newChannel(boolean decodeInlineCommands) {
new RedisArrayAggregator());
}

@After
@AfterEach
public void teardown() throws Exception {
assertFalse(channel.finish());
}
Expand All @@ -67,15 +71,20 @@ public void splitEOLDoesNotInfiniteLoop() throws Exception {
ReferenceCountUtil.release(msg);
}

@Test(expected = DecoderException.class)
@Test
public void shouldNotDecodeInlineCommandByDefault() {
assertFalse(channel.writeInbound(byteBufOf("P")));
assertFalse(channel.writeInbound(byteBufOf("I")));
assertFalse(channel.writeInbound(byteBufOf("N")));
assertFalse(channel.writeInbound(byteBufOf("G")));
assertTrue(channel.writeInbound(byteBufOf("\r\n")));

channel.readInbound();
assertThrows(DecoderException.class, new Executable() {
@Override
public void execute() {
assertFalse(channel.writeInbound(byteBufOf("P")));
assertFalse(channel.writeInbound(byteBufOf("I")));
assertFalse(channel.writeInbound(byteBufOf("N")));
assertFalse(channel.writeInbound(byteBufOf("G")));
assertTrue(channel.writeInbound(byteBufOf("\r\n")));

channel.readInbound();
}
});
}

@Test
Expand Down Expand Up @@ -264,22 +273,27 @@ public void shouldDecodeNestedArray() throws Exception {
ReferenceCountUtil.release(msg);
}

@Test(expected = IllegalReferenceCountException.class)
public void shouldErrorOnDoubleReleaseArrayReferenceCounted() throws Exception {
@Test
public void shouldErrorOnDoubleReleaseArrayReferenceCounted() {
ByteBuf buf = Unpooled.buffer();
buf.writeBytes(byteBufOf("*2\r\n"));
buf.writeBytes(byteBufOf("*3\r\n:1\r\n:2\r\n:3\r\n"));
buf.writeBytes(byteBufOf("*2\r\n+Foo\r\n-Bar\r\n"));
assertTrue(channel.writeInbound(buf));

ArrayRedisMessage msg = channel.readInbound();
final ArrayRedisMessage msg = channel.readInbound();

ReferenceCountUtil.release(msg);
ReferenceCountUtil.release(msg);
assertThrows(IllegalReferenceCountException.class, new Executable() {
@Override
public void execute() {
ReferenceCountUtil.release(msg);
}
});
}

@Test(expected = IllegalReferenceCountException.class)
public void shouldErrorOnReleaseArrayChildReferenceCounted() throws Exception {
@Test
public void shouldErrorOnReleaseArrayChildReferenceCounted() {
ByteBuf buf = Unpooled.buffer();
buf.writeBytes(byteBufOf("*2\r\n"));
buf.writeBytes(byteBufOf("*3\r\n:1\r\n:2\r\n:3\r\n"));
Expand All @@ -288,12 +302,17 @@ public void shouldErrorOnReleaseArrayChildReferenceCounted() throws Exception {

ArrayRedisMessage msg = channel.readInbound();

List<RedisMessage> children = msg.children();
final List<RedisMessage> children = msg.children();
ReferenceCountUtil.release(msg);
ReferenceCountUtil.release(children.get(1));
assertThrows(IllegalReferenceCountException.class, new Executable() {
@Override
public void execute() {
ReferenceCountUtil.release(children.get(1));
}
});
}

@Test(expected = IllegalReferenceCountException.class)
@Test
public void shouldErrorOnReleasecontentOfArrayChildReferenceCounted() throws Exception {
ByteBuf buf = Unpooled.buffer();
buf.writeBytes(byteBufOf("*2\r\n"));
Expand All @@ -303,9 +322,14 @@ public void shouldErrorOnReleasecontentOfArrayChildReferenceCounted() throws Exc
ArrayRedisMessage msg = channel.readInbound();

List<RedisMessage> children = msg.children();
ByteBuf childBuf = ((FullBulkStringRedisMessage) children.get(0)).content();
final ByteBuf childBuf = ((FullBulkStringRedisMessage) children.get(0)).content();
ReferenceCountUtil.release(msg);
ReferenceCountUtil.release(childBuf);
assertThrows(IllegalReferenceCountException.class, new Executable() {
@Override
public void execute() {
ReferenceCountUtil.release(childBuf);
}
});
}

@Test
Expand Down

0 comments on commit 84826a4

Please sign in to comment.