Finalize upgrade to Reactor Netty 0.8

This commit fixes the last issue for the upgrade to Reactor Netty 0.8.

Closes: gh-13321
pull/13870/merge
Brian Clozel 6 years ago
parent e15ca514c8
commit 57dbea0db3

@ -16,11 +16,11 @@
package org.springframework.boot.web.embedded.netty; package org.springframework.boot.web.embedded.netty;
import java.net.BindException;
import java.time.Duration; import java.time.Duration;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import reactor.netty.ChannelBindException;
import reactor.netty.DisposableServer; import reactor.netty.DisposableServer;
import reactor.netty.http.HttpResources; import reactor.netty.http.HttpResources;
import reactor.netty.http.server.HttpServer; import reactor.netty.http.server.HttpServer;
@ -69,8 +69,9 @@ public class NettyWebServer implements WebServer {
this.disposableServer = startHttpServer(); this.disposableServer = startHttpServer();
} }
catch (Exception ex) { catch (Exception ex) {
if (findBindException(ex) != null) { ChannelBindException bindException = findBindException(ex);
throw new PortInUseException(getPort()); if (bindException != null) {
throw new PortInUseException(bindException.localPort());
} }
throw new WebServerException("Unable to start Netty", ex); throw new WebServerException("Unable to start Netty", ex);
} }
@ -87,11 +88,11 @@ public class NettyWebServer implements WebServer {
return this.httpServer.handle(this.handlerAdapter).bindNow(); return this.httpServer.handle(this.handlerAdapter).bindNow();
} }
private BindException findBindException(Exception ex) { private ChannelBindException findBindException(Exception ex) {
Throwable candidate = ex; Throwable candidate = ex;
while (candidate != null) { while (candidate != null) {
if (candidate instanceof BindException) { if (candidate instanceof ChannelBindException) {
return (BindException) candidate; return (ChannelBindException) candidate;
} }
candidate = candidate.getCause(); candidate = candidate.getCause();
} }

@ -18,14 +18,14 @@ package org.springframework.boot.web.embedded.netty;
import java.util.Arrays; import java.util.Arrays;
import org.junit.Ignore; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import reactor.netty.http.server.HttpServer; import reactor.netty.http.server.HttpServer;
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactoryTests; import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactoryTests;
import org.springframework.boot.web.server.WebServerException; import org.springframework.boot.web.server.PortInUseException;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
@ -46,14 +46,15 @@ public class NettyReactiveWebServerFactoryTests
} }
@Test @Test
@Ignore
public void exceptionIsThrownWhenPortIsAlreadyInUse() { public void exceptionIsThrownWhenPortIsAlreadyInUse() {
AbstractReactiveWebServerFactory factory = getFactory(); AbstractReactiveWebServerFactory factory = getFactory();
factory.setPort(0); factory.setPort(0);
this.webServer = factory.getWebServer(new EchoHandler()); this.webServer = factory.getWebServer(new EchoHandler());
this.webServer.start(); this.webServer.start();
factory.setPort(this.webServer.getPort()); factory.setPort(this.webServer.getPort());
this.thrown.expect(WebServerException.class); this.thrown.expect(PortInUseException.class);
this.thrown.expect(
Matchers.hasProperty("port", Matchers.equalTo(this.webServer.getPort())));
factory.getWebServer(new EchoHandler()).start(); factory.getWebServer(new EchoHandler()).start();
} }

Loading…
Cancel
Save