Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: event-sourced entity testkit support for initial events/state #127

Merged
merged 8 commits into from
Jan 13, 2025

Conversation

leviramsey
Copy link
Contributor

When testing an entity, it's generally required to put the entity into a given state (essentially the "given" in a "given-when-then" or the "arrange" in an "arrange-act-assert").

Presently the testkit supports doing this only by executing calls against the entity. In many situations, this is sufficient and captures the real-world encapsulation of the entity.

One limitation of this is that it can only test states which are reachable by the present set of calls and the events persistable from those calls.

This limitation becomes apparent if previously deployed code had a bug resulting in persistence of some sequence of events which could only be persisted before the bug was fixed. Since the persisted events are "part of the permanent record", part of the fix must necessarily be adjusting the entity's logic to behave as well as possible if that sequence of events was persisted (e.g. changing the state those events resulted in, or changing how future calls are handled in such a state). However, changing the entity's logic such that such a sequence of events is not persisted would mean that there could be no regression testing for the actual fix. There is a workaround: leave some logic in the entity which would persist the otherwise impossible sequence of events (the general form of this would be a method in the entity to unconditionally persist given events, which could be an "attractive nuisance" (especially in a larger team)).

Since the logic in the entity is only present for the purpose of tests, we can preserve the encapsulation (at least outside of tests) by instead allowing the testkit to be initialized into provided states, which is what this change implements.

With this change, the initial state for the testkit can be defined in terms of events (which will be treated as if they were read from persistence before any calls are made against the entity, viz. the initial state will be the left-fold over those events) or in terms of an initial state.

Copy link
Member

@patriknw patriknw left a comment

Choose a reason for hiding this comment

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

looking good

@leviramsey leviramsey force-pushed the given-events-or-state branch from 018ca01 to e8a8db9 Compare January 9, 2025 12:27
@github-actions github-actions bot added the documentation documentation related label Jan 9, 2025
@leviramsey leviramsey changed the base branch from main to java-spi January 9, 2025 12:28
Copy link
Contributor

@aludwiko aludwiko left a comment

Choose a reason for hiding this comment

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

LGTM

@github-actions github-actions bot removed the documentation documentation related label Jan 11, 2025
@leviramsey
Copy link
Contributor Author

Similar can probably be done for KV entities, but that's not as personally relevant for me :)

@octonato
Copy link
Member

Similar can probably be done for KV entities, but that's not as personally relevant for me :)

Yeah, it will be good to align the testkits and have the means to provide a initial state to a KVE as well.

Copy link
Member

@octonato octonato left a comment

Choose a reason for hiding this comment

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

Let's merge this one for now and add the same on KVE on follow-up.

@octonato octonato merged commit a7630cc into akka:java-spi Jan 13, 2025
22 checks passed
@leviramsey leviramsey deleted the given-events-or-state branch January 13, 2025 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants