Ensure that Tomcat is completely stopped when its initialization fails

Closes gh-12736
pull/12859/head
Andy Wilkinson 7 years ago
parent 30de75c75c
commit edc00eef24

@ -134,6 +134,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
}
}
catch (Exception ex) {
stopSilently();
throw new EmbeddedServletContainerException(
"Unable to start embedded Tomcat", ex);
}

@ -23,6 +23,7 @@ import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@ -491,7 +492,18 @@ public class TomcatEmbeddedServletContainerFactoryTests
@Test
public void faultyFilterCausesStartFailure() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
final AtomicReference<Tomcat> tomcatReference = new AtomicReference<Tomcat>();
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(
0) {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcatReference.set(tomcat);
return super.getTomcatEmbeddedServletContainer(tomcat);
}
};
factory.addInitializers(new ServletContextInitializer() {
@Override
@ -518,7 +530,13 @@ public class TomcatEmbeddedServletContainerFactoryTests
});
this.thrown.expect(EmbeddedServletContainerException.class);
factory.getEmbeddedServletContainer().start();
try {
factory.getEmbeddedServletContainer();
}
finally {
assertThat(tomcatReference.get().getServer().getState())
.isEqualTo(LifecycleState.STOPPED);
}
}
@Override

Loading…
Cancel
Save