jldb/mdb/crashconsistency_test.go

78 lines
1.2 KiB
Go
Raw Normal View History

2023-10-13 09:43:27 +00:00
package mdb
import (
"log"
"os"
"os/exec"
"testing"
"time"
2023-10-16 08:50:19 +00:00
"git.crumpington.com/public/jldb/lib/errs"
2023-10-13 09:43:27 +00:00
)
func TestCrashConsistency(t *testing.T) {
if testing.Short() {
t.Skip("Sipping test in short mode.")
}
// Build the test binary.
err := exec.Command(
"go", "build",
"-o", "testing/crashconsistency/p",
"testing/crashconsistency/main.go").Run()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll("testing/crashconsistency/p")
rootDir := t.TempDir()
defer os.RemoveAll(rootDir)
for i := 0; i < 32; i++ {
cmd := exec.Command("testing/crashconsistency/p", rootDir)
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second / 2)
for {
if err := cmd.Process.Kill(); err != nil {
log.Printf("Kill failed: %v", err)
time.Sleep(time.Second)
continue
}
break
}
var (
db DataDB
err error
)
for {
db, err = OpenDataDB(rootDir)
if err == nil {
break
}
if errs.Locked.Is(err) {
log.Printf("Locked.")
time.Sleep(time.Second / 10)
continue
}
t.Fatal(err)
}
tx := db.Snapshot()
computed := db.ComputeCRC(tx)
stored := db.ReadCRC(tx)
if computed != stored {
t.Fatal(stored, computed)
}
db.Close()
}
}