一种特殊的 Channel 实现——EmbeddedChannel,它是 Netty 专门为改进针对ChannelHandler 的单元测试而提供的。
将入站数据或者出站数据写入到 EmbeddedChannel 中,然后检查是否有任何东西到达了 ChannelPipeline 的尾端。以这种方式,你便可以确定消息是否已经被编码或者被解码过了,以及是否触发了任何的 ChannelHandler 动作。
image.png

writeInbound(Object… msgs)

将入站消息写到 EmbeddedChannel 中。如果可以通过 readInbound()方法从EmbeddedChannel 中读取数据,则返回 true

readInbound()

从 EmbeddedChannel 中读取一个入站消息。任何返回的东西都穿越了整个ChannelPipeline。如果没有任何可供读取的,则返回 null

writeOutbound(Object… msgs)

将出站消息写到 EmbeddedChannel 中。如果现在可以通过 readOutbound()方法从EmbeddedChannel 中读取到什么东西,则返回 true

readOutbound()

从 EmbeddedChannel 中读取一个出站消息。任何返回的东西都穿越了整个ChannelPipeline。如果没有任何可供读取的,则返回 null

finish()

将 EmbeddedChannel 标记为完成,并且如果有可被读取的入站数据或者出站数据,则返回 true。这个方法还将会调用 EmbeddedChannel 上的 close()方法。
入站数据由 ChannelInboundHandler 处理,代表从远程节点读取的数据。出站数据由ChannelOutboundHandler 处理,代表将要写到远程节点的数据。
使用 writeOutbound()方法将消息写到 Channel 中,并通过 ChannelPipeline 沿着出站的方向传递。随后,你可以使用 readOutbound()方法来读取已被处理过的消息,以确定结果是否和预期一样。 类似地,对于入站数据,你需要使用 writeInbound()和 readInbound()方法。
在每种情况下,消息都将会传递过 ChannelPipeline,并且被相关的ChannelInboundHandler 或者 ChannelOutboundHandler 处理。