mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Making sure to stop embedded web servers in tests
Closes gh-9012
This commit is contained in:
parent
7b1fc01733
commit
5121b848e1
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -19,6 +19,7 @@ package org.springframework.boot.actuate.cloudfoundry;
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
@ -44,6 +45,15 @@ public class SkipSslVerificationHttpRequestFactoryTests {
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private EmbeddedServletContainer container;
|
||||
|
||||
@After
|
||||
public void shutdownContainer() {
|
||||
if (this.container != null) {
|
||||
this.container.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restCallToSelfSignedServershouldNotThrowSslException() throws Exception {
|
||||
String httpsUrl = getHttpsUrl();
|
||||
@ -66,10 +76,10 @@ public class SkipSslVerificationHttpRequestFactoryTests {
|
||||
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(
|
||||
0);
|
||||
factory.setSsl(getSsl("password", "classpath:test.jks"));
|
||||
EmbeddedServletContainer container = factory.getEmbeddedServletContainer(
|
||||
this.container = factory.getEmbeddedServletContainer(
|
||||
new ServletRegistrationBean(new ExampleServlet(), "/hello"));
|
||||
container.start();
|
||||
return "https://localhost:" + container.getPort() + "/hello";
|
||||
this.container.start();
|
||||
return "https://localhost:" + this.container.getPort() + "/hello";
|
||||
}
|
||||
|
||||
private Ssl getSsl(String keyPassword, String keyStore) {
|
||||
|
@ -43,6 +43,7 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.boot.bind.RelaxedDataBinder;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
|
||||
@ -438,12 +439,12 @@ public class ServerPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void defaultTomcatBackgroundProcessorDelay() throws Exception {
|
||||
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
|
||||
this.properties.customize(container);
|
||||
assertThat(
|
||||
((TomcatEmbeddedServletContainer) container.getEmbeddedServletContainer())
|
||||
.getTomcat().getEngine().getBackgroundProcessorDelay())
|
||||
.isEqualTo(30);
|
||||
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
|
||||
this.properties.customize(factory);
|
||||
EmbeddedServletContainer container = factory.getEmbeddedServletContainer();
|
||||
assertThat(((TomcatEmbeddedServletContainer) container).getTomcat().getEngine()
|
||||
.getBackgroundProcessorDelay()).isEqualTo(30);
|
||||
container.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -451,12 +452,12 @@ public class ServerPropertiesTests {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("server.tomcat.background-processor-delay", "5");
|
||||
bindProperties(map);
|
||||
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
|
||||
this.properties.customize(container);
|
||||
assertThat(
|
||||
((TomcatEmbeddedServletContainer) container.getEmbeddedServletContainer())
|
||||
.getTomcat().getEngine().getBackgroundProcessorDelay())
|
||||
.isEqualTo(5);
|
||||
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
|
||||
this.properties.customize(factory);
|
||||
EmbeddedServletContainer container = factory.getEmbeddedServletContainer();
|
||||
assertThat(((TomcatEmbeddedServletContainer) container).getTomcat().getEngine()
|
||||
.getBackgroundProcessorDelay()).isEqualTo(5);
|
||||
container.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -267,9 +267,9 @@ public class JettyEmbeddedServletContainerFactoryTests
|
||||
JettyEmbeddedServletContainerFactory factory = getFactory();
|
||||
factory.setThreadPool(null);
|
||||
assertThat(factory.getThreadPool()).isNull();
|
||||
JettyEmbeddedServletContainer servletContainer = (JettyEmbeddedServletContainer) factory
|
||||
.getEmbeddedServletContainer();
|
||||
assertThat(servletContainer.getServer().getThreadPool()).isNotNull();
|
||||
this.container = factory.getEmbeddedServletContainer();
|
||||
assertThat(((JettyEmbeddedServletContainer) this.container).getServer()
|
||||
.getThreadPool()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -277,9 +277,9 @@ public class JettyEmbeddedServletContainerFactoryTests
|
||||
JettyEmbeddedServletContainerFactory factory = getFactory();
|
||||
ThreadPool threadPool = mock(ThreadPool.class);
|
||||
factory.setThreadPool(threadPool);
|
||||
JettyEmbeddedServletContainer servletContainer = (JettyEmbeddedServletContainer) factory
|
||||
.getEmbeddedServletContainer();
|
||||
assertThat(servletContainer.getServer().getThreadPool()).isSameAs(threadPool);
|
||||
this.container = factory.getEmbeddedServletContainer();
|
||||
assertThat(((JettyEmbeddedServletContainer) this.container).getServer()
|
||||
.getThreadPool()).isSameAs(threadPool);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -263,10 +263,15 @@ public class UndertowEmbeddedServletContainerFactoryTests
|
||||
}
|
||||
|
||||
private ServletContainer getServletContainerFromNewFactory() {
|
||||
UndertowEmbeddedServletContainer undertow1 = (UndertowEmbeddedServletContainer) getFactory()
|
||||
.getEmbeddedServletContainer();
|
||||
return ((DeploymentManager) ReflectionTestUtils.getField(undertow1, "manager"))
|
||||
.getDeployment().getServletContainer();
|
||||
UndertowEmbeddedServletContainer container = (UndertowEmbeddedServletContainer)
|
||||
getFactory().getEmbeddedServletContainer();
|
||||
try {
|
||||
return ((DeploymentManager) ReflectionTestUtils.getField(container, "manager"))
|
||||
.getDeployment().getServletContainer();
|
||||
}
|
||||
finally {
|
||||
container.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user