-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
plugins { | ||
id("java-conventions") | ||
} | ||
|
||
dependencies { | ||
implementation(project(":core")) | ||
|
||
implementation(platform(libs.netty.bom)) | ||
implementation("io.netty:netty-all") | ||
|
||
testImplementation(platform(libs.junit.bom)) | ||
testImplementation("org.junit.jupiter:junit-jupiter") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
|
||
testLogging { | ||
showStandardStreams = true | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
chapter09-unit-testing/src/main/java/com/joebrothers/chapter09/AbsIntegerEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.joebrothers.chapter09; | ||
|
||
import java.util.List; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.MessageToMessageEncoder; | ||
|
||
public class AbsIntegerEncoder extends MessageToMessageEncoder<ByteBuf> { | ||
@Override | ||
protected void encode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) | ||
throws Exception { | ||
while (in.readableBytes() >= 4) { | ||
final int value = Math.abs(in.readInt()); | ||
out.add(value); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
chapter09-unit-testing/src/main/java/com/joebrothers/chapter09/FixedLengthFrameDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.joebrothers.chapter09; | ||
|
||
import java.util.List; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.ByteToMessageDecoder; | ||
|
||
public class FixedLengthFrameDecoder extends ByteToMessageDecoder { | ||
|
||
private final int frameLength; | ||
|
||
public FixedLengthFrameDecoder(int frameLength) { | ||
if (frameLength <= 0) { | ||
throw new IllegalArgumentException("frameLength must be a positive integer: " + frameLength); | ||
} | ||
|
||
this.frameLength = frameLength; | ||
} | ||
|
||
@Override | ||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) | ||
throws Exception { | ||
while (in.readableBytes() >= frameLength) { | ||
final ByteBuf buf = in.readBytes(frameLength); | ||
out.add(buf); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
chapter09-unit-testing/src/main/java/com/joebrothers/chapter09/FrameChunkDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.joebrothers.chapter09; | ||
|
||
import java.util.List; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.ByteToMessageDecoder; | ||
import io.netty.handler.codec.TooLongFrameException; | ||
|
||
public class FrameChunkDecoder extends ByteToMessageDecoder { | ||
|
||
private final int maxFrameSize; | ||
|
||
public FrameChunkDecoder(int maxFrameSize) { | ||
if (maxFrameSize <= 0) { | ||
throw new IllegalArgumentException("maxFrameSize must be a positive number: " + maxFrameSize); | ||
} | ||
|
||
this.maxFrameSize = maxFrameSize; | ||
} | ||
|
||
@Override | ||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) | ||
throws Exception { | ||
final int readableBytes = in.readableBytes(); | ||
if (readableBytes > maxFrameSize) { | ||
// discard | ||
in.clear(); | ||
throw new TooLongFrameException(); | ||
} | ||
final ByteBuf buf = in.readBytes(readableBytes); | ||
out.add(buf); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
chapter09-unit-testing/src/test/java/com/joebrothers/chapter09/AbsIntegerEncoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.joebrothers.chapter09; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.embedded.EmbeddedChannel; | ||
|
||
class AbsIntegerEncoderTest { | ||
|
||
@Test | ||
void testEncode() { | ||
final ByteBuf buf = Unpooled.buffer(); | ||
for (int i = 1; i < 10; i++) { | ||
buf.writeInt(i * -1); | ||
} | ||
|
||
final EmbeddedChannel channel = new EmbeddedChannel(new AbsIntegerEncoder()); | ||
|
||
// Write | ||
assertTrue(channel.writeOutbound(buf)); | ||
assertTrue(channel.finish()); | ||
|
||
// Read | ||
for (int i = 1; i < 10; i++) { | ||
assertEquals(i, (int) channel.readOutbound()); | ||
} | ||
assertNull(channel.readOutbound()); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...r09-unit-testing/src/test/java/com/joebrothers/chapter09/FixedLengthFrameDecoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.joebrothers.chapter09; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.embedded.EmbeddedChannel; | ||
|
||
class FixedLengthFrameDecoderTest { | ||
|
||
@Test | ||
public void testFramesDecoded() { | ||
final ByteBuf buf = Unpooled.buffer(); | ||
for (int i = 0; i < 9; i++) { | ||
buf.writeByte(i); | ||
} | ||
|
||
final ByteBuf input = buf.duplicate(); | ||
final EmbeddedChannel channel = new EmbeddedChannel(new FixedLengthFrameDecoder(3)); | ||
|
||
// Write bytes | ||
assertTrue(channel.writeInbound(input.retain())); // retain! | ||
assertTrue(channel.finish()); | ||
|
||
// Read messages | ||
ByteBuf read; | ||
for (int i=0; i<3; i++) { | ||
read = channel.readInbound(); | ||
assertEquals(buf.readSlice(3), read); | ||
read.release(); | ||
} | ||
|
||
assertNull(channel.readInbound()); | ||
buf.release(); | ||
} | ||
|
||
@Test | ||
public void testFramesDecoded2() { | ||
final ByteBuf buf = Unpooled.buffer(); | ||
for (int i=0; i<9; i++) { | ||
buf.writeByte(i); | ||
} | ||
|
||
final ByteBuf input = buf.duplicate(); | ||
final EmbeddedChannel channel = new EmbeddedChannel(new FixedLengthFrameDecoder(3)); | ||
assertFalse(channel.writeInbound(input.readBytes(2))); // frame is not ready to be read! | ||
assertTrue(channel.writeInbound(input.readBytes(7))); | ||
assertTrue(channel.finish()); | ||
|
||
ByteBuf read; | ||
for (int i=0; i<3; i++) { | ||
read = channel.readInbound(); | ||
assertEquals(buf.readSlice(3), read); | ||
read.release(); | ||
} | ||
|
||
assertNull(channel.readInbound()); | ||
buf.release(); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
chapter09-unit-testing/src/test/java/com/joebrothers/chapter09/FrameChunkDecoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.joebrothers.chapter09; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.embedded.EmbeddedChannel; | ||
import io.netty.handler.codec.TooLongFrameException; | ||
|
||
class FrameChunkDecoderTest { | ||
|
||
@Test | ||
void testFramesDecoded() { | ||
final ByteBuf buf = Unpooled.buffer(); | ||
for (int i = 0; i < 9; i++) { | ||
buf.writeByte(i); | ||
} | ||
final ByteBuf input = buf.duplicate(); | ||
|
||
final EmbeddedChannel channel = new EmbeddedChannel(new FrameChunkDecoder(3)); | ||
assertTrue(channel.writeInbound(input.readBytes(2))); | ||
|
||
try { | ||
// bigger than max size | ||
channel.writeInbound(input.readBytes(4)); | ||
fail(); | ||
} catch (TooLongFrameException e) { | ||
// expected | ||
} | ||
|
||
assertTrue(channel.writeInbound(input.readBytes(3))); | ||
assertTrue(channel.finish()); | ||
|
||
// Read | ||
ByteBuf read; | ||
{ | ||
read = channel.readInbound(); | ||
assertEquals(buf.readSlice(2), read); | ||
read.release(); | ||
} | ||
{ | ||
read = channel.readInbound(); | ||
assertEquals(buf.skipBytes(4).readSlice(3), read); | ||
read.release(); | ||
} | ||
|
||
buf.release(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters