How to test Encoders? #13628
Unanswered
abergmeier
asked this question in
Q&A
Replies: 3 comments
-
Netty provides //kotlin
class MyDecoderTest {
@Test
fun `test decoder`() {
//init channel
val channel = EmbeddedChannel(MyDecoder())
//send data
channel.writeInbound(1)
//get and validate result
val result = channel.readInbound<Int>()
assertEquals(2, result)
}
@Test
fun `test encoder using hexdump`() {
//init channel
val channel = EmbeddedChannel(MyEncoder())
//send data
channel.writeOutbound(1)
val outboundBuf = channel.readOutbound<ByteBuf>()
// compare with the hex string using hexdump (more human-readable)
assertEquals("00000001", ByteBufUtil.hexDump(outboundBuf))
}
@Test
fun `test encoder using another buffer`() {
//init channel
val channel = EmbeddedChannel(MyEncoder())
//send data
channel.writeOutbound(1)
val outboundBuf = channel.readOutbound<ByteBuf>()
//validate result
assertEquals(4, outboundBuf.readableBytes()) //check length of buf
val expectedBuf = channel.alloc().directBuffer()
.writeInt(1) //create similar buf
assertEquals(expectedBuf, outboundBuf)
}
@Test
fun `test encoder using byte array`() {
//init channel
val channel = EmbeddedChannel(MyEncoder())
//send data
channel.writeOutbound(1)
val outboundBuf = channel.readOutbound<ByteBuf>()
//validate result
val resultArray = ByteArray(4)
outboundBuf.readBytes(resultArray)
assert(byteArrayOf(0x00, 0x00, 0x00, 0x01) contentEquals resultArray)
}
class MyDecoder : MessageToMessageDecoder<Int>() {
override fun decode(ctx: ChannelHandlerContext, msg: Int, out: MutableList<Any>) {
val newInt = msg + 1
out.add(newInt)
}
}
class MyEncoder : MessageToByteEncoder<Int>() {
override fun encode(ctx: ChannelHandlerContext, msg: Int, out: ByteBuf) {
//encode int message to ByteBuf
out.writeInt(msg)
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for the examples. |
Beta Was this translation helpful? Give feedback.
0 replies
-
The |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In this repo,
AbstractEncoderTest
is used to testMessageToByteEncoder
.Including
netty-all
(via Gradle), it seems likeAbstractEncoderTest
is not included.So how is one supposed to best test
MessageToByteEncoder
?Beta Was this translation helpful? Give feedback.
All reactions