question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bootstrap a EmbeddedChannel failed

See original GitHub issue

I found EmbeddedChannel will registered itself in its constructor at EmbeddedChannel.java#L178. And Bootstrap connect will try register channel after create it and found it had been registered and throw exception AbstractBootstrap.java#L332.

Because it was just for some test purpose, so it’s not a big deal. But it would be better if we can bootstrap a EmbeddedChannel! Some of my idea: maybe we can ignore register if registered at EmbeddedEventLoop.java#L130, because EmbeddedChannel never blocked.

Expected behavior

Following code should pass.

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class BootstrapEmbeddedChannel {
    @Test
    public void shouldGetEmbeddedChanenl() {
        EmbeddedChannel firstCh = new EmbeddedChannel();

        ChannelFuture secondChFuture = new Bootstrap()
                .group(firstCh.eventLoop())
                .channel(firstCh.getClass())
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {

                    }
                }).connect("localhost", 9999);

        assertTrue(secondChFuture.isSuccess());
    }
}

Actual behavior

java.lang.IllegalStateException: registered to an event loop already
	at io.netty.channel.AbstractChannel$AbstractUnsafe.register(AbstractChannel.java:460)
	at io.netty.channel.embedded.EmbeddedEventLoop.register(EmbeddedEventLoop.java:130)
	at io.netty.channel.embedded.EmbeddedEventLoop.register(EmbeddedEventLoop.java:124)
	at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:332)
	at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163)
	at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:145)
	at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:126)

Steps to reproduce

Minimal yet complete reproducer code (or URL to code)

Same above.

Netty version

4.1.9.Final

JVM version (e.g. java -version)

1.8.0_65

OS version (e.g. uname -a)

Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
asarkarcommented, Jan 11, 2018

@chhsiao90 Hmm. I couldn’t get bootstrap working in unit test so I took it out using a factory class. It’s cleaner design, but still bothering me. Netty has good examples, but sadly no accompanying unit testing samples.

Here my learning project: https://github.com/asarkar/kotlin/tree/master/netty-learning/proxy

0reactions
chhsiao90commented, Jan 9, 2018

@asarkar - agreed! that’s what I’m doing too, create a channel factory instead of using bootstrap directly. And writing unit test is always hard…

Read more comments on GitHub >

github_iconTop Results From Across the Web

EmbeddedChannel (Netty API Reference (4.0.56.Final))
Request to close this Channel and notify the ChannelFuture once the operation completes, either because the operation was successful or because of an...
Read more >
Ways to configure a Netty EmbeddedChannel via Bootstrap?
In the Apache PLC4X project I am currently setting up a framework for integration tests. PLC4X 's Java drivers are heavily using Netty....
Read more >
Chapter 8. Bootstrapping - Netty in Action - liveBook · Manning
Binds the ServerChannel and returns a ChannelFuture, which is notified once the connection operation is complete (with the success or error result). The...
Read more >
EmbeddedChannel Class Reference - Apple
EmbeddedChannel is a Channel implementation that does neither any actual IO nor has a proper eventing mechanism.
Read more >
io.netty.util.concurrent.ImmediateEventExecutor Java Examples
thenReturn(embeddedChannel); when(channelHandlerContext.executor()). ... public void testConnectNotExists(Bootstrap cb) throws Throwable { final ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found