aboutsummaryrefslogtreecommitdiffstats
path: root/dex/cache_test.go
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-12-11 18:47:49 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:54 +0800
commit877339db1c966b1ec00dc94327a8aa8d00383234 (patch)
treed9bfd962a9503cb42736666aa6d7fbc23834e7fc /dex/cache_test.go
parent705c8302ee963467395dc288538c70b35cfdf2c1 (diff)
downloaddexon-877339db1c966b1ec00dc94327a8aa8d00383234.tar.gz
dexon-877339db1c966b1ec00dc94327a8aa8d00383234.tar.zst
dexon-877339db1c966b1ec00dc94327a8aa8d00383234.zip
dex: Pull blocks from blockdb if cache miss (#84)
Diffstat (limited to 'dex/cache_test.go')
-rw-r--r--dex/cache_test.go28
1 files changed, 26 insertions, 2 deletions
diff --git a/dex/cache_test.go b/dex/cache_test.go
index ac5609c1a..8a0066c94 100644
--- a/dex/cache_test.go
+++ b/dex/cache_test.go
@@ -23,6 +23,7 @@ import (
"testing"
coreCommon "github.com/dexon-foundation/dexon-consensus/common"
+ coreBlockdb "github.com/dexon-foundation/dexon-consensus/core/blockdb"
coreTypes "github.com/dexon-foundation/dexon-consensus/core/types"
)
@@ -41,7 +42,11 @@ func (v byHash) Swap(i int, j int) {
}
func TestCacheVote(t *testing.T) {
- cache := newCache(3)
+ db, err := coreBlockdb.NewMemBackedBlockDB()
+ if err != nil {
+ panic(err)
+ }
+ cache := newCache(3, db)
pos0 := coreTypes.Position{
Height: uint64(0),
}
@@ -126,7 +131,11 @@ func TestCacheVote(t *testing.T) {
}
func TestCacheBlock(t *testing.T) {
- cache := newCache(3)
+ db, err := coreBlockdb.NewMemBackedBlockDB()
+ if err != nil {
+ panic(err)
+ }
+ cache := newCache(3, db)
block1 := &coreTypes.Block{
Hash: coreCommon.NewRandomHash(),
}
@@ -178,4 +187,19 @@ func TestCacheBlock(t *testing.T) {
if !hasNewBlock {
t.Errorf("expect block %s in cache, have %v", block4, blocks)
}
+
+ block5 := &coreTypes.Block{
+ Hash: coreCommon.NewRandomHash(),
+ }
+ if err := db.Put(*block5); err != nil {
+ panic(err)
+ }
+ blocks = cache.blocks(coreCommon.Hashes{block5.Hash})
+ if len(blocks) != 1 {
+ t.Errorf("fail to get blocks: have %d, want 1", len(blocks))
+ } else {
+ if !blocks[0].Hash.Equal(block5.Hash) {
+ t.Errorf("get wrong block: have %s, want %s", blocks[0], block5)
+ }
+ }
}