go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

persist_test.go (2601B)


      1 package engine
      2 
      3 import (
      4 	"bytes"
      5 	"context"
      6 	"testing"
      7 
      8 	"git.defalsify.org/vise.git/cache"
      9 	"git.defalsify.org/vise.git/persist"
     10 	"git.defalsify.org/vise.git/state"
     11 	memdb "git.defalsify.org/vise.git/db/mem"
     12 )
     13 
     14 
     15 func TestPersistNewAcrossEngine(t *testing.T) {
     16 	var err error
     17 	var cfg Config
     18 	generateTestData(t)
     19 	st := state.NewState(1)
     20 	ca := cache.NewCache()
     21 	rs := newTestWrapper(dataDir, st)
     22 	ctx := context.Background()
     23 	store := memdb.NewMemDb()
     24 	store.Connect(ctx, "")
     25 	pe := persist.NewPersister(store)
     26 	en := NewEngine(cfg, rs)
     27 	en = en.WithState(st)
     28 	en = en.WithMemory(ca)
     29 	en = en.WithPersister(pe)
     30 	cont, err := en.Exec(ctx, []byte{})
     31 	if err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	if !cont {
     35 		t.Errorf("expected cont")
     36 	}
     37 
     38 	r := bytes.NewBuffer(nil)
     39 	_, err = en.Flush(ctx, r)
     40 	if err != nil {
     41 		t.Fatal(err)
     42 	}
     43 
     44 	cont, err = en.Exec(ctx, []byte("1"))
     45 	if err != nil {
     46 		t.Fatal(err)
     47 	}
     48 	if !cont {
     49 		t.Errorf("expected cont")
     50 	}
     51 	r = bytes.NewBuffer(nil)
     52 	_, err = en.Flush(ctx, r)
     53 	if err != nil {
     54 		t.Fatal(err)
     55 	}
     56 
     57 	err = en.Finish()
     58 	if err != nil {
     59 		t.Fatal(err)
     60 	}
     61 
     62 	cfg.FlagCount = 1
     63 	pe = persist.NewPersister(store)
     64 	en = NewEngine(cfg, rs)
     65 	en = en.WithPersister(pe)
     66 	cont, err = en.Exec(ctx, []byte{})
     67 	if err != nil {
     68 		t.Fatal(err)
     69 	}
     70 	if !cont {
     71 		t.Errorf("expected cont")
     72 	}
     73 	location, _ := st.Where()
     74 	if location != "foo" {
     75 		t.Errorf("expected location 'foo', got '%s", location)	
     76 	}
     77 }
     78 
     79 func TestPersistSameAcrossEngine(t *testing.T) {
     80 	var err error
     81 	var cfg Config
     82 	generateTestData(t)
     83 	st := state.NewState(1)
     84 	ca := cache.NewCache()
     85 	rs := newTestWrapper(dataDir, st)
     86 	ctx := context.Background()
     87 	store := memdb.NewMemDb()
     88 	store.Connect(ctx, "")
     89 	pe := persist.NewPersister(store)
     90 	pe = pe.WithFlush()
     91 	en := NewEngine(cfg, rs)
     92 	en = en.WithState(st)
     93 	en = en.WithMemory(ca)
     94 	en = en.WithPersister(pe)
     95 	cont, err := en.Exec(ctx, []byte{})
     96 	if err != nil {
     97 		t.Fatal(err)
     98 	}
     99 	if !cont {
    100 		t.Errorf("expected cont")
    101 	}
    102 
    103 	r := bytes.NewBuffer(nil)
    104 	_, err = en.Flush(ctx, r)
    105 	if err != nil {
    106 		t.Fatal(err)
    107 	}
    108 
    109 	cont, err = en.Exec(ctx, []byte("1"))
    110 	if err != nil {
    111 		t.Fatal(err)
    112 	}
    113 	if !cont {
    114 		t.Errorf("expected cont")
    115 	}
    116 	r = bytes.NewBuffer(nil)
    117 	_, err = en.Flush(ctx, r)
    118 	if err != nil {
    119 		t.Fatal(err)
    120 	}
    121 
    122 	err = en.Finish()
    123 	if err != nil {
    124 		t.Fatal(err)
    125 	}
    126 
    127 	cfg.FlagCount = 1
    128 	en = NewEngine(cfg, rs)
    129 	en = en.WithPersister(pe)
    130 	cont, err = en.Exec(ctx, []byte{})
    131 	if err != nil {
    132 		t.Fatal(err)
    133 	}
    134 	if !cont {
    135 		t.Errorf("expected cont")
    136 	}
    137 	location, _ := st.Where()
    138 	if location != "foo" {
    139 		t.Errorf("expected location 'foo', got '%s", location)	
    140 	}
    141 }