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);
}
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();
}
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;
}
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");
}
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");
}
Aggregations