Skip to content

Commit

Permalink
GH-368 - Polishing.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Nov 14, 2023
1 parent 80e53ac commit 9f42ae4
Showing 1 changed file with 48 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,74 +47,70 @@ class PersistentDomainEventIntegrationTest {
@Test
void exposesEventPublicationForFailedListener() throws Exception {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().getPropertySources().addFirst(
new MapPropertySource("test", Map.of("spring.modulith.republish-outstanding-events-on-restart", "true")));
context.register(ApplicationConfiguration.class, InfrastructureConfiguration.class);
context.refresh();
try (var context = new AnnotationConfigApplicationContext()) {

EventPublicationRegistry registry = context.getBean(EventPublicationRegistry.class);
context.getEnvironment().getPropertySources().addFirst(
new MapPropertySource("test", Map.of("spring.modulith.republish-outstanding-events-on-restart", "true")));
context.register(ApplicationConfiguration.class, InfrastructureConfiguration.class);
context.refresh();

try {
var registry = context.getBean(EventPublicationRegistry.class);

context.getBean(Client.class).method();
try {

Thread.sleep(200);

assertThat(context.getBean(NonTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FirstTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(SecondTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(ThirdTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FourthTxEventListener.class).getInvoked()).isEqualTo(1);

} catch (Throwable e) {
context.getBean(Client.class).method();

System.out.println(e);
Thread.sleep(200);

} finally {
assertThat(context.getBean(NonTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FirstTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(SecondTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(ThirdTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FourthTxEventListener.class).getInvoked()).isEqualTo(1);

assertThat(registry.findIncompletePublications()) //
.extracting(TargetEventPublication::getTargetIdentifier) //
.extracting(PublicationTargetIdentifier::getValue) //
.hasSize(2) //
.allSatisfy(id -> {
assertThat(id)
.matches(it -> //
it.contains(SecondTxEventListener.class.getName()) //
|| it.contains(FourthTxEventListener.class.getName()));
});
} finally {

}
assertThat(registry.findIncompletePublications()) //
.extracting(TargetEventPublication::getTargetIdentifier) //
.extracting(PublicationTargetIdentifier::getValue) //
.hasSize(2) //
.allSatisfy(id -> {
assertThat(id)
.matches(it -> //
it.contains(SecondTxEventListener.class.getName()) //
|| it.contains(FourthTxEventListener.class.getName()));
});

// Resubmit failed publications
var incompletePublications = context.getBean(IncompleteEventPublications.class);
}

incompletePublications.resubmitIncompletePublications(__ -> true);
// Resubmit failed publications
var incompletePublications = context.getBean(IncompleteEventPublications.class);

Thread.sleep(200);
incompletePublications.resubmitIncompletePublications(__ -> true);

assertThat(context.getBean(NonTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FirstTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(SecondTxEventListener.class).getInvoked()).isEqualTo(2);
assertThat(context.getBean(ThirdTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FourthTxEventListener.class).getInvoked()).isEqualTo(2);
Thread.sleep(200);

// Still 2 uncompleted publications
assertThat(registry.findIncompletePublications()).hasSize(2);
assertThat(context.getBean(NonTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FirstTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(SecondTxEventListener.class).getInvoked()).isEqualTo(2);
assertThat(context.getBean(ThirdTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FourthTxEventListener.class).getInvoked()).isEqualTo(2);

incompletePublications.resubmitIncompletePublications(it -> {
return TargetEventPublication.class.cast(it)
.getTargetIdentifier()
.getValue().contains(SecondTxEventListener.class.getName());
});
// Still 2 uncompleted publications
assertThat(registry.findIncompletePublications()).hasSize(2);

assertThat(context.getBean(NonTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FirstTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(SecondTxEventListener.class).getInvoked()).isEqualTo(3);
assertThat(context.getBean(ThirdTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FourthTxEventListener.class).getInvoked()).isEqualTo(2);
incompletePublications.resubmitIncompletePublications(it -> {
return TargetEventPublication.class.cast(it)
.getTargetIdentifier()
.getValue().contains(SecondTxEventListener.class.getName());
});

context.close();
assertThat(context.getBean(NonTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FirstTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(SecondTxEventListener.class).getInvoked()).isEqualTo(3);
assertThat(context.getBean(ThirdTxEventListener.class).getInvoked()).isEqualTo(1);
assertThat(context.getBean(FourthTxEventListener.class).getInvoked()).isEqualTo(2);
}
}

@Configuration
Expand Down

0 comments on commit 9f42ae4

Please sign in to comment.