Made it easier to use custom ChannelId instances with Channel implementations that rely on the AbstractChannel(Channel parent) constructor.

Motivation:

The AbstractChannel(Channel parent) constructor was previously hard-coded to always
call DefaultChannelId.newInstance(), and this made it difficult to use a custom
ChannelId implementation with some commonly used Channel implementations.

Modifications:

Introduced newId() method in AbstractChannel, which by default returns
DefaultChannelId.newInstance() but can be overridden by subclasses. Added
ensureDefaultChannelId() test to AbstractChannelTest, to ensure the prior
behavior of calling DefaultChannelId.newInstance() still holds with the
AbstractChannel(Channel parent) constructor.

Result:

AbstractChannel now has the protected newId() method, but there is no functional
difference.
This commit is contained in:
Travis Haagen 2016-02-01 15:07:28 -08:00 committed by Norman Maurer
parent d97f17060f
commit a75dcb2756
2 changed files with 18 additions and 1 deletions

View File

@ -78,7 +78,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
*/ */
protected AbstractChannel(Channel parent) { protected AbstractChannel(Channel parent) {
this.parent = parent; this.parent = parent;
id = DefaultChannelId.newInstance(); id = newId();
unsafe = newUnsafe(); unsafe = newUnsafe();
pipeline = new DefaultChannelPipeline(this); pipeline = new DefaultChannelPipeline(this);
} }
@ -101,6 +101,14 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
return id; return id;
} }
/**
* Returns a new {@link DefaultChannelId} instance. Subclasses may override this method to assign custom
* {@link ChannelId}s to {@link Channel}s that use the {@link AbstractChannel#AbstractChannel(Channel)} constructor.
*/
protected ChannelId newId() {
return DefaultChannelId.newInstance();
}
@Override @Override
public boolean isWritable() { public boolean isWritable() {
ChannelOutboundBuffer buf = unsafe.outboundBuffer(); ChannelOutboundBuffer buf = unsafe.outboundBuffer();

View File

@ -22,6 +22,8 @@ import org.easymock.IAnswer;
import org.junit.Test; import org.junit.Test;
import static org.easymock.EasyMock.*; import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
public class AbstractChannelTest { public class AbstractChannelTest {
@ -90,6 +92,13 @@ public class AbstractChannelTest {
verify(handler); verify(handler);
} }
@Test
public void ensureDefaultChannelId() {
TestChannel channel = new TestChannel();
final ChannelId channelId = channel.id();
assertThat(channelId, instanceOf(DefaultChannelId.class));
}
private static void registerChannel(EventLoop eventLoop, Channel channel) throws Exception { private static void registerChannel(EventLoop eventLoop, Channel channel) throws Exception {
DefaultChannelPromise future = new DefaultChannelPromise(channel); DefaultChannelPromise future = new DefaultChannelPromise(channel);
channel.unsafe().register(eventLoop, future); channel.unsafe().register(eventLoop, future);