Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spring-boot-admin-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,10 @@
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Stream;
Expand Down Expand Up @@ -47,6 +48,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.awaitility.Awaitility.await;

public abstract class AbstractClientApplicationTest {

Expand All @@ -59,7 +61,7 @@ public abstract class AbstractClientApplicationTest {

private static final CountDownLatch cdl = new CountDownLatch(1);

protected void setUp(WebApplicationType type) throws Exception {
protected void setUp(WebApplicationType type) {
setUpWiremock();
setUpApplication(type);
}
Expand Down Expand Up @@ -100,8 +102,6 @@ void tearDown() {
public void test_context() throws InterruptedException, UnknownHostException {
setUpApplicationContext();

cdl.await();
Thread.sleep(2500);
String hostName = InetAddress.getLocalHost().getCanonicalHostName();
String serviceHost = "http://" + hostName + ":" + getServerPort();
String managementHost = "http://" + hostName + ":" + getManagementPort();
Expand All @@ -113,15 +113,14 @@ public void test_context() throws InterruptedException, UnknownHostException {
.withRequestBody(matchingJsonPath("$.serviceUrl", equalTo(serviceHost + "/")))
.withRequestBody(matchingJsonPath("$.metadata.startup", matching(".+")));

wireMock.verify(request);
cdl.await();
await().atMost(Duration.ofMillis(2500)).untilAsserted(() -> wireMock.verify(request));
}

@Test
public void test_context_with_snake_case() throws InterruptedException, UnknownHostException {
setUpApplicationContext("--spring.jackson.property-naming-strategy=SNAKE_CASE");

cdl.await();
Thread.sleep(2500);
String hostName = InetAddress.getLocalHost().getCanonicalHostName();
String serviceHost = "http://" + hostName + ":" + getServerPort();
String managementHost = "http://" + hostName + ":" + getManagementPort();
Expand All @@ -133,7 +132,8 @@ public void test_context_with_snake_case() throws InterruptedException, UnknownH
.withRequestBody(matchingJsonPath("$.service_url", equalTo(serviceHost + "/")))
.withRequestBody(matchingJsonPath("$.metadata.startup", matching(".+")));

wireMock.verify(request);
cdl.await();
await().atMost(Duration.ofMillis(2500)).untilAsserted(() -> wireMock.verify(request));
}

private int getServerPort() {
Expand All @@ -154,14 +154,7 @@ public static class TestClientApplication {
@EventListener
public void ping(ApplicationReadyEvent ev) {
new Thread(() -> {
try {
while (registrator.getRegisteredId() == null) {
Thread.sleep(500);
}
}
catch (InterruptedException ex) {
Thread.interrupted();
}
await().atMost(Duration.ofMillis(500)).until(() -> registrator.getRegisteredId() != null);
cdl.countDown();
}).start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
Expand All @@ -38,11 +40,12 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class RemindingNotifierTest {
class RemindingNotifierTest {

private static final Instance instance1 = Instance.create(InstanceId.of("id-1"))
.register(Registration.create("App", "http://health").build())
Expand Down Expand Up @@ -71,38 +74,40 @@ public class RemindingNotifierTest {
private InstanceRepository repository;

@BeforeEach
public void setUp() {
void setUp() {
this.repository = mock(InstanceRepository.class);
when(this.repository.find(any())).thenReturn(Mono.empty());
when(this.repository.find(instance1.getId())).thenReturn(Mono.just(instance1));
when(this.repository.find(instance2.getId())).thenReturn(Mono.just(instance2));
}

@Test
public void should_throw_on_invalid_ctor() {
assertThatThrownBy(() -> new CompositeNotifier(null)).isInstanceOf(IllegalArgumentException.class);
@ParameterizedTest
@NullSource
void should_throw_on_invalid_ctor(Iterable<Notifier> delegates) {
assertThatThrownBy(() -> new CompositeNotifier(delegates)).isInstanceOf(IllegalArgumentException.class);
}

@Test
public void should_remind_only_down_events() throws InterruptedException {
void should_remind_only_down_events() {
TestNotifier notifier = new TestNotifier();
RemindingNotifier reminder = new RemindingNotifier(notifier, this.repository);
reminder.setReminderPeriod(Duration.ZERO);

StepVerifier.create(reminder.notify(appDown)).verifyComplete();
StepVerifier.create(reminder.notify(appEndpointsDiscovered)).verifyComplete();
StepVerifier.create(reminder.notify(otherAppUp)).verifyComplete();
Thread.sleep(10);
StepVerifier.create(reminder.sendReminders()).verifyComplete();
Thread.sleep(10);
StepVerifier.create(reminder.sendReminders()).verifyComplete();

await().pollDelay(Duration.ofMillis(10))
.untilAsserted(() -> StepVerifier.create(reminder.sendReminders()).verifyComplete());
await().pollDelay(Duration.ofMillis(10))
.untilAsserted(() -> StepVerifier.create(reminder.sendReminders()).verifyComplete());

assertThat(notifier.getEvents()).containsExactlyInAnyOrder(appDown, appEndpointsDiscovered, otherAppUp, appDown,
appDown);
}

@Test
public void should_not_remind_remind_after_up() {
void should_not_remind_remind_after_up() {
TestNotifier notifier = new TestNotifier();
RemindingNotifier reminder = new RemindingNotifier(notifier, this.repository);
reminder.setReminderPeriod(Duration.ZERO);
Expand All @@ -115,7 +120,7 @@ public void should_not_remind_remind_after_up() {
}

@Test
public void should_not_remind_remind_after_deregister() {
void should_not_remind_remind_after_deregister() {
TestNotifier notifier = new TestNotifier();
RemindingNotifier reminder = new RemindingNotifier(notifier, this.repository);
reminder.setReminderPeriod(Duration.ZERO);
Expand All @@ -128,7 +133,7 @@ public void should_not_remind_remind_after_deregister() {
}

@Test
public void should_not_remind_remind_before_period_ends() {
void should_not_remind_remind_before_period_ends() {
TestNotifier notifier = new TestNotifier();
RemindingNotifier reminder = new RemindingNotifier(notifier, this.repository);
reminder.setReminderPeriod(Duration.ofHours(24));
Expand All @@ -140,7 +145,7 @@ public void should_not_remind_remind_before_period_ends() {
}

@Test
public void should_resubscribe_after_error() {
void should_resubscribe_after_error() {
TestPublisher<InstanceEvent> eventPublisher = TestPublisher.create();

Flux<InstanceEvent> emittedNotifications = Flux.create((emitter) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class InfoUpdateTriggerTest {
class InfoUpdateTriggerTest {

private final Instance instance = Instance.create(InstanceId.of("id-1"))
.register(Registration.create("foo", "http://health-1").build());
Expand All @@ -57,7 +57,7 @@ public class InfoUpdateTriggerTest {
private InfoUpdateTrigger trigger;

@BeforeEach
public void setUp() throws Exception {
void setUp() {
when(this.updater.updateInfo(any(InstanceId.class))).thenReturn(Mono.empty());

this.trigger = new InfoUpdateTrigger(this.updater, this.events.flux(), Duration.ofMinutes(5),
Expand All @@ -67,7 +67,7 @@ public void setUp() throws Exception {
}

@Test
public void should_start_and_stop_monitor() throws Exception {
void should_start_and_stop_monitor() {
// given
this.trigger.stop();
this.trigger.setInterval(Duration.ofMillis(10));
Expand All @@ -77,33 +77,30 @@ public void should_start_and_stop_monitor() throws Exception {

this.events.next(
new InstanceStatusChangedEvent(this.instance.getId(), this.instance.getVersion(), StatusInfo.ofDown()));

Thread.sleep(50L);
// then it should start updating one time for registration and at least once for
// monitor
verify(this.updater, atLeast(2)).updateInfo(this.instance.getId());
await().atMost(Duration.ofMillis(50))
.pollInterval(Duration.ofMillis(10))
.untilAsserted(() -> verify(this.updater, atLeast(2)).updateInfo(this.instance.getId()));

// given long lifetime
this.trigger.setLifetime(Duration.ofSeconds(10));
Thread.sleep(50L);
clearInvocations(this.updater);
// when the lifetime is not expired
Thread.sleep(50L);
// should never update
verify(this.updater, never()).updateInfo(any(InstanceId.class));
// when the lifetime is not expired should never update
await().pollDelay(Duration.ofMillis(50))
.untilAsserted(() -> verify(this.updater, never()).updateInfo(any(InstanceId.class)));

// when trigger ist destroyed
this.trigger.setLifetime(Duration.ofMillis(10));
this.trigger.stop();
clearInvocations(this.updater);
Thread.sleep(15L);

// it should stop updating
verify(this.updater, never()).updateInfo(any(InstanceId.class));
// when trigger ist destroyed it should stop updating
await().pollDelay(Duration.ofMillis(15))
.untilAsserted(() -> verify(this.updater, never()).updateInfo(any(InstanceId.class)));
}

@Test
public void should_not_update_when_stopped() {
void should_not_update_when_stopped() {
// when registered event is emitted but the trigger has been stopped
this.trigger.stop();
clearInvocations(this.updater);
Expand All @@ -114,7 +111,7 @@ public void should_not_update_when_stopped() {
}

@Test
public void should_update_on_endpoints_detectes_event() {
void should_update_on_endpoints_detects_event() {
// when registered event is emitted
this.events.next(new InstanceEndpointsDetectedEvent(this.instance.getId(), this.instance.getVersion(),
this.instance.getEndpoints()));
Expand All @@ -123,7 +120,7 @@ public void should_update_on_endpoints_detectes_event() {
}

@Test
public void should_update_on_status_changed_event() {
void should_update_on_status_changed_event() {
// when registered event is emitted
this.events.next(
new InstanceStatusChangedEvent(this.instance.getId(), this.instance.getVersion(), StatusInfo.ofDown()));
Expand All @@ -132,7 +129,7 @@ public void should_update_on_status_changed_event() {
}

@Test
public void should_update_on_instance_registration_update_event() {
void should_update_on_instance_registration_update_event() {
// when registered event is emitted
this.events.next(new InstanceRegistrationUpdatedEvent(this.instance.getId(), this.instance.getVersion(),
this.instance.getRegistration()));
Expand All @@ -141,15 +138,15 @@ public void should_update_on_instance_registration_update_event() {
}

@Test
public void should_not_update_on_non_relevant_event() {
void should_not_update_on_non_relevant_event() {
// when some non-registered event is emitted
this.events.next(new InstanceInfoChangedEvent(this.instance.getId(), this.instance.getVersion(), Info.empty()));
// then should not update
verify(this.updater, never()).updateInfo(this.instance.getId());
}

@Test
public void should_continue_update_after_error() throws InterruptedException {
void should_continue_update_after_error() {
// when status-change event is emitted and an error is emitted
when(this.updater.updateInfo(any())).thenReturn(Mono.error(IllegalStateException::new))
.thenReturn(Mono.empty());
Expand Down
Loading
Loading