Add comprehensive unit tests for InMemoryEventStore
- Test SaveEvent persists events correctly (single, multiple, multi-actor) - Test GetEvents retrieves events in insertion order - Test GetEvents with fromVersion filtering - Test GetLatestVersion returns correct version - Test behavior with non-existent actor IDs (returns empty/zero) - Test concurrent access safety with race detector - Add mutex protection to InMemoryEventStore for thread safety Closes #3 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit was merged in pull request #32.
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"git.flowmade.one/flowmade-one/aether"
|
||||
)
|
||||
|
||||
// InMemoryEventStore provides a simple in-memory event store for testing
|
||||
type InMemoryEventStore struct {
|
||||
mu sync.RWMutex
|
||||
events map[string][]*aether.Event // actorID -> events
|
||||
}
|
||||
|
||||
@@ -18,6 +21,9 @@ func NewInMemoryEventStore() *InMemoryEventStore {
|
||||
|
||||
// SaveEvent saves an event to the in-memory store
|
||||
func (es *InMemoryEventStore) SaveEvent(event *aether.Event) error {
|
||||
es.mu.Lock()
|
||||
defer es.mu.Unlock()
|
||||
|
||||
if _, exists := es.events[event.ActorID]; !exists {
|
||||
es.events[event.ActorID] = make([]*aether.Event, 0)
|
||||
}
|
||||
@@ -27,6 +33,9 @@ func (es *InMemoryEventStore) SaveEvent(event *aether.Event) error {
|
||||
|
||||
// GetEvents retrieves events for an actor from a specific version
|
||||
func (es *InMemoryEventStore) GetEvents(actorID string, fromVersion int64) ([]*aether.Event, error) {
|
||||
es.mu.RLock()
|
||||
defer es.mu.RUnlock()
|
||||
|
||||
events, exists := es.events[actorID]
|
||||
if !exists {
|
||||
return []*aether.Event{}, nil
|
||||
@@ -44,6 +53,9 @@ func (es *InMemoryEventStore) GetEvents(actorID string, fromVersion int64) ([]*a
|
||||
|
||||
// GetLatestVersion returns the latest version for an actor
|
||||
func (es *InMemoryEventStore) GetLatestVersion(actorID string) (int64, error) {
|
||||
es.mu.RLock()
|
||||
defer es.mu.RUnlock()
|
||||
|
||||
events, exists := es.events[actorID]
|
||||
if !exists || len(events) == 0 {
|
||||
return 0, nil
|
||||
|
||||
Reference in New Issue
Block a user