last update
This commit is contained in:
parent
758f3ccd43
commit
65e8a972e4
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue