Fix StatusAggregator static initialization
Prior to this commit, there was a cycle between `StatusAggregator` and `SimpleStatusAggregator`, which caused a static initialization bug - depending on which class (the implementation or its interface) was loaded first. This commit turns the static field of the `StatusAggregator` interface into a static method to avoid this problem. Fixes gh-21211
This commit is contained in:
parent
ea5f282e9b
commit
ec871d6752
|
|
@ -56,7 +56,7 @@ class AvailabilityProbesHealthEndpointGroup implements HealthEndpointGroup {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StatusAggregator getStatusAggregator() {
|
public StatusAggregator getStatusAggregator() {
|
||||||
return StatusAggregator.DEFAULT;
|
return StatusAggregator.getDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ class AvailabilityProbesHealthEndpointGroupTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getStatusAggregatorReturnsDefaultStatusAggregator() {
|
void getStatusAggregatorReturnsDefaultStatusAggregator() {
|
||||||
assertThat(this.group.getStatusAggregator()).isEqualTo(StatusAggregator.DEFAULT);
|
assertThat(this.group.getStatusAggregator()).isEqualTo(StatusAggregator.getDefault());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -37,6 +37,9 @@ import org.springframework.util.ObjectUtils;
|
||||||
public class SimpleStatusAggregator implements StatusAggregator {
|
public class SimpleStatusAggregator implements StatusAggregator {
|
||||||
|
|
||||||
private static final List<String> DEFAULT_ORDER;
|
private static final List<String> DEFAULT_ORDER;
|
||||||
|
|
||||||
|
static final StatusAggregator INSTANCE;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
List<String> defaultOrder = new ArrayList<>();
|
List<String> defaultOrder = new ArrayList<>();
|
||||||
defaultOrder.add(Status.DOWN.getCode());
|
defaultOrder.add(Status.DOWN.getCode());
|
||||||
|
|
@ -44,6 +47,7 @@ public class SimpleStatusAggregator implements StatusAggregator {
|
||||||
defaultOrder.add(Status.UP.getCode());
|
defaultOrder.add(Status.UP.getCode());
|
||||||
defaultOrder.add(Status.UNKNOWN.getCode());
|
defaultOrder.add(Status.UNKNOWN.getCode());
|
||||||
DEFAULT_ORDER = Collections.unmodifiableList(getUniformCodes(defaultOrder.stream()));
|
DEFAULT_ORDER = Collections.unmodifiableList(getUniformCodes(defaultOrder.stream()));
|
||||||
|
INSTANCE = new SimpleStatusAggregator();
|
||||||
}
|
}
|
||||||
|
|
||||||
private final List<String> order;
|
private final List<String> order;
|
||||||
|
|
|
||||||
|
|
@ -33,10 +33,13 @@ import java.util.Set;
|
||||||
public interface StatusAggregator {
|
public interface StatusAggregator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link StatusAggregator} instance using default ordering rules.
|
* Return {@link StatusAggregator} instance using default ordering rules.
|
||||||
|
* @return a {@code StatusAggregator} with default ordering rules.
|
||||||
* @since 2.3.0
|
* @since 2.3.0
|
||||||
*/
|
*/
|
||||||
StatusAggregator DEFAULT = new SimpleStatusAggregator();
|
static StatusAggregator getDefault() {
|
||||||
|
return SimpleStatusAggregator.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the aggregate status for the given set of statuses.
|
* Return the aggregate status for the given set of statuses.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -29,7 +29,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
class SimpleStatusAggregatorTests {
|
class SimpleStatusAggregatorTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getAggregateStatusWhenUsingDefaultOrder() {
|
void getAggregateStatusWhenUsingDefaultInstance() {
|
||||||
|
StatusAggregator aggregator = StatusAggregator.getDefault();
|
||||||
|
Status status = aggregator.getAggregateStatus(Status.DOWN, Status.UP, Status.UNKNOWN, Status.OUT_OF_SERVICE);
|
||||||
|
assertThat(status).isEqualTo(Status.DOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getAggregateStatusWhenUsingNewDefaultOrder() {
|
||||||
SimpleStatusAggregator aggregator = new SimpleStatusAggregator();
|
SimpleStatusAggregator aggregator = new SimpleStatusAggregator();
|
||||||
Status status = aggregator.getAggregateStatus(Status.DOWN, Status.UP, Status.UNKNOWN, Status.OUT_OF_SERVICE);
|
Status status = aggregator.getAggregateStatus(Status.DOWN, Status.UP, Status.UNKNOWN, Status.OUT_OF_SERVICE);
|
||||||
assertThat(status).isEqualTo(Status.DOWN);
|
assertThat(status).isEqualTo(Status.DOWN);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue