|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.metrics.export.datadog; |
18 | 18 |
|
| 19 | +import java.util.Map; |
| 20 | + |
19 | 21 | import io.micrometer.core.instrument.Clock; |
20 | 22 | import io.micrometer.datadog.DatadogConfig; |
21 | 23 | import io.micrometer.datadog.DatadogMeterRegistry; |
22 | 24 | import org.junit.Test; |
23 | 25 |
|
24 | 26 | import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 27 | +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; |
25 | 28 | import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
26 | 29 | import org.springframework.context.annotation.Bean; |
27 | 30 | import org.springframework.context.annotation.Configuration; |
28 | 31 | import org.springframework.context.annotation.Import; |
| 32 | +import org.springframework.test.util.ReflectionTestUtils; |
29 | 33 |
|
30 | 34 | import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.mockito.Mockito.spy; |
| 36 | +import static org.mockito.Mockito.verify; |
31 | 37 |
|
32 | 38 | /** |
33 | 39 | * Tests for {@link DatadogMetricsExportAutoConfiguration}. |
@@ -87,6 +93,32 @@ public void allowsCustomRegistryToBeUsed() { |
87 | 93 | .hasBean("customRegistry").hasSingleBean(DatadogConfig.class)); |
88 | 94 | } |
89 | 95 |
|
| 96 | + @Test |
| 97 | + public void stopsMeterRegistryWhenContextIsClosed() { |
| 98 | + this.runner.withUserConfiguration(BaseConfiguration.class) |
| 99 | + .withPropertyValues("management.metrics.export.datadog.api-key=abcde") |
| 100 | + .run((context) -> { |
| 101 | + DatadogMeterRegistry registry = spyOnDisposableBean( |
| 102 | + DatadogMeterRegistry.class, context); |
| 103 | + context.close(); |
| 104 | + verify(registry).stop(); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + @SuppressWarnings("unchecked") |
| 109 | + private <T> T spyOnDisposableBean(Class<T> type, |
| 110 | + AssertableApplicationContext context) { |
| 111 | + String[] names = context.getBeanNamesForType(type); |
| 112 | + assertThat(names).hasSize(1); |
| 113 | + String registryBeanName = names[0]; |
| 114 | + Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils |
| 115 | + .getField(context.getAutowireCapableBeanFactory(), "disposableBeans"); |
| 116 | + Object registryAdapter = disposableBeans.get(registryBeanName); |
| 117 | + T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean")); |
| 118 | + ReflectionTestUtils.setField(registryAdapter, "bean", registry); |
| 119 | + return registry; |
| 120 | + } |
| 121 | + |
90 | 122 | @Configuration |
91 | 123 | static class BaseConfiguration { |
92 | 124 |
|
@@ -122,7 +154,7 @@ public String get(String k) { |
122 | 154 | @Import(BaseConfiguration.class) |
123 | 155 | static class CustomRegistryConfiguration { |
124 | 156 |
|
125 | | - @Bean |
| 157 | + @Bean(destroyMethod = "stop") |
126 | 158 | public DatadogMeterRegistry customRegistry(DatadogConfig config, Clock clock) { |
127 | 159 | return new DatadogMeterRegistry(config, clock); |
128 | 160 | } |
|
0 commit comments