Commit 5bb3d178 by Johannes Edmeier

fix unstable tests

parent 38452e45
...@@ -32,14 +32,16 @@ import de.codecentric.boot.admin.server.notify.OpsGenieNotifier; ...@@ -32,14 +32,16 @@ import de.codecentric.boot.admin.server.notify.OpsGenieNotifier;
import de.codecentric.boot.admin.server.notify.PagerdutyNotifier; import de.codecentric.boot.admin.server.notify.PagerdutyNotifier;
import de.codecentric.boot.admin.server.notify.SlackNotifier; import de.codecentric.boot.admin.server.notify.SlackNotifier;
import de.codecentric.boot.admin.server.notify.TelegramNotifier; import de.codecentric.boot.admin.server.notify.TelegramNotifier;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.publisher.UnicastProcessor;
import reactor.test.StepVerifier; import reactor.test.StepVerifier;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
...@@ -63,18 +65,16 @@ public class AdminServerNotifierAutoConfigurationTest { ...@@ -63,18 +65,16 @@ public class AdminServerNotifierAutoConfigurationTest {
} }
@Test @Test
public void test_notifierListener() throws InterruptedException { public void test_notifierListener() {
load(TestSingleNotifierConfig.class); load(TestSingleNotifierConfig.class);
InstanceEventStore store = context.getBean(InstanceEventStore.class); InstanceEventStore store = context.getBean(InstanceEventStore.class);
StepVerifier.create(store) StepVerifier.create(context.getBean(TestNotifier.class).getFlux())
.expectSubscription() .expectSubscription()
.then(() -> StepVerifier.create(store.append(Collections.singletonList(APP_DOWN))).verifyComplete()) .then(() -> StepVerifier.create(store.append(Collections.singletonList(APP_DOWN))).verifyComplete())
.expectNext(APP_DOWN) .expectNext(APP_DOWN)
.thenCancel() .thenCancel()
.verify(); .verify();
Thread.sleep(100); //wait for the notifications in different thread
assertThat(context.getBean(TestNotifier.class).getEvents()).containsOnly(APP_DOWN);
} }
@Test @Test
...@@ -161,7 +161,8 @@ public class AdminServerNotifierAutoConfigurationTest { ...@@ -161,7 +161,8 @@ public class AdminServerNotifierAutoConfigurationTest {
public static class TestSingleNotifierConfig { public static class TestSingleNotifierConfig {
@Bean @Bean
public Notifier testNotifier() { @Qualifier("testNotifier")
public TestNotifier testNotifier() {
return new TestNotifier(); return new TestNotifier();
} }
} }
...@@ -175,12 +176,14 @@ public class AdminServerNotifierAutoConfigurationTest { ...@@ -175,12 +176,14 @@ public class AdminServerNotifierAutoConfigurationTest {
private static class TestMultipleNotifierConfig { private static class TestMultipleNotifierConfig {
@Bean @Bean
public Notifier testNotifier1() { @Qualifier("testNotifier1")
public TestNotifier testNotifier1() {
return new TestNotifier(); return new TestNotifier();
} }
@Bean @Bean
public Notifier testNotifier2() { @Qualifier("testNotifier2")
public TestNotifier testNotifier2() {
return new TestNotifier(); return new TestNotifier();
} }
} }
...@@ -188,27 +191,36 @@ public class AdminServerNotifierAutoConfigurationTest { ...@@ -188,27 +191,36 @@ public class AdminServerNotifierAutoConfigurationTest {
private static class TestMultipleWithPrimaryNotifierConfig { private static class TestMultipleWithPrimaryNotifierConfig {
@Bean @Bean
@Primary @Primary
public Notifier testNotifierPrimary() { @Qualifier("testNotifier")
public TestNotifier testNotifierPrimary() {
return new TestNotifier(); return new TestNotifier();
} }
@Bean @Bean
public Notifier testNotifier2() { @Qualifier("testNotifier3")
public TestNotifier testNotifier2() {
return new TestNotifier(); return new TestNotifier();
} }
} }
private static class TestNotifier implements Notifier { private static class TestNotifier implements Notifier {
private List<InstanceEvent> events = new ArrayList<>(); private final Flux<InstanceEvent> publishedFlux;
private final FluxSink<InstanceEvent> sink;
private TestNotifier() {
UnicastProcessor<InstanceEvent> unicastProcessor = UnicastProcessor.create();
this.publishedFlux = unicastProcessor.publish().autoConnect(0);
this.sink = unicastProcessor.sink();
}
@Override @Override
public Mono<Void> notify(InstanceEvent event) { public Mono<Void> notify(InstanceEvent event) {
this.events.add(event); this.sink.next(event);
return null; return Mono.empty();
} }
public List<InstanceEvent> getEvents() { public Flux<InstanceEvent> getFlux() {
return events; return publishedFlux;
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment