Initial commit
This commit is contained in:
76
mdb/crashconsistency_test.go
Normal file
76
mdb/crashconsistency_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package mdb
|
||||
|
||||
import (
|
||||
"git.crumpington.com/public/jldb/lib/errs"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user