Replace timer-based SMA with a timer-less implementation

This commit is contained in:
Ingo Oppermann
2024-10-23 11:08:13 +02:00
parent 2dda47b81f
commit df30a6b8e3
17 changed files with 543 additions and 371 deletions

View File

@@ -4,20 +4,14 @@ import (
"testing"
"time"
timesrc "github.com/datarhei/core/v16/time"
"github.com/stretchr/testify/require"
)
type testTimeSource struct {
now time.Time
}
func (t *testTimeSource) Now() time.Time {
return t.now
}
func TestCache(t *testing.T) {
ts := &testTimeSource{
now: time.Unix(0, 0),
ts := &timesrc.TestSource{
N: time.Unix(0, 0),
}
c := NewCache[string](ts)
@@ -31,21 +25,21 @@ func TestCache(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "bar", v)
ts.now = time.Unix(10, 0)
ts.Set(10, 0)
v, err = c.Get("foo")
require.NoError(t, err)
require.Equal(t, "bar", v)
ts.now = time.Unix(11, 0)
ts.Set(11, 0)
_, err = c.Get("foo")
require.Error(t, err)
}
func TestCachePurge(t *testing.T) {
ts := &testTimeSource{
now: time.Unix(0, 0),
ts := &timesrc.TestSource{
N: time.Unix(0, 0),
}
c := NewCache[string](ts)
@@ -56,14 +50,14 @@ func TestCachePurge(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "bar", v)
ts.now = time.Unix(59, 0)
ts.Set(59, 0)
c.Put("foz", "boz", 10*time.Second)
_, ok := c.entries["foo"]
require.True(t, ok)
ts.now = time.Unix(61, 0)
ts.Set(61, 0)
c.Put("foz", "boz", 10*time.Second)