Allow to set parent Channel when constructing EmbeddedChannel (#9230)

Motivation:

Sometimes it is beneficial to be able to set a parent Channel in EmbeddedChannel if the handler that should be tested depend on the parent.

Modifications:

- Add another constructor which allows to specify a parent
- Add unit tests

Result:

Fixes https://github.com/netty/netty/issues/9228.
This commit is contained in:
Norman Maurer 2019-06-08 09:11:31 -07:00
parent 576b1768f4
commit acd13e1cf7
2 changed files with 29 additions and 1 deletions

View File

@ -155,8 +155,25 @@ public class EmbeddedChannel extends AbstractChannel {
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(ChannelId channelId, boolean register, boolean hasDisconnect,
ChannelHandler... handlers) {
this(null, channelId, register, hasDisconnect, handlers);
}
/**
* Create a new instance with the channel ID set to the given ID and the pipeline
* initialized with the specified handlers.
*
* @param parent the parent {@link Channel} of this {@link EmbeddedChannel}.
* @param channelId the {@link ChannelId} that will be used to identify this channel
* @param register {@code true} if this {@link Channel} is registered to the {@link EventLoop} in the
* constructor. If {@code false} the user will need to call {@link #register()}.
* @param hasDisconnect {@code false} if this {@link Channel} will delegate {@link #disconnect()}
* to {@link #close()}, {@link false} otherwise.
* @param handlers the {@link ChannelHandler}s which will be add in the {@link ChannelPipeline}
*/
public EmbeddedChannel(Channel parent, ChannelId channelId, boolean register, boolean hasDisconnect,
final ChannelHandler... handlers) {
super(null, new EmbeddedEventLoop(), channelId);
super(parent, new EmbeddedEventLoop(), channelId);
metadata = metadata(hasDisconnect);
config = new DefaultChannelConfig(this);
setup(register, handlers);

View File

@ -52,6 +52,17 @@ import io.netty.util.concurrent.ScheduledFuture;
public class EmbeddedChannelTest {
@Test
public void testParent() {
EmbeddedChannel parent = new EmbeddedChannel();
EmbeddedChannel channel = new EmbeddedChannel(parent, EmbeddedChannelId.INSTANCE, true, false);
assertSame(parent, channel.parent());
assertNull(parent.parent());
assertFalse(channel.finish());
assertFalse(parent.finish());
}
@Test
public void testNotRegistered() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(false, false);