Skip to content

Commit

Permalink
make use of iterator in eventstore suite
Browse files Browse the repository at this point in the history
  • Loading branch information
hallgren committed Jan 14, 2022
1 parent ac9be98 commit e944cdf
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions eventstore/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/rand"
"sync"
"testing"
"time"

Expand All @@ -26,17 +27,15 @@ func Test(t *testing.T, esFunc eventstoreFunc) {
run func(es eventsourcing.EventStore) error
}{
{"should save and get events", saveAndGetEvents},
/*
{"should get events after version", getEventsAfterVersion},
{"should not save events from different aggregates", saveEventsFromMoreThanOneAggregate},
{"should not save events from different aggregate types", saveEventsFromMoreThanOneAggregateType},
{"should not save events in wrong order", saveEventsInWrongOrder},
{"should not save events in wrong version", saveEventsInWrongVersion},
{"should not save event with no reason", saveEventsWithEmptyReason},
{"should save and get event concurrently", saveAndGetEventsConcurrently},
{"should return error when no events", getErrWhenNoEvents},
{"should get global event order from save", saveReturnGlobalEventOrder},
*/
{"should get events after version", getEventsAfterVersion},
{"should not save events from different aggregates", saveEventsFromMoreThanOneAggregate},
{"should not save events from different aggregate types", saveEventsFromMoreThanOneAggregateType},
{"should not save events in wrong order", saveEventsInWrongOrder},
{"should not save events in wrong version", saveEventsInWrongVersion},
{"should not save event with no reason", saveEventsWithEmptyReason},
{"should save and get event concurrently", saveAndGetEventsConcurrently},
{"should return error when no events", getErrWhenNoEvents},
{"should get global event order from save", saveReturnGlobalEventOrder},
}
ser := eventsourcing.NewSerializer(json.Marshal, json.Unmarshal)

Expand Down Expand Up @@ -197,11 +196,6 @@ func saveAndGetEvents(es eventsourcing.EventStore) error {
if fetchedEventsIncludingPartTwo[0].Metadata["test"] != "hello" {
return errors.New("wrong event meta data returned")
}
/*
if fetchedEventsIncludingPartTwo[0].Timestamp.Format(time.RFC3339) != timestamp.Format(time.RFC3339) {
return fmt.Errorf("wrong timestamp exp: %s got: %s", fetchedEventsIncludingPartTwo[0].Timestamp.Format(time.RFC3339), timestamp.Format(time.RFC3339))
}
*/

data, ok := fetchedEventsIncludingPartTwo[0].Data.(*FrequentFlierAccountCreated)
if !ok {
Expand All @@ -214,19 +208,27 @@ func saveAndGetEvents(es eventsourcing.EventStore) error {
return nil
}

/*
func getEventsAfterVersion(es eventsourcing.EventStore) error {
var fetchedEvents []eventsourcing.Event
aggregateID := AggregateID()
err := es.Save(testEvents(aggregateID))
if err != nil {
return err
}

fetchedEvents, err := es.Get(aggregateID, aggregateType, 1)
iterator, err := es.Get(aggregateID, aggregateType, 1)
if err != nil {
return err
}

for {
event, err := iterator.Next()
if err != nil {
break
}
fetchedEvents = append(fetchedEvents, event)
}
iterator.Close()
// Should return one less event
if len(fetchedEvents) != len(testEvents(aggregateID))-1 {
return fmt.Errorf("wrong number of events returned exp: %d, got:%d", len(fetchedEvents), len(testEvents(aggregateID))-1)
Expand Down Expand Up @@ -318,11 +320,20 @@ func saveAndGetEventsConcurrently(es eventsourcing.EventStore) error {
eventID := fmt.Sprintf("%s-%d", aggregateID, i)
go func() {
defer wg.Done()
events, e := es.Get(eventID, aggregateType, 0)
iterator, e := es.Get(eventID, aggregateType, 0)
if e != nil {
err = e
return
}
events := make([]eventsourcing.Event, 0)
for {
event, err := iterator.Next()
if err != nil {
break
}
events = append(events, event)
}
iterator.Close()
if len(events) != 6 {
err = fmt.Errorf("wrong number of events fetched, expecting 6 got %d", len(events))
return
Expand Down

0 comments on commit e944cdf

Please sign in to comment.