Skip to content

Commit

Permalink
Merge pull request spring-projects#14975 from Christoph Dreis
Browse files Browse the repository at this point in the history
* spring-projectsgh-14975:
  Use MeterRegistry.isClosed() for tests
  • Loading branch information
wilkinsona committed Oct 29, 2018
2 parents 974f90c + 62ca9e4 commit b1d159b
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,18 @@

package org.springframework.boot.actuate.autoconfigure.metrics.export.atlas;

import java.util.Map;

import com.netflix.spectator.atlas.AtlasConfig;
import io.micrometer.atlas.AtlasMeterRegistry;
import io.micrometer.core.instrument.Clock;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

/**
* Tests for {@link AtlasMetricsExportAutoConfiguration}.
Expand Down Expand Up @@ -89,27 +83,14 @@ public void allowsCustomRegistryToBeUsed() {
public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> {
AtlasMeterRegistry registry = spyOnDisposableBean(
AtlasMeterRegistry.class, context);
AtlasMeterRegistry registry = context
.getBean(AtlasMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close();
verify(registry).stop();
assertThat(registry.isClosed()).isTrue();
});
}

@SuppressWarnings("unchecked")
private <T> T spyOnDisposableBean(Class<T> type,
AssertableApplicationContext context) {
String[] names = context.getBeanNamesForType(type);
assertThat(names).hasSize(1);
String registryBeanName = names[0];
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
Object registryAdapter = disposableBeans.get(registryBeanName);
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
return registry;
}

@Configuration
static class BaseConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,18 @@

package org.springframework.boot.actuate.autoconfigure.metrics.export.datadog;

import java.util.Map;

import io.micrometer.core.instrument.Clock;
import io.micrometer.datadog.DatadogConfig;
import io.micrometer.datadog.DatadogMeterRegistry;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

/**
* Tests for {@link DatadogMetricsExportAutoConfiguration}.
Expand Down Expand Up @@ -98,27 +92,14 @@ public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("management.metrics.export.datadog.api-key=abcde")
.run((context) -> {
DatadogMeterRegistry registry = spyOnDisposableBean(
DatadogMeterRegistry.class, context);
DatadogMeterRegistry registry = context
.getBean(DatadogMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close();
verify(registry).stop();
assertThat(registry.isClosed()).isTrue();
});
}

@SuppressWarnings("unchecked")
private <T> T spyOnDisposableBean(Class<T> type,
AssertableApplicationContext context) {
String[] names = context.getBeanNamesForType(type);
assertThat(names).hasSize(1);
String registryBeanName = names[0];
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
Object registryAdapter = disposableBeans.get(registryBeanName);
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
return registry;
}

@Configuration
static class BaseConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,18 @@

package org.springframework.boot.actuate.autoconfigure.metrics.export.ganglia;

import java.util.Map;

import io.micrometer.core.instrument.Clock;
import io.micrometer.ganglia.GangliaConfig;
import io.micrometer.ganglia.GangliaMeterRegistry;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

/**
* Tests for {@link GangliaMetricsExportAutoConfiguration}.
Expand Down Expand Up @@ -89,27 +83,14 @@ public void allowsCustomRegistryToBeUsed() {
public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> {
GangliaMeterRegistry registry = spyOnDisposableBean(
GangliaMeterRegistry.class, context);
GangliaMeterRegistry registry = context
.getBean(GangliaMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close();
verify(registry).stop();
assertThat(registry.isClosed()).isTrue();
});
}

@SuppressWarnings("unchecked")
private <T> T spyOnDisposableBean(Class<T> type,
AssertableApplicationContext context) {
String[] names = context.getBeanNamesForType(type);
assertThat(names).hasSize(1);
String registryBeanName = names[0];
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
Object registryAdapter = disposableBeans.get(registryBeanName);
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
return registry;
}

@Configuration
static class BaseConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,19 @@

package org.springframework.boot.actuate.autoconfigure.metrics.export.graphite;

import java.util.Map;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Tags;
import io.micrometer.graphite.GraphiteConfig;
import io.micrometer.graphite.GraphiteMeterRegistry;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

/**
* Tests for {@link GraphiteMetricsExportAutoConfiguration}.
Expand Down Expand Up @@ -106,27 +100,14 @@ public void allowsCustomRegistryToBeUsed() {
public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> {
GraphiteMeterRegistry registry = spyOnDisposableBean(
GraphiteMeterRegistry.class, context);
GraphiteMeterRegistry registry = context
.getBean(GraphiteMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close();
verify(registry).stop();
assertThat(registry.isClosed()).isTrue();
});
}

@SuppressWarnings("unchecked")
private <T> T spyOnDisposableBean(Class<T> type,
AssertableApplicationContext context) {
String[] names = context.getBeanNamesForType(type);
assertThat(names).hasSize(1);
String registryBeanName = names[0];
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
Object registryAdapter = disposableBeans.get(registryBeanName);
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
return registry;
}

@Configuration
static class BaseConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,18 @@

package org.springframework.boot.actuate.autoconfigure.metrics.export.influx;

import java.util.Map;

import io.micrometer.core.instrument.Clock;
import io.micrometer.influx.InfluxConfig;
import io.micrometer.influx.InfluxMeterRegistry;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

/**
* Tests for {@link InfluxMetricsExportAutoConfiguration}.
Expand Down Expand Up @@ -89,27 +83,14 @@ public void allowsCustomRegistryToBeUsed() {
public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> {
InfluxMeterRegistry registry = spyOnDisposableBean(
InfluxMeterRegistry.class, context);
InfluxMeterRegistry registry = context
.getBean(InfluxMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close();
verify(registry).stop();
assertThat(registry.isClosed()).isTrue();
});
}

@SuppressWarnings("unchecked")
private <T> T spyOnDisposableBean(Class<T> type,
AssertableApplicationContext context) {
String[] names = context.getBeanNamesForType(type);
assertThat(names).hasSize(1);
String registryBeanName = names[0];
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
Object registryAdapter = disposableBeans.get(registryBeanName);
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
return registry;
}

@Configuration
static class BaseConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,18 @@

package org.springframework.boot.actuate.autoconfigure.metrics.export.jmx;

import java.util.Map;

import io.micrometer.core.instrument.Clock;
import io.micrometer.jmx.JmxConfig;
import io.micrometer.jmx.JmxMeterRegistry;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

/**
* Tests for {@link JmxMetricsExportAutoConfiguration}.
Expand Down Expand Up @@ -89,27 +83,13 @@ public void allowsCustomRegistryToBeUsed() {
public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> {
JmxMeterRegistry registry = spyOnDisposableBean(
JmxMeterRegistry.class, context);
JmxMeterRegistry registry = context.getBean(JmxMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close();
verify(registry).stop();
assertThat(registry.isClosed()).isTrue();
});
}

@SuppressWarnings("unchecked")
private <T> T spyOnDisposableBean(Class<T> type,
AssertableApplicationContext context) {
String[] names = context.getBeanNamesForType(type);
assertThat(names).hasSize(1);
String registryBeanName = names[0];
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
Object registryAdapter = disposableBeans.get(registryBeanName);
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
return registry;
}

@Configuration
static class BaseConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,18 @@

package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;

import java.util.Map;

import io.micrometer.core.instrument.Clock;
import io.micrometer.newrelic.NewRelicConfig;
import io.micrometer.newrelic.NewRelicMeterRegistry;
import org.junit.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

/**
*
Expand Down Expand Up @@ -111,27 +105,14 @@ public void stopsMeterRegistryWhenContextIsClosed() {
.withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
"management.metrics.export.newrelic.account-id=abcde")
.withUserConfiguration(BaseConfiguration.class).run((context) -> {
NewRelicMeterRegistry registry = spyOnDisposableBean(
NewRelicMeterRegistry.class, context);
NewRelicMeterRegistry registry = context
.getBean(NewRelicMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close();
verify(registry).stop();
assertThat(registry.isClosed()).isTrue();
});
}

@SuppressWarnings("unchecked")
private <T> T spyOnDisposableBean(Class<T> type,
AssertableApplicationContext context) {
String[] names = context.getBeanNamesForType(type);
assertThat(names).hasSize(1);
String registryBeanName = names[0];
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
Object registryAdapter = disposableBeans.get(registryBeanName);
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
return registry;
}

@Configuration
static class BaseConfiguration {

Expand Down
Loading

0 comments on commit b1d159b

Please sign in to comment.