Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,37 @@
/**
* An implementation of the {@link StreamingCondition} that will start
* {@link StreamableEventSource#open(StreamingCondition) streaming} from the given {@code position}.
* <p>
* A {@code null} {@code position} is normalized to {@link TrackingToken#FIRST}, so {@link #position()} itself never
* returns {@code null}.
*
* @param position The {@link TrackingToken} describing the position to start streaming from.
* @param position The {@link TrackingToken} describing the position to start streaming from, or {@code null} to
* start from {@link TrackingToken#FIRST}.
* @author Steven van Beelen
* @since 5.0.0
*/
record StartingFrom(@Nullable TrackingToken position) implements StreamingCondition {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this confusing, as this mixes two concerns: The StartingFrom property position is NonNull as it defaults to FIRST. The constructor argument StartingFrom() is nullable.

Using the record like this makes the property nullable, leading to null conflict in position() (which returns the nullable property as a non-null getter).

So: I guess javas record behaviour does not fit and I would vouch for turning this into a class:

public final class StartingFrom implements StreamingCondition {

    private final TrackingToken position;

    public StartingFrom(@Nullable TrackingToken position) {
        this.position = position != null ? position : TrackingToken.FIRST;
    }

    /*
     * Explicitly overridden so the accessor does not inherit the @Nullable annotation carried by the position
     * record component below, which is only there to let the compact constructor accept a null input.
     */
    @Override
    public TrackingToken position() {
        return position;
    }
   ....
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could indeed be a class to make this more clear, I think that would be backwards compatible, but I have to check.


StartingFrom {
position = position != null ? position : TrackingToken.FIRST;
}

/*
* Explicitly overridden so the accessor does not inherit the @Nullable annotation carried by the position
* record component below, which is only there to let the compact constructor accept a null input.
*/
@Override
public TrackingToken position() {
return position;
}

@Override
public StreamingCondition withCriteria(EventCriteria criteria) {
if (position == null) {
throw new IllegalArgumentException("The position may not be null when adding criteria to it");
}
return new DefaultStreamingCondition(position, criteria);
}

@Override
public StreamingCondition or(EventCriteria criteria) {
if (position == null) {
throw new IllegalArgumentException("The position may not be null when adding criteria to it");
}
return new DefaultStreamingCondition(position, criteria);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public AsyncInMemoryStreamableEventSource(boolean streamCallbackSupported, boole
public @NonNull CompletableFuture<TrackingToken> latestToken(@Nullable ProcessingContext context) {
return CompletableFuture.completedFuture(
eventStorage.isEmpty()
? null
? new GlobalSequenceTrackingToken(-1)
: new GlobalSequenceTrackingToken(eventStorage.lastKey() + 1)
);
}
Expand Down Expand Up @@ -270,18 +270,11 @@ public AsyncMessageStream(StreamingCondition condition) {
return;
}

// Properly handle tracking token position (unlike the original implementation)
TrackingToken startToken = condition.position();
if (startToken == null) {
// Start from the beginning
this.currentPosition = new AtomicLong(0);
} else {
// Events are published with a token one higher than their storage index, so a token's position is the
// index of the first event that has not been consumed yet. Matching the InMemoryEventStorageEngine,
// which opens a stream at condition.position() directly.
long tokenPosition = startToken.position().orElse(-1);
this.currentPosition = new AtomicLong(Math.max(0, tokenPosition));
}
// Events are published with a token one higher than their storage index, so a token's position is the
// index of the first event that has not been consumed yet. Matching the InMemoryEventStorageEngine,
// which opens a stream at condition.position() directly.
long tokenPosition = condition.position().position().orElse(-1);
this.currentPosition = new AtomicLong(Math.max(0, tokenPosition));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.axonframework.messaging.eventstreaming;

import org.axonframework.messaging.eventhandling.processing.streaming.token.GlobalSequenceTrackingToken;
import org.axonframework.messaging.eventhandling.processing.streaming.token.TrackingToken;
import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -57,12 +58,25 @@ void orReplacesTheMatchAnyCriteriaWithTheGivenCriteria() {
}

@Test
void orThrowsIllegalArgumentExceptionWhenPositionIsNull() {
// given a StartingFrom without a position
void nullPositionIsNormalizedToFirst() {
// when a StartingFrom is constructed without a position
StreamingCondition nullPositionTestSubject = StreamingCondition.startingFrom(null);

// when combining criteria, then it is rejected as criteria cannot ride on a null position
assertThrows(IllegalArgumentException.class, () -> nullPositionTestSubject.or(TEST_CRITERIA));
// then the position defaults to TrackingToken.FIRST rather than staying null
assertEquals(TrackingToken.FIRST, nullPositionTestSubject.position());
}

@Test
void orCombinesCriteriaWhenPositionWasNull() {
// given a StartingFrom constructed without a position
StreamingCondition nullPositionTestSubject = StreamingCondition.startingFrom(null);

// when combining criteria
StreamingCondition result = nullPositionTestSubject.or(TEST_CRITERIA);

// then the normalized FIRST position and the given criteria are used
assertEquals(TrackingToken.FIRST, result.position());
assertEquals(TEST_CRITERIA, result.criteria());
}

@Test
Expand All @@ -79,11 +93,15 @@ void withCriteriaSetsTheGivenCriteriaPreservingThePosition() {
}

@Test
void withCriteriaThrowsIllegalArgumentExceptionWhenPositionIsNull() {
// given a StartingFrom without a position
void withCriteriaSetsTheGivenCriteriaWhenPositionWasNull() {
// given a StartingFrom constructed without a position
StreamingCondition nullPositionTestSubject = StreamingCondition.startingFrom(null);

// when replacing criteria, then it is rejected as criteria cannot ride on a null position
assertThrows(IllegalArgumentException.class, () -> nullPositionTestSubject.withCriteria(TEST_CRITERIA));
// when its criteria is replaced
StreamingCondition result = nullPositionTestSubject.withCriteria(TEST_CRITERIA);

// then the normalized FIRST position and the given criteria are used
assertEquals(TrackingToken.FIRST, result.position());
assertEquals(TEST_CRITERIA, result.criteria());
}
}
Loading