Compare commits

..

2 Commits

Author SHA1 Message Date
Erik Winter 65e8a972e4 last update 2024-10-16 07:34:08 +02:00
Erik Winter 758f3ccd43 findornext 2024-10-16 07:20:08 +02:00
5 changed files with 40 additions and 4 deletions

View File

@ -38,9 +38,6 @@ func (ml *LocalID) Find(id string) (int, error) {
}
func (ml *LocalID) FindOrNext(id string) (int, error) {
ml.mutex.Lock()
defer ml.mutex.Unlock()
lid, err := ml.Find(id)
switch {
case errors.Is(err, storage.ErrNotFound):

View File

@ -45,6 +45,14 @@ func TestLocalID(t *testing.T) {
if actLid != 1 {
t.Errorf("exp 1, git %v", actLid)
}
t.Log("retrieve unknown")
actLid, actErr = repo.FindOrNext("new")
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if actLid != 2 {
t.Errorf("exp 2, got %v", actLid)
}
actIDs, actErr = repo.FindAll()
if actErr != nil {

View File

@ -3,6 +3,7 @@ package memory
import (
"sort"
"sync"
"time"
"go-mod.ewintr.nl/planner/item"
)
@ -50,3 +51,17 @@ func (r *Sync) DeleteAll() error {
return nil
}
func (r *Sync) LastUpdate() (time.Time, error) {
r.mutex.RLock()
defer r.mutex.RUnlock()
var last time.Time
for _, i := range r.items {
if i.Updated.After(last) {
last = i.Updated
}
}
return last, nil
}

View File

@ -3,6 +3,7 @@ package memory_test
import (
"fmt"
"testing"
"time"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage/memory"
@ -14,11 +15,15 @@ func TestSync(t *testing.T) {
mem := memory.NewSync()
t.Log("store")
now := time.Now()
ts := now
count := 3
for i := 0; i < count; i++ {
mem.Store(item.Item{
ID: fmt.Sprintf("id-%d", i),
Updated: ts,
})
ts = ts.Add(-1 * time.Minute)
}
t.Log("find all")
@ -30,6 +35,15 @@ func TestSync(t *testing.T) {
t.Errorf("exp %v, got %v", count, len(actItems))
}
t.Log("last update")
actLU, actErr := mem.LastUpdate()
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if !actLU.Equal(now) {
t.Errorf("exp %v, got %v", now, actLU)
}
t.Log("delete all")
if err := mem.DeleteAll(); err != nil {
t.Errorf("exp nil, got %v", err)

View File

@ -3,6 +3,7 @@ package storage
import (
"errors"
"sort"
"time"
"go-mod.ewintr.nl/planner/item"
)
@ -23,6 +24,7 @@ type Sync interface {
FindAll() ([]item.Item, error)
Store(i item.Item) error
DeleteAll() error
LastUpdate() (time.Time, error)
}
type Event interface {