|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.bus; |
| 18 | + |
| 19 | +import java.util.concurrent.CountDownLatch; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.AfterAll; |
| 22 | +import org.junit.jupiter.api.BeforeAll; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.testcontainers.containers.RabbitMQContainer; |
| 25 | +import org.testcontainers.junit.jupiter.Container; |
| 26 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 27 | + |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.boot.SpringBootConfiguration; |
| 30 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 31 | +import org.springframework.boot.builder.SpringApplicationBuilder; |
| 32 | +import org.springframework.boot.test.context.SpringBootTest; |
| 33 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 34 | +import org.springframework.cloud.bus.event.EnvironmentChangeRemoteApplicationEvent; |
| 35 | +import org.springframework.context.ApplicationListener; |
| 36 | +import org.springframework.context.ConfigurableApplicationContext; |
| 37 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Ryan Baxter |
| 44 | + */ |
| 45 | +@SpringBootTest(webEnvironment = RANDOM_PORT, |
| 46 | + properties = { "management.endpoints.web.exposure.include=*", |
| 47 | + "spring.cloud.stream.bindings.springCloudBusOutput.producer.errorChannelEnabled=true", |
| 48 | + "logging.level.org.springframework.cloud.bus=TRACE", "spring.cloud.bus.id=app:1" }) |
| 49 | +@Testcontainers |
| 50 | +public class ShutdownListenerIntegrationTests { |
| 51 | + |
| 52 | + private static ConfigurableApplicationContext context; |
| 53 | + |
| 54 | + @Container |
| 55 | + @ServiceConnection |
| 56 | + private static final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer("rabbitmq:4.0-management"); |
| 57 | + |
| 58 | + @BeforeAll |
| 59 | + static void before() { |
| 60 | + context = new SpringApplicationBuilder(TestConfig.class) |
| 61 | + .properties("server.port=0", "spring.rabbitmq.host=" + rabbitMQContainer.getHost(), |
| 62 | + "spring.rabbitmq.port=" + rabbitMQContainer.getAmqpPort(), |
| 63 | + "management.endpoints.web.exposure.include=*", "spring.cloud.bus.id=app:2", "debug=true") |
| 64 | + .run(); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterAll |
| 68 | + static void after() { |
| 69 | + if (context != null) { |
| 70 | + context.close(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testShutdown(@Autowired WebTestClient client) { |
| 76 | + assertThat(rabbitMQContainer.isRunning()); |
| 77 | + client.post().uri("/actuator/busshutdown/app:2").exchange().expectStatus().is2xxSuccessful(); |
| 78 | + assertThat(context.isClosed()); |
| 79 | + } |
| 80 | + |
| 81 | + @SpringBootConfiguration |
| 82 | + @EnableAutoConfiguration |
| 83 | + static class TestConfig implements ApplicationListener<EnvironmentChangeRemoteApplicationEvent> { |
| 84 | + |
| 85 | + CountDownLatch latch = new CountDownLatch(1); |
| 86 | + |
| 87 | + @Override |
| 88 | + public void onApplicationEvent(EnvironmentChangeRemoteApplicationEvent event) { |
| 89 | + latch.countDown(); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments