ussd/internal/storage/timed_test.go

86 lines
1.8 KiB
Go
Raw Normal View History

2024-10-02 23:06:43 +02:00
package storage
import (
"context"
"testing"
"time"
"git.defalsify.org/vise.git/db"
memdb "git.defalsify.org/vise.git/db/mem"
)
func TestStaleDb(t *testing.T) {
ctx := context.Background()
mdb := memdb.NewMemDb()
err := mdb.Connect(ctx, "")
if err != nil {
t.Fatal(err)
}
tdb := NewTimedDb(mdb, time.Duration(time.Millisecond))
tdb.SetPrefix(db.DATATYPE_USERDATA)
k := []byte("foo")
err = tdb.Put(ctx, k, []byte("bar"))
if err != nil {
t.Fatal(err)
}
2024-10-03 00:57:55 +02:00
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "", k) {
2024-10-02 23:06:43 +02:00
t.Fatal("expected not stale")
}
time.Sleep(time.Millisecond)
2024-10-03 00:57:55 +02:00
if !tdb.Stale(ctx, db.DATATYPE_USERDATA, "", k) {
2024-10-02 23:06:43 +02:00
t.Fatal("expected stale")
}
}
2024-10-03 00:57:55 +02:00
func TestFilteredStaleDb(t *testing.T) {
ctx := context.Background()
mdb := memdb.NewMemDb()
err := mdb.Connect(ctx, "")
if err != nil {
t.Fatal(err)
}
k := []byte("foo")
tdb := NewTimedDb(mdb, time.Duration(time.Millisecond))
tdb = tdb.WithMatch(db.DATATYPE_STATE, []byte("in"))
tdb.SetPrefix(db.DATATYPE_USERDATA)
tdb.SetSession("inky")
err = tdb.Put(ctx, k, []byte("bar"))
if err != nil {
t.Fatal(err)
}
tdb.SetPrefix(db.DATATYPE_STATE)
tdb.SetSession("inky")
err = tdb.Put(ctx, k, []byte("pinky"))
if err != nil {
t.Fatal(err)
}
tdb.SetSession("blinky")
err = tdb.Put(ctx, k, []byte("clyde"))
if err != nil {
t.Fatal(err)
}
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "inky", k) {
t.Fatal("expected not stale")
}
if tdb.Stale(ctx, db.DATATYPE_STATE, "inky", k) {
t.Fatal("expected not stale")
}
if tdb.Stale(ctx, db.DATATYPE_STATE, "blinky", k) {
t.Fatal("expected not stale")
}
time.Sleep(time.Millisecond)
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "inky", k) {
t.Fatal("expected not stale")
}
if !tdb.Stale(ctx, db.DATATYPE_STATE, "inky", k) {
t.Fatal("expected stale")
}
if tdb.Stale(ctx, db.DATATYPE_STATE, "blinky", k) {
t.Fatal("expected not stale")
}
}