Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling MainClassApplicationUnderTest.close() leaves Server Socket open #1472

Open
andypeel opened this issue Aug 15, 2019 · 3 comments
Open

Comments

@andypeel
Copy link

I'm sharing an instance of MainClassApplicationUnderTest (or rather a subclass of it, to expose the server start call) between all test case methods in a JUnit 4 test class. This works fine, but when I add a second test class, the Ratpack server started by the first test class is still bound to the port specified and the following is thrown:

Caused by: java.net.BindException: Address already in use: bind
	at sun.nio.ch.Net.bind0(Native Method)
	at sun.nio.ch.Net.bind(Net.java:433)
	at sun.nio.ch.Net.bind(Net.java:425)
	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
	at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:132)
	at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:551)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1332)
	at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:503)
	at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:488)
	at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:984)
	at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:247)
	at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:355)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:416)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:515)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at ratpack.exec.internal.DefaultExecController$ExecControllerBindingThreadFactory.lambda$newThread$0(DefaultExecController.java:137)

Interestingly, this only happens when running via Maven/Surefire. Running my full test suite in Intellij works fine. But then Intellij does its own weird stuff when running tests.

Code is as follows:


    private BindingsImposition bindingsImposition;

    public TestableRatpackServer() {
        super(Application.class);
    }

    public TestableRatpackServer(BindingsImposition bindingsImposition) {
        super(Application.class);
        this.bindingsImposition = bindingsImposition;
    }

    protected void addImpositions(ImpositionsSpec impositions) {
        if (bindingsImposition != null) {
            impositions.add(bindingsImposition);
        }
    }

    public void startNow() throws Exception {
        try {
            createServer().start();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void close() {
        super.close();
    }
}
public class Application {
    public static void main(String[] args) throws Exception {
        RatpackServer.start(serverSpec -> serverSpec
                .serverConfig(ServerConfig.builder().development(false).port(8080)));
    }
}
public class TestApplication {

    private static TestableRatpackServer server;

    @BeforeClass
    public static void setUpClass() throws Exception {
        server = new TestableRatpackServer();
        server.startNow();
    }

    @AfterClass
    public static void tearDownClass() {
        server.close();
    }

    @Test
    public void doNothing() {
    }
}

I'm using Ratpack 1.7.2

@andypeel
Copy link
Author

I have a zip of a simple project, but I can't upload it.

@andypeel
Copy link
Author

@johnrengelman
Copy link
Member

johnrengelman commented Aug 15, 2019

Can you just post the sample app source to a public Github repo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants