Search in sources :

Example 6 with InstanceEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceEvent in project spring-boot-admin by codecentric.

the class AbstractEventStoreTest method should_throw_optimictic_locking_exception.

@Test
public void should_throw_optimictic_locking_exception() {
    InstanceEvent event0 = new InstanceRegisteredEvent(id, 0L, registration);
    InstanceEvent event1 = new InstanceStatusChangedEvent(id, 1L, StatusInfo.ofDown());
    InstanceEvent event1b = new InstanceDeregisteredEvent(id, 1L);
    InstanceEventStore store = createStore(100);
    StepVerifier.create(store.append(asList(event0, event1))).verifyComplete();
    StepVerifier.create(store.append(singletonList(event1b))).verifyError(OptimisticLockingException.class);
}
Also used : InstanceRegisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent) InstanceDeregisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Test(org.junit.jupiter.api.Test)

Example 7 with InstanceEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceEvent in project spring-boot-admin by codecentric.

the class AbstractEventStoreTest method should_store_events.

@Test
public void should_store_events() {
    InstanceEventStore store = createStore(100);
    StepVerifier.create(store.findAll()).verifyComplete();
    Instant now = Instant.now();
    InstanceEvent event1 = new InstanceRegisteredEvent(id, 0L, now, registration);
    InstanceEvent eventOther = new InstanceRegisteredEvent(InstanceId.of("other"), 0L, now.plusMillis(10), registration);
    InstanceEvent event2 = new InstanceDeregisteredEvent(id, 1L, now.plusMillis(20));
    StepVerifier.create(store).expectSubscription().then(() -> StepVerifier.create(store.append(singletonList(event1))).verifyComplete()).expectNext(event1).then(() -> StepVerifier.create(store.append(singletonList(eventOther))).verifyComplete()).expectNext(eventOther).then(() -> StepVerifier.create(store.append(singletonList(event2))).verifyComplete()).expectNext(event2).thenCancel().verify();
    StepVerifier.create(store.find(id)).expectNext(event1, event2).verifyComplete();
    StepVerifier.create(store.find(InstanceId.of("-"))).verifyComplete();
    StepVerifier.create(store.findAll()).expectNext(event1, eventOther, event2).verifyComplete();
}
Also used : InstanceRegisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent) Instant(java.time.Instant) InstanceDeregisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent) Test(org.junit.jupiter.api.Test)

Example 8 with InstanceEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceEvent in project spring-boot-admin by codecentric.

the class Instance method apply.

private Instance apply(InstanceEvent event, boolean isNewEvent) {
    Assert.notNull(event, "'event' must not be null");
    Assert.isTrue(this.id.equals(event.getInstance()), "'event' must refer the same instance");
    Assert.isTrue(event.getVersion() >= this.nextVersion(), () -> "Event " + event.getVersion() + " must be greater or equal to " + this.nextVersion());
    List<InstanceEvent> unsavedEvents = appendToEvents(event, isNewEvent);
    if (event instanceof InstanceRegisteredEvent) {
        Registration registration = ((InstanceRegisteredEvent) event).getRegistration();
        return new Instance(this.id, event.getVersion(), registration, true, StatusInfo.ofUnknown(), event.getTimestamp(), Info.empty(), Endpoints.empty(), updateBuildVersion(registration.getMetadata()), updateTags(registration.getMetadata()), unsavedEvents);
    } else if (event instanceof InstanceRegistrationUpdatedEvent) {
        Registration registration = ((InstanceRegistrationUpdatedEvent) event).getRegistration();
        return new Instance(this.id, event.getVersion(), registration, this.registered, this.statusInfo, this.statusTimestamp, this.info, this.endpoints, updateBuildVersion(registration.getMetadata(), this.info.getValues()), updateTags(registration.getMetadata(), this.info.getValues()), unsavedEvents);
    } else if (event instanceof InstanceStatusChangedEvent) {
        StatusInfo statusInfo = ((InstanceStatusChangedEvent) event).getStatusInfo();
        return new Instance(this.id, event.getVersion(), this.registration, this.registered, statusInfo, event.getTimestamp(), this.info, this.endpoints, this.buildVersion, this.tags, unsavedEvents);
    } else if (event instanceof InstanceEndpointsDetectedEvent) {
        Endpoints endpoints = ((InstanceEndpointsDetectedEvent) event).getEndpoints();
        return new Instance(this.id, event.getVersion(), this.registration, this.registered, this.statusInfo, this.statusTimestamp, this.info, endpoints, this.buildVersion, this.tags, unsavedEvents);
    } else if (event instanceof InstanceInfoChangedEvent) {
        Info info = ((InstanceInfoChangedEvent) event).getInfo();
        Map<String, ?> metaData = (this.registration != null) ? this.registration.getMetadata() : emptyMap();
        return new Instance(this.id, event.getVersion(), this.registration, this.registered, this.statusInfo, this.statusTimestamp, info, this.endpoints, updateBuildVersion(metaData, info.getValues()), updateTags(metaData, info.getValues()), unsavedEvents);
    } else if (event instanceof InstanceDeregisteredEvent) {
        return new Instance(this.id, event.getVersion(), this.registration, false, StatusInfo.ofUnknown(), event.getTimestamp(), Info.empty(), Endpoints.empty(), null, Tags.empty(), unsavedEvents);
    }
    return this;
}
Also used : InstanceRegisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent) InstanceInfoChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent) InstanceEndpointsDetectedEvent(de.codecentric.boot.admin.server.domain.events.InstanceEndpointsDetectedEvent) Info(de.codecentric.boot.admin.server.domain.values.Info) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) InstanceRegistrationUpdatedEvent(de.codecentric.boot.admin.server.domain.events.InstanceRegistrationUpdatedEvent) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent) InstanceStatusChangedEvent(de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent) Endpoints(de.codecentric.boot.admin.server.domain.values.Endpoints) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) Registration(de.codecentric.boot.admin.server.domain.values.Registration) InstanceDeregisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent)

Example 9 with InstanceEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceEvent in project spring-boot-admin by codecentric.

the class InstanceTest method should_throw_when_applied_wrong_event.

@Test
public void should_throw_when_applied_wrong_event() {
    Instance instance = Instance.create(InstanceId.of("id"));
    assertThatThrownBy(() -> instance.apply((InstanceEvent) null)).isInstanceOf(IllegalArgumentException.class).hasMessage("'event' must not be null");
    assertThatThrownBy(() -> instance.apply(new InstanceDeregisteredEvent(InstanceId.of("wrong"), 0L))).isInstanceOf(IllegalArgumentException.class).hasMessage("'event' must refer the same instance");
    assertThatThrownBy(() -> instance.apply(new InstanceDeregisteredEvent(InstanceId.of("id"), 1L)).apply(new InstanceDeregisteredEvent(InstanceId.of("id"), 1L))).isInstanceOf(IllegalArgumentException.class).hasMessage("Event 1 must be greater or equal to 2");
}
Also used : InstanceDeregisteredEvent(de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent) Test(org.junit.jupiter.api.Test)

Example 10 with InstanceEvent

use of de.codecentric.boot.admin.server.domain.events.InstanceEvent in project spring-boot-admin by codecentric.

the class InstanceTest method should_track_unsaved_events.

@Test
public void should_track_unsaved_events() {
    Registration registration = Registration.create("foo", "http://health").build();
    Info info = Info.from(singletonMap("foo", "bar"));
    Instance newInstance = Instance.create(InstanceId.of("id"));
    assertThat(newInstance.isRegistered()).isFalse();
    assertThatThrownBy(newInstance::getRegistration).isInstanceOf(IllegalStateException.class);
    assertThat(newInstance.getInfo()).isEqualTo(Info.empty());
    assertThat(newInstance.getStatusInfo()).isEqualTo(StatusInfo.ofUnknown());
    assertThat(newInstance.getUnsavedEvents()).isEmpty();
    Instance instance = newInstance.register(registration).register(registration);
    assertThat(instance.getRegistration()).isEqualTo(registration);
    assertThat(instance.isRegistered()).isTrue();
    assertThat(instance.getVersion()).isEqualTo(0L);
    Registration registration2 = Registration.create("foo2", "http://health").build();
    instance = instance.register(registration2);
    assertThat(instance.getRegistration()).isEqualTo(registration2);
    assertThat(instance.isRegistered()).isTrue();
    assertThat(instance.getVersion()).isEqualTo(1L);
    instance = instance.withStatusInfo(StatusInfo.ofUp()).withStatusInfo(StatusInfo.ofUp());
    assertThat(instance.getStatusInfo()).isEqualTo(StatusInfo.ofUp());
    assertThat(instance.getVersion()).isEqualTo(2L);
    instance = instance.withInfo(info).withInfo(info);
    assertThat(instance.getInfo()).isEqualTo(info);
    assertThat(instance.getVersion()).isEqualTo(3L);
    instance = instance.deregister().deregister();
    assertThat(instance.isRegistered()).isFalse();
    assertThat(instance.getRegistration()).isEqualTo(registration2);
    assertThat(instance.getInfo()).isEqualTo(Info.empty());
    assertThat(instance.getStatusInfo()).isEqualTo(StatusInfo.ofUnknown());
    assertThat(instance.getVersion()).isEqualTo(4L);
    assertThat(instance.getUnsavedEvents().stream().map(InstanceEvent::getType)).containsExactly("REGISTERED", "REGISTRATION_UPDATED", "STATUS_CHANGED", "INFO_CHANGED", "DEREGISTERED");
}
Also used : Registration(de.codecentric.boot.admin.server.domain.values.Registration) Info(de.codecentric.boot.admin.server.domain.values.Info) StatusInfo(de.codecentric.boot.admin.server.domain.values.StatusInfo) InstanceEvent(de.codecentric.boot.admin.server.domain.events.InstanceEvent) Test(org.junit.jupiter.api.Test)

Aggregations

InstanceEvent (de.codecentric.boot.admin.server.domain.events.InstanceEvent)11 Test (org.junit.jupiter.api.Test)8 InstanceDeregisteredEvent (de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent)5 InstanceRegisteredEvent (de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent)5 InstanceStatusChangedEvent (de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent)5 Registration (de.codecentric.boot.admin.server.domain.values.Registration)3 StatusInfo (de.codecentric.boot.admin.server.domain.values.StatusInfo)3 Instance (de.codecentric.boot.admin.server.domain.entities.Instance)2 Info (de.codecentric.boot.admin.server.domain.values.Info)2 InstanceId (de.codecentric.boot.admin.server.domain.values.InstanceId)2 Instant (java.time.Instant)2 List (java.util.List)2 Function (java.util.function.Function)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Flux (reactor.core.publisher.Flux)2 Mono (reactor.core.publisher.Mono)2 InstanceEndpointsDetectedEvent (de.codecentric.boot.admin.server.domain.events.InstanceEndpointsDetectedEvent)1 InstanceInfoChangedEvent (de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent)1 InstanceRegistrationUpdatedEvent (de.codecentric.boot.admin.server.domain.events.InstanceRegistrationUpdatedEvent)1