Files
seaweedfs/seaweed-block/core/chunkkernel/step_test.go
T

86 lines
2.0 KiB
Go

package chunkkernel
import (
"bufio"
"errors"
"os"
"strings"
"testing"
)
func TestExportedModelEdges(t *testing.T) {
f, err := os.Open("testdata/chunk-kernel-1.tsv")
if err != nil {
t.Fatal(err)
}
defer f.Close()
scan := bufio.NewScanner(f)
seen := map[string]bool{}
for scan.Scan() {
line := scan.Text()
if strings.HasPrefix(line, "#") {
continue
}
cols := strings.Split(line, "\t")
if len(cols) != 5 {
t.Fatalf("bad row %q", line)
}
key := strings.Join(cols[:3], "/")
if seen[key] {
t.Fatalf("duplicate %s", key)
}
seen[key] = true
t.Run(key, func(t *testing.T) {
result, effect, err := Step(State{Kind: Kind(cols[0])}, Event(cols[1]), Arg{Value: cols[2]})
if cols[3] == "refused" {
var defect *CallerDefect
if !errors.As(err, &defect) {
t.Fatalf("refusal error=%v", err)
}
if effect != None {
t.Fatal(effect)
}
return
}
if err != nil {
t.Fatal(err)
}
if string(result.Kind) != cols[3] || string(effect) != cols[4] {
t.Fatalf("got %s/%s want %s/%s", result.Kind, effect, cols[3], cols[4])
}
})
}
if err := scan.Err(); err != nil {
t.Fatal(err)
}
if len(seen) != 56 {
t.Fatalf("edges=%d want56", len(seen))
}
}
func TestAbsentWriteStartsBitmapAfresh(t *testing.T) {
stale := []uint64{1, 2}
written := []uint64{7}
got, _, err := Step(State{Kind: Absent, Blocks: stale}, Write, Arg{Value: "some", WrittenBlocks: written})
if err != nil {
t.Fatal(err)
}
if got.Kind != Partial || len(got.Blocks) != 1 || got.Blocks[0] != 7 {
t.Fatalf("stale bitmap survived: %+v", got)
}
got.Blocks[0] = 9
if stale[0] != 1 || written[0] != 7 {
t.Fatal("Step mutated its inputs")
}
}
func TestPartialWriteKeepsExistingBlocks(t *testing.T) {
got, _, err := Step(State{Kind: Partial, Blocks: []uint64{2, 4}}, Write, Arg{Value: "some", WrittenBlocks: []uint64{4, 6}})
if err != nil {
t.Fatal(err)
}
if len(got.Blocks) != 3 || got.Blocks[0] != 2 || got.Blocks[1] != 4 || got.Blocks[2] != 6 {
t.Fatalf("bitmap=%v", got.Blocks)
}
}