Skip to content

Commit

Permalink
fix unstable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshiste committed Mar 12, 2018
1 parent 38452e4 commit 5bb3d17
Showing 1 changed file with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@
import de.codecentric.boot.admin.server.notify.PagerdutyNotifier;
import de.codecentric.boot.admin.server.notify.SlackNotifier;
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.UnicastProcessor;
import reactor.test.StepVerifier;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Expand All @@ -63,18 +65,16 @@ public void close() {
}

@Test
public void test_notifierListener() throws InterruptedException {
public void test_notifierListener() {
load(TestSingleNotifierConfig.class);
InstanceEventStore store = context.getBean(InstanceEventStore.class);

StepVerifier.create(store)
StepVerifier.create(context.getBean(TestNotifier.class).getFlux())
.expectSubscription()
.then(() -> StepVerifier.create(store.append(Collections.singletonList(APP_DOWN))).verifyComplete())
.expectNext(APP_DOWN)
.thenCancel()
.verify();
Thread.sleep(100); //wait for the notifications in different thread
assertThat(context.getBean(TestNotifier.class).getEvents()).containsOnly(APP_DOWN);
}

@Test
Expand Down Expand Up @@ -161,7 +161,8 @@ private void load(Class<?> config, String... environment) {

public static class TestSingleNotifierConfig {
@Bean
public Notifier testNotifier() {
@Qualifier("testNotifier")
public TestNotifier testNotifier() {
return new TestNotifier();
}
}
Expand All @@ -175,40 +176,51 @@ public JavaMailSenderImpl mailSender() {

private static class TestMultipleNotifierConfig {
@Bean
public Notifier testNotifier1() {
@Qualifier("testNotifier1")
public TestNotifier testNotifier1() {
return new TestNotifier();
}

@Bean
public Notifier testNotifier2() {
@Qualifier("testNotifier2")
public TestNotifier testNotifier2() {
return new TestNotifier();
}
}

private static class TestMultipleWithPrimaryNotifierConfig {
@Bean
@Primary
public Notifier testNotifierPrimary() {
@Qualifier("testNotifier")
public TestNotifier testNotifierPrimary() {
return new TestNotifier();
}

@Bean
public Notifier testNotifier2() {
@Qualifier("testNotifier3")
public TestNotifier testNotifier2() {
return new TestNotifier();
}
}

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
public Mono<Void> notify(InstanceEvent event) {
this.events.add(event);
return null;
this.sink.next(event);
return Mono.empty();
}

public List<InstanceEvent> getEvents() {
return events;
public Flux<InstanceEvent> getFlux() {
return publishedFlux;
}
}
}

0 comments on commit 5bb3d17

Please sign in to comment.