RestartIntegrationTests.java
/*
* Copyright 2012-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.context.restart;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.BDDAssertions.then;
public class RestartIntegrationTests {
private ConfigurableApplicationContext context;
@AfterEach
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void testRestartTwice() {
this.context = SpringApplication.run(TestConfiguration.class, "--management.endpoint.restart.enabled=true",
"--server.port=0", "--spring.cloud.bootstrap.enabled=true",
"--management.endpoints.web.exposure.include=restart", "--spring.liveBeansView.mbeanDomain=livebeans");
RestartEndpoint endpoint = this.context.getBean(RestartEndpoint.class);
then(this.context.getParent()).isNotNull();
then(this.context.getParent().getParent()).isNull();
this.context = endpoint.doRestart();
then(this.context).isNotNull();
then(this.context.getParent()).isNotNull();
then(this.context.getParent().getParent()).isNull();
RestartEndpoint next = this.context.getBean(RestartEndpoint.class);
then(next).isSameAs(endpoint);
this.context = next.doRestart();
then(this.context).isNotNull();
then(this.context.getParent()).isNotNull();
then(this.context.getParent().getParent()).isNull();
// FIXME: 4.0.0 missing LiveBeansView
/*
* LiveBeansView beans = new LiveBeansView(); String json =
* beans.getSnapshotAsJson();
* then(json).containsOnlyOnce("parent\": \"bootstrap");
* then(json).containsOnlyOnce("parent\": null");
*/
}
@Test
public void testResumeAfterPause() {
this.context = SpringApplication.run(PauseHandlerConfiguration.class,
"--management.endpoint.restart.enabled=true", "--server.port=0",
"--management.endpoints.web.exposure.include=restart,pause,resume");
RestartEndpoint endpoint = this.context.getBean(RestartEndpoint.class);
TestPauseHandler handler = this.context.getBean(TestPauseHandler.class);
then(endpoint.getResumeEndpoint().resume()).isFalse();
then(endpoint.getPauseEndpoint().pause()).isTrue();
then(handler.paused).isTrue();
then(endpoint.getResumeEndpoint().resume()).isTrue();
then(handler.paused).isFalse();
then(endpoint.getResumeEndpoint().resume()).isFalse();
}
@Test
public void testRestartAfterPause() {
this.context = SpringApplication.run(PauseHandlerConfiguration.class,
"--management.endpoint.restart.enabled=true", "--server.port=0",
"--management.endpoints.web.exposure.include=restart,pause,resume");
RestartEndpoint endpoint = this.context.getBean(RestartEndpoint.class);
TestPauseHandler closed = this.context.getBean(TestPauseHandler.class);
then(endpoint.getPauseEndpoint().pause()).isTrue();
this.context = endpoint.doRestart();
TestPauseHandler handler = this.context.getBean(TestPauseHandler.class);
then(handler).isNotSameAs(closed);
then(endpoint.getResumeEndpoint().resume()).isFalse();
then(endpoint.getPauseEndpoint().pause()).isTrue();
then(handler.paused).isTrue();
then(endpoint.getResumeEndpoint().resume()).isTrue();
then(handler.paused).isFalse();
}
@Test
public void testResumeWithoutPauseHandlers() {
RestartEndpoint endpoint = new RestartEndpoint();
endpoint.doPause();
then(endpoint.getResumeEndpoint().resume()).isFalse();
}
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
protected static class TestConfiguration {
}
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
protected static class PauseHandlerConfiguration {
@Bean
TestPauseHandler testPauseHandler() {
return new TestPauseHandler();
}
}
static class TestPauseHandler implements PauseHandler {
boolean paused;
@Override
public void pause() {
this.paused = true;
}
@Override
public void resume() {
this.paused = false;
}
}
}