Skip to content

Commit a9365ed

Browse files
committed
GH-1056 - Switch to object identity comparison when looking up event publications.
We now rather use an object identity comparison in TargetEventPublication.isAssociatedWith(…) instead of an ….equals(…) as that would return the wrong publication for identical events.
1 parent 19db233 commit a9365ed

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

spring-modulith-events/spring-modulith-events-core/src/main/java/org/springframework/modulith/events/core/TargetEventPublication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ default boolean isIdentifiedBy(PublicationTargetIdentifier identifier) {
7575
}
7676

7777
/**
78-
* Returns whether the {@link TargetEventPublication} is associated with the given event and
78+
* Returns whether the {@link TargetEventPublication} is associated with the given event instance and
7979
* {@link PublicationTargetIdentifier}.
8080
*
8181
* @param event must not be {@literal null}.
@@ -87,6 +87,6 @@ default boolean isAssociatedWith(Object event, PublicationTargetIdentifier ident
8787
Assert.notNull(event, "Event must not be null!");
8888
Assert.notNull(identifier, "PublicationTargetIdentifier must not be null!");
8989

90-
return isIdentifiedBy(identifier) && getEvent().equals(event);
90+
return isIdentifiedBy(identifier) && getEvent() == event;
9191
}
9292
}

spring-modulith-events/spring-modulith-events-core/src/test/java/org/springframework/modulith/events/core/DefaultEventPublicationRegistryUnitTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ void removesFailingResubmissionFromInProgressPublications() {
8282
assertThat(registry.getPublicationsInProgress()).isEmpty();
8383
}
8484

85+
@Test // GH-1056
86+
void obtainsCorrectInProgressPublicationForIdenticalEvents() {
87+
88+
var inProgress = createRegistry(Instant.now()).getPublicationsInProgress();
89+
90+
var identifier = PublicationTargetIdentifier.of("id");
91+
92+
var firstEvent = new SampleEvent("Foo");
93+
var secondEvent = new SampleEvent("Foo");
94+
95+
var first = inProgress.register(TargetEventPublication.of(firstEvent, identifier));
96+
var second = inProgress.register(TargetEventPublication.of(secondEvent, identifier));
97+
98+
assertThat(inProgress.getPublication(firstEvent, identifier)).containsSame(first);
99+
assertThat(inProgress.getPublication(secondEvent, identifier)).containsSame(second);
100+
101+
}
102+
85103
private DefaultEventPublicationRegistry createRegistry(Instant instant) {
86104

87105
var clock = Clock.fixed(instant, ZoneId.systemDefault());
@@ -94,4 +112,6 @@ private Consumer<TargetEventPublication> failingConsumer() {
94112
throw new IllegalStateException();
95113
};
96114
}
115+
116+
record SampleEvent(String payload) {}
97117
}

spring-modulith-events/spring-modulith-events-core/src/test/java/org/springframework/modulith/events/core/TargetEventPublicationUnitTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,19 @@ void publicationIsIncompleteByDefault() {
5151
assertThat(publication.isCompleted()).isFalse();
5252
assertThat(publication.getCompletionDate()).isNotPresent();
5353
}
54+
55+
@Test // GH-1056
56+
void isOnlyAssociatedWithTheVerySameEventInstance() {
57+
58+
var first = new SampleEvent("Foo");
59+
var second = new SampleEvent("Foo");
60+
61+
var identifier = PublicationTargetIdentifier.of("id");
62+
var publication = TargetEventPublication.of(first, identifier);
63+
64+
assertThat(publication.isAssociatedWith(first, identifier)).isTrue();
65+
assertThat(publication.isAssociatedWith(second, identifier)).isFalse();
66+
}
67+
68+
record SampleEvent(String payload) {}
5469
}

0 commit comments

Comments
 (0)