Skip to content

Commit

Permalink
Ignore old/duplicate events in projector
Browse files Browse the repository at this point in the history
  • Loading branch information
maxekman committed Dec 7, 2020
1 parent 82599db commit 9c34447
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions eventhandler/projector/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ func (h *EventHandler) HandleEvent(ctx context.Context, event eh.Event) error {

// The entity should be one version behind the event.
if entity, ok := entity.(eh.Versionable); ok {
// Ignore old/duplicate events.
if entity.AggregateVersion() >= event.Version() {
return nil
}

if entity.AggregateVersion()+1 != event.Version() {
return Error{
Err: eh.ErrIncorrectEntityVersion,
Expand Down
10 changes: 10 additions & 0 deletions eventhandler/projector/eventhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func TestEventHandler_UpdateModel(t *testing.T) {
if repo.Entity != projector.newEntity {
t.Error("the new entity should be correct:", repo.Entity)
}

// Handle event again, should be a no-op.
if err := handler.HandleEvent(ctx, event); err != nil {
t.Error("there shoud be no error:", err)
}
}

func TestEventHandler_UpdateModelWithVersion(t *testing.T) {
Expand Down Expand Up @@ -137,6 +142,11 @@ func TestEventHandler_UpdateModelWithVersion(t *testing.T) {
if repo.Entity != projector.newEntity {
t.Error("the new entity should be correct:", repo.Entity)
}

// Handle event again, should be a no-op.
if err := handler.HandleEvent(ctx, event); err != nil {
t.Error("there shoud be no error:", err)
}
}

func TestEventHandler_UpdateModelWithEventsOutOfOrder(t *testing.T) {
Expand Down

0 comments on commit 9c34447

Please sign in to comment.