mirror of
https://github.com/datarhei/core.git
synced 2025-10-05 07:57:13 +08:00
Replace timer-based SMA with a timer-less implementation
This commit is contained in:
@@ -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 := ×rc.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 := ×rc.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)
|
||||
|
||||
|
Reference in New Issue
Block a user