ussd/internal/storage/timed_test.go

126 lines
2.9 KiB
Go
Raw Permalink 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("fo"))
2024-10-03 00:57:55 +02:00
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")
}
}
func TestFilteredSameKeypartStaleDb(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 = tdb.WithMatch(db.DATATYPE_USERDATA, []byte("ba"))
tdb.SetPrefix(db.DATATYPE_USERDATA)
tdb.SetSession("xyzzy")
err = tdb.Put(ctx, []byte("bar"), []byte("inky"))
if err != nil {
t.Fatal(err)
}
tdb.SetPrefix(db.DATATYPE_USERDATA)
tdb.SetSession("xyzzy")
err = tdb.Put(ctx, []byte("baz"), []byte("pinky"))
if err != nil {
t.Fatal(err)
}
tdb.SetPrefix(db.DATATYPE_USERDATA)
tdb.SetSession("xyzzy")
err = tdb.Put(ctx, []byte("foo"), []byte("blinky"))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond)
if !tdb.Stale(ctx, db.DATATYPE_USERDATA, "xyzzy", []byte("bar")) {
t.Fatal("expected stale")
}
if !tdb.Stale(ctx, db.DATATYPE_USERDATA, "xyzzy", []byte("baz")) {
t.Fatal("expected stale")
}
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "xyzzy", []byte("foo")) {
t.Fatal("expected not stale")
}
}