OSDN Git Service

add TestVoteResultCache
authorChengcheng Zhang <943420582@qq.com>
Sat, 15 Jun 2019 10:06:02 +0000 (18:06 +0800)
committerChengcheng Zhang <943420582@qq.com>
Sat, 15 Jun 2019 10:06:02 +0000 (18:06 +0800)
database/cache_test.go

index 673ab7d..3d06633 100644 (file)
@@ -5,6 +5,7 @@ import (
 
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
+       "github.com/vapor/protocol/state"
 )
 
 func TestBlockCache(t *testing.T) {
@@ -47,3 +48,39 @@ func TestBlockCache(t *testing.T) {
                }
        }
 }
+
+func TestVoteResultCache(t *testing.T) {
+       newVoteResult := func(seq uint64) *state.VoteResult {
+               return &state.VoteResult{
+                       Seq: seq,
+               }
+       }
+       voteResults := make(map[uint64]*state.VoteResult)
+       for i := 0; i < maxCachedVoteResults+10; i++ {
+               voteResult := newVoteResult(uint64(i))
+               voteResults[voteResult.Seq] = voteResult
+       }
+
+       cache := newVoteResultCache(func(seq uint64) (*state.VoteResult, error) {
+               return voteResults[seq], nil
+       })
+
+       for i := 0; i < maxCachedVoteResults+10; i++ {
+               voteResult := newVoteResult(uint64(i))
+               cache.lookup(voteResult.Seq)
+       }
+
+       for i := 0; i < 10; i++ {
+               voteResult := newVoteResult(uint64(i))
+               if v, _ := cache.get(voteResult.Seq); v != nil {
+                       t.Fatalf("find old vote result")
+               }
+       }
+
+       for i := 10; i < maxCachedVoteResults+10; i++ {
+               voteResult := newVoteResult(uint64(i))
+               if v, _ := cache.get(voteResult.Seq); v == nil {
+                       t.Fatalf("can't find new vote result")
+               }
+       }
+}