MCPcopy Create free account
hub / github.com/DNAProject/DNA / TestCacheDB

Function TestCacheDB

smartcontract/storage/cachedb_test.go:41–87  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

39}
40
41func TestCacheDB(t *testing.T) {
42 N := 10000
43 mem := make(map[string]string)
44 memback, _ := leveldbstore.NewMemLevelDBStore()
45 overlay := overlaydb.NewOverlayDB(memback)
46
47 cache := NewCacheDB(overlay)
48 for i := 0; i < N; i++ {
49 key, val := genRandKeyVal()
50 cache.Put([]byte(key), []byte(val))
51 mem[key] = val
52 }
53
54 for key := range mem {
55 op := rand.Int() % 2
56 if op == 0 {
57 //delete
58 delete(mem, key)
59 cache.Delete([]byte(key))
60 } else if op == 1 {
61 //update
62 _, val := genRandKeyVal()
63 mem[key] = val
64 cache.Put([]byte(key), []byte(val))
65 }
66 }
67
68 for key, val := range mem {
69 value, err := cache.Get([]byte(key))
70 assert.Nil(t, err)
71 assert.NotNil(t, value)
72 assert.Equal(t, []byte(val), value)
73 }
74 cache.Commit()
75
76 prefix := common.ST_STORAGE
77 for key, val := range mem {
78 pkey := make([]byte, 1+len(key))
79 pkey[0] = byte(prefix)
80 copy(pkey[1:], key)
81 raw, err := overlay.Get(pkey)
82 assert.Nil(t, err)
83 assert.NotNil(t, raw)
84 assert.Equal(t, []byte(val), raw)
85 }
86
87}

Callers

nothing calls this directly

Calls 9

PutMethod · 0.95
DeleteMethod · 0.95
GetMethod · 0.95
CommitMethod · 0.95
GetMethod · 0.95
NewMemLevelDBStoreFunction · 0.92
NewOverlayDBFunction · 0.92
NewCacheDBFunction · 0.85
genRandKeyValFunction · 0.85

Tested by

no test coverage detected