mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
134 lines
4.0 KiB
Go
134 lines
4.0 KiB
Go
package chunkkernel
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestExportedModelEdges(t *testing.T) {
|
|
f, err := os.Open("chunk-kernel-2.tsv")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
scan := bufio.NewScanner(f)
|
|
seen := map[string]bool{}
|
|
version := ""
|
|
for scan.Scan() {
|
|
line := scan.Text()
|
|
if strings.HasPrefix(line, "#") {
|
|
if _, v, ok := strings.Cut(line, "version: "); ok {
|
|
version = v
|
|
}
|
|
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) {
|
|
input := State{Kind: Kind(cols[0])}
|
|
result, effect, err := Step(input, Event(cols[1]), Arg{Value: cols[2]})
|
|
if string(effect) != cols[4] {
|
|
t.Fatalf("effect=%s want%s", effect, cols[4])
|
|
}
|
|
if cols[3] == "refused" {
|
|
var defect *CallerDefect
|
|
if !errors.As(err, &defect) || result.State != input || result.Outcome != Refused || effect != None {
|
|
t.Fatalf("refused edge changed input: %+v/%s/%v", result, effect, err)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cols[3] == "ignored" {
|
|
if result.State != input || result.Outcome != Ignored {
|
|
t.Fatalf("ignored edge changed input: %+v", result)
|
|
}
|
|
} else if string(result.State.Kind) != cols[3] || result.Outcome != Applied {
|
|
t.Fatalf("got%+v want%s", result, cols[3])
|
|
}
|
|
})
|
|
}
|
|
if err := scan.Err(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if version == "" || version != Version {
|
|
t.Fatalf("version=%q want%q", version, Version)
|
|
}
|
|
// Every input in the exported model's domain must appear exactly once.
|
|
domains := map[Event][]string{Write: {"some", "all"}, Fill: {"-"}, Hash: {"-"}, Commit: {"-"}, Evict: {"-"}, Move: {"equal", "differs"}, Changed: {"some", "all", "none"}, Rebuilt: {"some", "all", "none"}}
|
|
total := 0
|
|
for _, s := range []Kind{Absent, Partial, Unknown, HashUnpublished, HashPublished} {
|
|
for e, args := range domains {
|
|
for _, a := range args {
|
|
total++
|
|
if !seen[string(s)+"/"+string(e)+"/"+a] {
|
|
t.Errorf("missing %s/%s/%s", s, e, a)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(seen) != total {
|
|
t.Fatalf("edges=%d domain=%d", len(seen), total)
|
|
}
|
|
}
|
|
|
|
func TestOutsideDomainPreservesInput(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
s Kind
|
|
e Event
|
|
a string
|
|
}{
|
|
{"not-a-state", Write, "all"}, {Absent, "not-an-event", "-"},
|
|
{Partial, Fill, "equal"}, {HashPublished, Evict, "equal"},
|
|
{Unknown, Write, "none"}, {HashUnpublished, Commit, "some"},
|
|
{Absent, Write, ""}, {"", Fill, "-"},
|
|
} {
|
|
t.Run(string(tc.s)+"/"+string(tc.e)+"/"+tc.a, func(t *testing.T) {
|
|
input := State{Kind: tc.s}
|
|
got, effect, err := Step(input, tc.e, Arg{Value: tc.a})
|
|
var defect *CallerDefect
|
|
if !errors.As(err, &defect) {
|
|
t.Fatalf("error=%v want CallerDefect", err)
|
|
}
|
|
if got.State != input || got.Outcome != Refused || effect != None {
|
|
t.Fatalf("refusal changed input: %+v/%s", got, effect)
|
|
}
|
|
if defect.State != tc.s || defect.Event != tc.e || defect.Arg != tc.a {
|
|
t.Fatalf("wrong defect: %+v", defect)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHashedChunkNeedsPublicationBeforeEviction(t *testing.T) {
|
|
got, _, err := Step(State{Kind: Unknown}, Hash, Arg{Value: "-"})
|
|
if err != nil || got.State.Kind != HashUnpublished {
|
|
t.Fatalf("hash: %+v %v", got, err)
|
|
}
|
|
held, effect, err := Step(got.State, Evict, Arg{Value: "-"})
|
|
var defect *CallerDefect
|
|
if !errors.As(err, &defect) || held.State != got.State || effect != None {
|
|
t.Fatalf("unpublished eviction: %+v/%s/%v", held, effect, err)
|
|
}
|
|
// The caller has independently established the committed HEAD names this hash.
|
|
published, _, err := Step(got.State, Commit, Arg{Value: "-"})
|
|
if err != nil || published.State.Kind != HashPublished {
|
|
t.Fatalf("commit: %+v %v", published, err)
|
|
}
|
|
evicted, effect, err := Step(published.State, Evict, Arg{Value: "-"})
|
|
if err != nil || evicted.State.Kind != Absent || effect != PageBeforeBytes {
|
|
t.Fatalf("published eviction: %+v/%s/%v", evicted, effect, err)
|
|
}
|
|
}
|