package mdb import ( "log" "os" "os/exec" "testing" "time" "git.crumpington.com/public/jldb/lib/errs" ) 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() } }