GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cache-mem/aof_test.go at main · ubgo/cache-mem Expand file tree/ Copy pathaof_test.goMore file actions 105 lines (90 loc) · 2.85 KB / Copy pathaof_test.goFile metadata and controls105 lines (90 loc) · 2.85 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 // aof_test.go — tests for append-only-file durability: append, replay, truncated-tail recovery, and CompactAOF. package memcache_test import ( "context" "os" "path/filepath" "testing" "time" memcache "github.com/ubgo/cache-mem" ) func TestAOFReplayAfterRestart(t *testing.T) { ctx := context.Background() path := filepath.Join(t.TempDir(), "cache.aof") c1 := memcache.New(memcache.WithAOF(path)) _ = c1.Set(ctx, "a", []byte("1"), 0) _ = c1.Set(ctx, "b", []byte("2"), time.Hour) _, _ = c1.Incr(ctx, "ctr", 5) _ = c1.Del(ctx, "b") _ = c1.Set(ctx, "c", []byte("3"), 0) if err := c1.Close(); err != nil { t.Fatal(err) } // "Restart": a fresh cache pointed at the same AOF must reconstruct state. c2 := memcache.New(memcache.WithAOF(path)) defer func() { _ = c2.Close() }() if v, err := c2.Get(ctx, "a"); err != nil || string(v) != "1" { t.Fatalf("a not recovered: %q %v", v, err) } if v, err := c2.Get(ctx, "c"); err != nil || string(v) != "3" { t.Fatalf("c not recovered: %q %v", v, err) } if has, _ := c2.Has(ctx, "b"); has { t.Fatal("b was deleted before restart; must not reappear") } if n, _ := c2.Incr(ctx, "ctr", 0); n != 5 { t.Fatalf("counter not recovered: got %d want 5", n) } } func TestAOFFlushClearsReplayedState(t *testing.T) { ctx := context.Background() path := filepath.Join(t.TempDir(), "f.aof") c1 := memcache.New(memcache.WithAOF(path)) _ = c1.Set(ctx, "x", []byte("v"), 0) _ = c1.Flush(ctx) _ = c1.Set(ctx, "y", []byte("w"), 0) _ = c1.Close() c2 := memcache.New(memcache.WithAOF(path)) defer func() { _ = c2.Close() }() if has, _ := c2.Has(ctx, "x"); has { t.Fatal("x set before Flush must not survive replay") } if v, _ := c2.Get(ctx, "y"); string(v) != "w" { t.Fatalf("y written after Flush must survive: %q", v) } } func TestCompactAOFPreservesState(t *testing.T) { ctx := context.Background() path := filepath.Join(t.TempDir(), "c.aof") c1 := memcache.New(memcache.WithAOF(path)) for i := 0; i < 100; i++ { _ = c1.Set(ctx, "k", []byte("old"), 0) // many rewrites of one key } _ = c1.Set(ctx, "k", []byte("final"), 0) _ = c1.Set(ctx, "keep", []byte("yes"), 0) fi1 := fileSize(t, path) if err := c1.CompactAOF(); err != nil { t.Fatal(err) } fi2 := fileSize(t, path) if fi2 >= fi1 { t.Fatalf("compaction did not shrink the log (%d -> %d)", fi1, fi2) } _ = c1.Set(ctx, "after", []byte("z"), 0) // appends still work post-compact _ = c1.Close() c2 := memcache.New(memcache.WithAOF(path)) defer func() { _ = c2.Close() }() for k, want := range map[string]string{"k": "final", "keep": "yes", "after": "z"} { if v, err := c2.Get(ctx, k); err != nil || string(v) != want { t.Fatalf("post-compact %s: %q %v (want %q)", k, v, err, want) } } } func fileSize(t *testing.T, path string) int64 { t.Helper() fi, err := os.Stat(path) if err != nil { t.Fatalf("stat %s: %v", path, err) } return fi.Size() } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |