From 2a87cffee63b559266935260c03550df91fdb1d3 Mon Sep 17 00:00:00 2001 From: Jan Mercl <0xjnml@gmail.com> Date: Thu, 22 May 2014 13:11:47 +0200 Subject: [PATCH] Make tests work in a read-only repository. --- falloc/all_test.go | 358 ++++++++++++++++++++++++++++-------------- falloc/dev_test.go | 348 ---------------------------------------- storage/all_test.go | 1 - storage/cache_test.go | 41 ++--- 4 files changed, 260 insertions(+), 488 deletions(-) delete mode 100644 falloc/dev_test.go diff --git a/falloc/all_test.go b/falloc/all_test.go index bbc049f..d70dcaf 100644 --- a/falloc/all_test.go +++ b/falloc/all_test.go @@ -11,16 +11,18 @@ import ( "errors" "flag" "fmt" - "github.com/cznic/fileutil" - "github.com/cznic/fileutil/storage" - "github.com/cznic/mathutil" "io/ioutil" "log" "math" "os" + "path/filepath" "runtime" "testing" "time" + + "github.com/cznic/fileutil" + "github.com/cznic/fileutil/storage" + "github.com/cznic/mathutil" ) var ( @@ -30,7 +32,6 @@ var ( devFlag = flag.Bool("dev", false, "enable dev tests") dropFlag = flag.Bool("drop", false, "drop system file cache for some of the dev tests before measurement") fadviseFlag = flag.Bool("fadvise", false, "hint kernel about random file access") - fnFlag = flag.String("f", "test.tmp", "test file name") nFlag = flag.Int("n", 1, "parameter for some of the dev tests") probeFlag = flag.Bool("probe", false, "report store probe statistics") optGo = flag.Int("go", 3, "GOMAXPROCS") @@ -41,6 +42,16 @@ func init() { runtime.GOMAXPROCS(*optGo) } +func temp() (dir, name string) { + dir, err := ioutil.TempDir("", "test-falloc-") + if err != nil { + panic(err) + } + + name = filepath.Join(dir, "test.db") + return dir, name +} + type balancedAcid struct { storage.Accessor nesting int @@ -397,13 +408,16 @@ func reaudit(t *testing.T, f *File, fn string) (of *File) { } func TestCreate(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } defer func() { - err := os.Remove(*fnFlag) + err := os.Remove(name) if err != nil { t.Fatal(err) } @@ -416,7 +430,7 @@ func TestCreate(t *testing.T) { t.Fatal(err) } - b, err := ioutil.ReadFile(*fnFlag) + b, err := ioutil.ReadFile(name) if err != nil { t.Fatal(err) } @@ -433,7 +447,10 @@ func TestCreate(t *testing.T) { } func TestOpen(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -441,7 +458,7 @@ func TestOpen(t *testing.T) { defer func() { probed(t, f) ec := f.Close() - er := os.Remove(*fnFlag) + er := os.Remove(name) if ec != nil { t.Fatal(ec) } @@ -455,7 +472,7 @@ func TestOpen(t *testing.T) { t.Fatal(err) } - if f, err = fopen(*fnFlag); err != nil { + if f, err = fopen(name); err != nil { t.Fatal(err) } @@ -485,14 +502,17 @@ func realloc(f *File, atom int64, b []byte, keepHandle bool) (y int64) { } func testContentEncodingDecoding(t *testing.T, min, max int) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } defer func() { ec := f.Close() - er := os.Remove(*fnFlag) + er := os.Remove(name) if ec != nil { t.Fatal(ec) } @@ -549,7 +569,7 @@ func testContentEncodingDecoding(t *testing.T, min, max int) { f = nil runtime.GC() - if f, err = fopen(*fnFlag); err != nil { + if f, err = fopen(name); err != nil { t.Fatal(err) } @@ -613,7 +633,7 @@ func testContentEncodingDecoding(t *testing.T, min, max int) { t.Fatal(auditblocks, blocks) } - if f = reaudit(t, f, *fnFlag); err != nil { + if f = reaudit(t, f, name); err != nil { t.Fatal(err) } } @@ -644,14 +664,17 @@ func free(f *File, h int64) { } func testFreeTail(t *testing.T, b []byte) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } defer func() { ec := f.Close() - er := os.Remove(*fnFlag) + er := os.Remove(name) if ec != nil { t.Fatal(ec) } @@ -687,7 +710,7 @@ func testFreeTail(t *testing.T, b []byte) { f = nil runtime.GC() - if f, err = fopen(*fnFlag); err != nil { + if f, err = fopen(name); err != nil { t.Fatal(err) } @@ -729,14 +752,17 @@ func TestFreeTail(t *testing.T) { } func testFreeTail2(t *testing.T, b []byte) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } defer func() { ec := f.Close() - er := os.Remove(*fnFlag) + er := os.Remove(name) if ec != nil { t.Fatal(ec) } @@ -774,7 +800,7 @@ func testFreeTail2(t *testing.T, b []byte) { f = nil runtime.GC() - if f, err = fopen(*fnFlag); err != nil { + if f, err = fopen(name); err != nil { t.Fatal(err) } @@ -816,14 +842,17 @@ func TestFreeTail2(t *testing.T) { } func testFreeIsolated(t *testing.T, b []byte) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } defer func() { ec := f.Close() - er := os.Remove(*fnFlag) + er := os.Remove(name) if ec != nil { t.Fatal(ec) } @@ -894,7 +923,7 @@ func testFreeIsolated(t *testing.T, b []byte) { f = nil runtime.GC() - if f, err = fopen(*fnFlag); err != nil { + if f, err = fopen(name); err != nil { t.Fatal(err) } @@ -955,7 +984,10 @@ func testFreeBlockList(t *testing.T, a, b int) { var h [2]int64 t.Log(a, b) - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -969,7 +1001,7 @@ func testFreeBlockList(t *testing.T, a, b int) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() used0, total0, err := f.audit() @@ -989,7 +1021,7 @@ func testFreeBlockList(t *testing.T, a, b int) { f = nil runtime.GC() - if f, err = fopen(*fnFlag); err != nil { + if f, err = fopen(name); err != nil { t.Fatal(err) } @@ -1020,7 +1052,7 @@ func testFreeBlockList(t *testing.T, a, b int) { f = nil runtime.GC() - if f, err = fopen(*fnFlag); err != nil { + if f, err = fopen(name); err != nil { t.Fatal(err) } @@ -1042,7 +1074,10 @@ func TestFreeBlockList(t *testing.T) { func testFreeBlockList2(t *testing.T, a, b, c int) { var h [3]int64 - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1056,7 +1091,7 @@ func testFreeBlockList2(t *testing.T, a, b, c int) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() used0, total0, err := f.audit() @@ -1078,7 +1113,7 @@ func testFreeBlockList2(t *testing.T, a, b, c int) { f = nil runtime.GC() - if f, err = fopen(*fnFlag); err != nil { + if f, err = fopen(name); err != nil { t.Fatal(err) } @@ -1110,7 +1145,7 @@ func testFreeBlockList2(t *testing.T, a, b, c int) { f = nil runtime.GC() - if f, err = fopen(*fnFlag); err != nil { + if f, err = fopen(name); err != nil { t.Fatal(err) } @@ -1158,7 +1193,10 @@ func testFreeBlockList3(t *testing.T, n, mod int) { t.Fatal(err) } - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1172,7 +1210,7 @@ func testFreeBlockList3(t *testing.T, n, mod int) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() ha := make([]int64, n) @@ -1185,7 +1223,7 @@ func testFreeBlockList3(t *testing.T, n, mod int) { t.Fatal(h) } } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) del := map[int64]bool{} for _ = range ha { i := rng.Next() @@ -1195,7 +1233,7 @@ func testFreeBlockList3(t *testing.T, n, mod int) { del[h] = true } } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) for _, h := range ha { if !del[h] { exp := content(b, h) @@ -1215,7 +1253,10 @@ func TestFreeBlockList3(t *testing.T) { } func TestRealloc1(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1229,7 +1270,7 @@ func TestRealloc1(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1256,7 +1297,7 @@ func TestRealloc1(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1277,7 +1318,10 @@ func TestRealloc1(t *testing.T) { } func TestRealloc1Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1291,7 +1335,7 @@ func TestRealloc1Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1318,7 +1362,7 @@ func TestRealloc1Keep(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1339,7 +1383,10 @@ func TestRealloc1Keep(t *testing.T) { } func TestRealloc2(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1353,7 +1400,7 @@ func TestRealloc2(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1380,7 +1427,7 @@ func TestRealloc2(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1401,7 +1448,10 @@ func TestRealloc2(t *testing.T) { } func TestRealloc2Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1415,7 +1465,7 @@ func TestRealloc2Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1442,7 +1492,7 @@ func TestRealloc2Keep(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1463,7 +1513,10 @@ func TestRealloc2Keep(t *testing.T) { } func TestRealloc3(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1477,7 +1530,7 @@ func TestRealloc3(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1505,7 +1558,7 @@ func TestRealloc3(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1526,7 +1579,10 @@ func TestRealloc3(t *testing.T) { } func TestRealloc3Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1540,7 +1596,7 @@ func TestRealloc3Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1568,7 +1624,7 @@ func TestRealloc3Keep(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1589,7 +1645,10 @@ func TestRealloc3Keep(t *testing.T) { } func TestRealloc4Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1603,7 +1662,7 @@ func TestRealloc4Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1631,7 +1690,7 @@ func TestRealloc4Keep(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1652,7 +1711,10 @@ func TestRealloc4Keep(t *testing.T) { } func TestRealloc5(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1666,7 +1728,7 @@ func TestRealloc5(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1696,7 +1758,7 @@ func TestRealloc5(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1717,7 +1779,10 @@ func TestRealloc5(t *testing.T) { } func TestRealloc5Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1731,7 +1796,7 @@ func TestRealloc5Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1761,7 +1826,7 @@ func TestRealloc5Keep(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1782,7 +1847,10 @@ func TestRealloc5Keep(t *testing.T) { } func TestRealloc6(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1796,7 +1864,7 @@ func TestRealloc6(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1826,7 +1894,7 @@ func TestRealloc6(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1847,7 +1915,10 @@ func TestRealloc6(t *testing.T) { } func TestRealloc6Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1861,7 +1932,7 @@ func TestRealloc6Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1891,7 +1962,7 @@ func TestRealloc6Keep(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(handle); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1912,7 +1983,10 @@ func TestRealloc6Keep(t *testing.T) { } func TestRelocRealloc1(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1926,7 +2000,7 @@ func TestRelocRealloc1(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -1957,7 +2031,7 @@ func TestRelocRealloc1(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -1978,7 +2052,10 @@ func TestRelocRealloc1(t *testing.T) { } func TestRelocRealloc1Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -1992,7 +2069,7 @@ func TestRelocRealloc1Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2023,7 +2100,7 @@ func TestRelocRealloc1Keep(t *testing.T) { t.Fatal(len(got), 0) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2044,7 +2121,10 @@ func TestRelocRealloc1Keep(t *testing.T) { } func TestRelocRealloc2(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2058,7 +2138,7 @@ func TestRelocRealloc2(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2087,7 +2167,7 @@ func TestRelocRealloc2(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2104,7 +2184,10 @@ func TestRelocRealloc2(t *testing.T) { } func TestRelocRealloc2Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2118,7 +2201,7 @@ func TestRelocRealloc2Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2147,7 +2230,7 @@ func TestRelocRealloc2Keep(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2164,7 +2247,10 @@ func TestRelocRealloc2Keep(t *testing.T) { } func TestRelocRealloc3(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2178,7 +2264,7 @@ func TestRelocRealloc3(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2207,7 +2293,7 @@ func TestRelocRealloc3(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2224,7 +2310,10 @@ func TestRelocRealloc3(t *testing.T) { } func TestRelocRealloc3Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2238,7 +2327,7 @@ func TestRelocRealloc3Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2267,7 +2356,7 @@ func TestRelocRealloc3Keep(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2284,7 +2373,10 @@ func TestRelocRealloc3Keep(t *testing.T) { } func TestRelocRealloc4(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2298,7 +2390,7 @@ func TestRelocRealloc4(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2331,7 +2423,7 @@ func TestRelocRealloc4(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2348,7 +2440,10 @@ func TestRelocRealloc4(t *testing.T) { } func TestRelocRealloc4Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2362,7 +2457,7 @@ func TestRelocRealloc4Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2395,7 +2490,7 @@ func TestRelocRealloc4Keep(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2412,7 +2507,10 @@ func TestRelocRealloc4Keep(t *testing.T) { } func TestRelocRealloc5(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2426,7 +2524,7 @@ func TestRelocRealloc5(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2455,7 +2553,7 @@ func TestRelocRealloc5(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2472,7 +2570,10 @@ func TestRelocRealloc5(t *testing.T) { } func TestRelocRealloc5Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2486,7 +2587,7 @@ func TestRelocRealloc5Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2515,7 +2616,7 @@ func TestRelocRealloc5Keep(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2532,7 +2633,10 @@ func TestRelocRealloc5Keep(t *testing.T) { } func TestRelocRealloc6(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2546,7 +2650,7 @@ func TestRelocRealloc6(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2571,7 +2675,7 @@ func TestRelocRealloc6(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2588,7 +2692,10 @@ func TestRelocRealloc6(t *testing.T) { } func TestRelocRealloc6Keep(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2602,7 +2709,7 @@ func TestRelocRealloc6Keep(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2627,7 +2734,7 @@ func TestRelocRealloc6Keep(t *testing.T) { t.Fatal(len(got), len(exp)) } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, exp) { t.Fatal(len(got), len(exp)) @@ -2644,7 +2751,10 @@ func TestRelocRealloc6Keep(t *testing.T) { } func TestFreespaceReuse(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2658,7 +2768,7 @@ func TestFreespaceReuse(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2694,7 +2804,7 @@ func TestFreespaceReuse(t *testing.T) { t.Fatal() } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) { t.Fatal() @@ -2719,7 +2829,10 @@ func TestFreespaceReuse(t *testing.T) { } func TestFreespaceReuse2(t *testing.T) { - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2733,7 +2846,7 @@ func TestFreespaceReuse2(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2769,7 +2882,7 @@ func TestFreespaceReuse2(t *testing.T) { t.Fatal() } - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) if got, _ := f.readUsed(h10); !bytes.Equal(got, c10) { t.Fatal() @@ -2796,7 +2909,11 @@ func TestFreespaceReuse2(t *testing.T) { func testBug1(t *testing.T, swap bool) { // Free lists table item for size 3856 points to list of free blocks // NOT of size 3856 but at least 3856. - f, err := fcreate(*fnFlag) + + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2810,7 +2927,7 @@ func testBug1(t *testing.T, swap bool) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() _ = alloc(f, nil) @@ -2831,7 +2948,7 @@ func testBug1(t *testing.T, swap bool) { free(f, f2) _ = alloc(f, nil) - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) used, total, err := f.audit() // c+3, c+4 if err != nil { @@ -2864,7 +2981,10 @@ func TestMix(t *testing.T) { } t.Log(n) - f, err := fcreate(*fnFlag) + dir, name := temp() + defer os.RemoveAll(dir) + + f, err := fcreate(name) if err != nil { t.Fatal(err) } @@ -2878,7 +2998,7 @@ func TestMix(t *testing.T) { f = nil runtime.GC() - os.Remove(*fnFlag) + os.Remove(name) }() b := make([]byte, 61680) @@ -2903,7 +3023,7 @@ func TestMix(t *testing.T) { t.Logf("write time A %.3g", dt) // verify - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) t.Logf("size A %d for %d bytes (fill factor %3.1f%%)", f.atoms<<4, payload, 100*float64(payload)/float64(f.atoms<<4)) t0 = time.Now() for _ = range ha { @@ -2926,7 +3046,7 @@ func TestMix(t *testing.T) { t.Logf("free time A %.3g", dt) // verify - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) t.Logf("size B %d (freeing half of the blocks)", f.atoms<<4) t0 = time.Now() for _ = range ha { @@ -2955,7 +3075,7 @@ func TestMix(t *testing.T) { } c := content(b, int64(r)) - //f = reaudit(t, f, *fnFlag) + //f = reaudit(t, f, name) if h2 := realloc(f, h, c, true); h2 != h { t.Fatal() } @@ -2964,7 +3084,7 @@ func TestMix(t *testing.T) { t.Logf("realoc time B %.3g", dt) // verify - f = reaudit(t, f, *fnFlag) + f = reaudit(t, f, name) t.Logf("size C %d for %d bytes (reallocated all used blocks to double size, fill factor %3.1f%%", f.atoms<<4, payload, 100*float64(payload)/float64(f.atoms<<4)) t0 = time.Now() diff --git a/falloc/dev_test.go b/falloc/dev_test.go deleted file mode 100644 index 5ff13ae..0000000 --- a/falloc/dev_test.go +++ /dev/null @@ -1,348 +0,0 @@ -// Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// blame: jnml, labs.nic.cz - -// Development utils/helpers - -package falloc - -import ( - "github.com/cznic/fileutil/storage" - "github.com/cznic/mathutil" - "os" - "runtime" - "testing" - "time" -) - -func nums(first, last, overhead int, t *testing.T) { - avail := last - first + 1 - std, escapes, rem := 0, 0, 0 - i := first - for avail > 0 { - if (i+overhead)&0xF == 0xF { - avail-- - escapes++ - } - if avail == 0 { - rem++ - break - } - - avail-- - std++ - i++ - } - if avail < 0 { - t.Fatal(avail) - } - - t.Logf( - "%04x...%04x (%5d range), overhead %d, std %04x...%04x (%5d values), esc %04x...%04x (%4d values, %d unused)", - first, last, last-first+1, overhead, - first, first+std-1, std, - first+std, last, escapes, rem, - ) -} - -func qmap(x []int) (y []int) { - m := map[int]bool{} - for _, v := range x { - m[v] = true - } - for k := range m { - y = append(y, k) - } - return -} - -func qmapmem(nFlag int) uint64 { - m := map[int64]bool{} - var ms runtime.MemStats - runtime.GC() - runtime.ReadMemStats(&ms) - mem := ms.HeapAlloc - for i := 0; i < nFlag; i++ { - m[int64(i)] = true - } - runtime.GC() - runtime.ReadMemStats(&ms) - return ms.HeapAlloc - mem -} - -func TestDev0(t *testing.T) { - if !*devFlag { - t.Log("Not enabled") - return - } - - nums(1, 0xFB, 1, t) - nums(1, 0xFFFF, 3, t) - - x := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - y := qmap(x) - t.Logf("%v %v", x, y) - x = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - y = qmap(x) - t.Logf("%v %v", x, y) - x = []int{5, 6, 7, 8, 9, 0, 1, 2, 3, 4} - y = qmap(x) - t.Logf("%v %v", x, y) - - nFlag := int(1e3) - mem := qmapmem(nFlag) - t.Logf("map[int64]bool, nFlag %d, mem %d, %.3g bytes/nFlag", nFlag, mem, float64(mem)/float64(nFlag)) - nFlag = int(1e4) - mem = qmapmem(nFlag) - t.Logf("map[int64]bool, nFlag %d, mem %d, %.3g bytes/nFlag", nFlag, mem, float64(mem)/float64(nFlag)) - nFlag = int(1e5) - mem = qmapmem(nFlag) - t.Logf("map[int64]bool, nFlag %d, mem %d, %.3g bytes/nFlag", nFlag, mem, float64(mem)/float64(nFlag)) - nFlag = int(1e6) - mem = qmapmem(nFlag) - t.Logf("map[int64]bool, nFlag %d, mem %d, %.3g bytes/nFlag", nFlag, mem, float64(mem)/float64(nFlag)) -} - -func testBenchAlloc(t *testing.T, n, block int) (dt int64) { - f, err := fcreate(*fnFlag) - if err != nil { - panic(err) - } - - defer func() { - f.Accessor().Sync() - probed(t, f) - ec := f.Close() - er := os.Remove(*fnFlag) - if ec != nil { - t.Fatal(ec) - } - - if er != nil { - t.Fatal(er) - } - }() - - b := make([]byte, block) - perc := 1 - if n >= 1000 { - perc = n / 1000 - } - dt = time.Now().UnixNano() - if probe, ok := f.Accessor().(*storage.Probe); ok { - probe.Reset() - } - for i := 0; i < n; i++ { - if i != 0 && i%perc == 0 { - ut := float64((time.Now().UnixNano() - dt) / 1e9) - q := float64(i) / float64(n) - rt := ut/q - ut - print("w ", (1000*uint64(i))/uint64(n), " eta ", int(rt/60), ":", int(rt)%60, " \r") - } - if _, err := f.Alloc(b); err != nil { - panic(err) - } - } - return time.Now().UnixNano() - dt -} - -func TestDevBenchAlloc(t *testing.T) { - if !*devFlag { - t.Log("Not enabled") - return - } - - defer func() { - if e := recover(); e != nil { - t.Fatal(e) - } - }() - - n, block := *nFlag, int(*blockFlag) - dt := testBenchAlloc(t, n, block) - ft := float64(dt) / 1e9 - nb := int64(n) * int64(block) - t.Logf( - "%10d blocks x %8d bytes == %8.3fMB, %8.3fs, %8.3fMB/s, %12.3f blocks/s", - n, block, float64(nb)/(1<<20), ft, float64(nb)/(1<<20)/ft, float64(n)/ft, - ) -} - -func testBenchRead(t *testing.T, n, block int) (dt int64) { - f, err := fcreate(*fnFlag) - if err != nil { - t.Fatal(err) - } - - perc := 1 - if n >= 1000 { - perc = n / 1000 - } - h := make([]Handle, n) - b := make([]byte, block) - dt = time.Now().UnixNano() - for i := 0; i < n; i++ { - if i != 0 && i%perc == 0 { - ut := float64((time.Now().UnixNano() - dt) / 1e9) - q := float64(i) / float64(n) - rt := ut/q - ut - print("w ", (1000*uint64(i))/uint64(n), " eta ", int(rt/60), ":", int(rt)%60, " \r") - } - if h[i], err = f.Alloc(b); err != nil { - t.Fatal(err) - } - } - - println() - if probe, ok := f.Accessor().(*storage.Probe); ok { - probe.Reset() - } - dt = time.Now().UnixNano() - for i := 0; i < n; i++ { - if i != 0 && i%perc == 0 { - ut := float64((time.Now().UnixNano() - dt) / 1e9) - q := float64(i) / float64(n) - rt := ut/q - ut - print("r ", (1000*uint64(i))/uint64(n), " eta ", int(rt/60), ":", int(rt)%60, " \r") - } - if _, err := f.Read(h[i]); err != nil { - t.Fatal(err) - } - } - dt = time.Now().UnixNano() - dt - - f.Accessor().Sync() - probed(t, f) - ec := f.Close() - er := os.Remove(*fnFlag) - if ec != nil { - t.Fatal(ec) - } - - if er != nil { - t.Fatal(er) - } - - return -} - -func TestDevBenchRead(t *testing.T) { - if !*devFlag { - t.Log("Not enabled") - return - } - - defer func() { - if e := recover(); e != nil { - t.Fatal(e) - } - }() - - n, block := *nFlag, int(*blockFlag) - dt := testBenchRead(t, n, block) - ft := float64(dt) / 1e9 - nb := int64(n) * int64(block) - t.Logf( - "%10d blocks x %8d bytes == %8.3fMB, %8.3fs, %8.3fMB/s, %12.3f blocks/s", - n, block, float64(nb)/(1<<20), ft, float64(nb)/(1<<20)/ft, float64(n)/ft, - ) -} - -func testBenchReadRnd(t *testing.T, n, block int) (dt int64) { - println(252, n, block) //TODO- - f, err := fcreate(*fnFlag) - println(254) //TODO- - if err != nil { - println(254) //TODO- - t.Fatal(err) - } - - println(259) //TODO- - defer func() { - f.Accessor().Sync() - probed(t, f) - ec := f.Close() - er := os.Remove(*fnFlag) - if ec != nil { - t.Fatal(ec) - } - - if er != nil { - t.Fatal(er) - } - }() - - h := make([]Handle, n) - b := make([]byte, block) - perc := 1 - if n >= 1000 { - perc = n / 1000 - } - println(281) //TODO- - t0 := time.Now().UnixNano() - for i := 0; i < n; i++ { - if h[i], err = f.Alloc(b); err != nil { - println(285) //TODO- - t.Fatal(err) - } - if i != 0 && i%perc == 0 { - dt := float64((time.Now().UnixNano() - t0) / 1e9) - q := float64(i) / float64(n) - rt := dt/q - dt - print("w ", (1000*uint64(i))/uint64(n), " eta ", int(rt/60), ":", int(rt)%60, " \r") - } - } - rng, err := mathutil.NewFC32(0, n-1, true) - if err != nil { - println(297) //TODO- - t.Fatal(err) - } - println("\nsync") - f.Accessor().Sync() - println("synced") - t0 = time.Now().UnixNano() - for i := 0; i < n; i++ { - if _, err := f.Read(h[rng.Next()]); err != nil { - println(306) //TODO- - t.Fatal(err) - } - if i != 0 && i%perc == 0 { - dt := float64((time.Now().UnixNano() - t0) / 1e9) - q := float64(i) / float64(n) - rt := dt/q - dt - print("r ", (1000*uint64(i))/uint64(n), " eta ", int(rt/60), ":", int(rt)%60, " \r") - } - } - println() - println(314) //TODO- - dt = time.Now().UnixNano() - t0 - if c, ok := f.Accessor().(*storage.Cache); ok { - x, _ := c.Stat() - t.Logf("Cache rq %10d, load %10d, purge %10d, top %10d, fs %10d", c.Rq, c.Load, c.Purge, c.Top, x.Size()) - } - println(323) //TODO- - return -} - -func TestDevBenchReadRnd(t *testing.T) { - if !*devFlag { - t.Log("Not enabled") - return - } - - defer func() { - if e := recover(); e != nil { - t.Fatal(e) - } - }() - - dt := testBenchReadRnd(t, *nFlag, int(*blockFlag)) - ft := float64(dt) / 1e9 - nb := int64(*nFlag) * int64(*blockFlag) - t.Logf( - "%10d blocks x %8d bytes == %8.3fMB, %8.3fs, %8.3fMB/s, %12.3f blocks/s", - *nFlag, *blockFlag, float64(nb)/(1<<20), ft, float64(nb)/(1<<20)/ft, float64(*nFlag)/ft, - ) -} diff --git a/storage/all_test.go b/storage/all_test.go index 519e171..8947a0a 100644 --- a/storage/all_test.go +++ b/storage/all_test.go @@ -13,7 +13,6 @@ import ( var ( devFlag = flag.Bool("dev", false, "enable dev tests") - fFlag = flag.String("f", "test.tmp", "test file name") goFlag = flag.Int("go", 1, "GOMAXPROCS") ) diff --git a/storage/cache_test.go b/storage/cache_test.go index af3766d..8dbad08 100644 --- a/storage/cache_test.go +++ b/storage/cache_test.go @@ -9,41 +9,38 @@ package storage import ( "io/ioutil" "os" + "path/filepath" "testing" ) -func newfile(t *testing.T) Accessor { - f, err := NewFile(*fFlag, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666) +func newfile(t *testing.T) (string, string, Accessor) { + dir, err := ioutil.TempDir("", "test-storage-") if err != nil { - t.Fatal("newfile", err) + panic(err) } - return f -} - -func openfile(t *testing.T) Accessor { - f, err := OpenFile(*fFlag, os.O_RDONLY, 0666) + name := filepath.Join(dir, "test.tmp") + f, err := NewFile(name, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666) if err != nil { - t.Fatal("openfile", err) + t.Fatal("newfile", err) } - return f + return dir, name, f } -func readfile(t *testing.T) (b []byte) { +func readfile(t *testing.T, name string) (b []byte) { var err error - if b, err = ioutil.ReadFile(*fFlag); err != nil { + if b, err = ioutil.ReadFile(name); err != nil { t.Fatal("readfile") } return } -func newcache(t *testing.T) (c *Cache) { - f := newfile(t) +func newcache(t *testing.T) (dir, name string, c *Cache) { + dir, name, f := newfile(t) var err error - c, err = NewCache(f, 1<<20, nil) - if err != nil { + if c, err = NewCache(f, 1<<20, nil); err != nil { t.Fatal("newCache", err) } @@ -51,18 +48,22 @@ func newcache(t *testing.T) (c *Cache) { } func TestCache0(t *testing.T) { - c := newcache(t) + dir, name, c := newcache(t) + defer os.RemoveAll(dir) + if err := c.Close(); err != nil { t.Fatal(10, err) } - if b := readfile(t); len(b) != 0 { + if b := readfile(t, name); len(b) != 0 { t.Fatal(20, len(b), 0) } } func TestCache1(t *testing.T) { - c := newcache(t) + dir, name, c := newcache(t) + defer os.RemoveAll(dir) + if n, err := c.WriteAt([]byte{0xa5}, 0); n != 1 { t.Fatal(20, n, err) } @@ -71,7 +72,7 @@ func TestCache1(t *testing.T) { t.Fatal(10, err) } - b := readfile(t) + b := readfile(t, name) if len(b) != 1 { t.Fatal(30, len(b), 1) }