vise/engine/persist_test.go

74 lines
1.3 KiB
Go
Raw Normal View History

2023-04-13 01:38:33 +02:00
package engine
import (
"context"
"io/ioutil"
"os"
"testing"
2023-04-14 10:59:37 +02:00
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/persist"
"git.defalsify.org/vise/state"
2023-04-13 01:38:33 +02:00
)
func TestPersist(t *testing.T) {
generateTestData(t)
cfg := Config{
OutputSize: 83,
2023-04-13 01:38:33 +02:00
SessionId: "xyzzy",
Root: "root",
}
rs := NewFsWrapper(dataDir, nil)
2023-04-14 10:59:37 +02:00
persistDir, err := ioutil.TempDir("", "vise_engine_persist")
2023-04-13 01:38:33 +02:00
if err != nil {
t.Fatal(err)
}
st := state.NewState(3)
ca := cache.NewCache().WithCacheSize(1024)
pr := persist.NewFsPersister(persistDir).WithContent(&st, ca)
w := os.Stdout
2023-04-13 01:38:33 +02:00
ctx := context.TODO()
st = state.NewState(cfg.FlagCount)
ca = cache.NewCache()
ca = ca.WithCacheSize(cfg.CacheSize)
pr = persist.NewFsPersister(persistDir).WithContent(&st, ca)
err = pr.Save(cfg.SessionId)
2023-04-13 01:38:33 +02:00
if err != nil {
t.Fatal(err)
2023-04-13 01:38:33 +02:00
}
2023-04-13 01:38:33 +02:00
pr = persist.NewFsPersister(persistDir)
inputs := []string{
"", // trigger init, will not exec
2023-04-13 01:38:33 +02:00
"1",
"2",
"00",
}
for _, v := range inputs {
err = RunPersisted(cfg, rs, pr, []byte(v), w, ctx)
if err != nil {
t.Fatal(err)
}
}
pr = persist.NewFsPersister(persistDir)
err = pr.Load(cfg.SessionId)
if err != nil {
t.Fatal(err)
}
stAfter := pr.GetState()
location, idx := stAfter.Where()
if location != "long" {
t.Fatalf("expected 'long', got %s", location)
}
if idx != 1 {
t.Fatalf("expected '1', got %v", idx)
}
2023-04-13 01:38:33 +02:00
}