swarm/storage: Add GC test over and under maxGCItems

This commit is contained in:
lash 2018-10-04 08:45:51 +02:00
parent 9e68d6d46f
commit f726026312
2 changed files with 35 additions and 13 deletions

View file

@ -575,9 +575,6 @@ func (s *LDBStore) delete(idx *dpaDBIndex, idxKey []byte, po uint8) {
gcIdxKey := getGCIdxKey(idx) gcIdxKey := getGCIdxKey(idx)
batch.Delete(gcIdxKey) batch.Delete(gcIdxKey)
batch.Delete(getDataKey(idx.Idx, po)) batch.Delete(getDataKey(idx.Idx, po))
if s.entryCnt == 0 {
panic("")
}
s.entryCnt-- s.entryCnt--
dbEntryCount.Dec(1) dbEntryCount.Dec(1)
cntKey := make([]byte, 2) cntKey := make([]byte, 2)

View file

@ -22,6 +22,8 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strconv"
"strings"
"testing" "testing"
"time" "time"
@ -296,11 +298,30 @@ func TestLDBStoreWithoutCollectGarbage(t *testing.T) {
} }
} }
func TestLDBStoreCollectGarbage(t *testing.T) {
cap := maxGCItems / 2
t.Run(fmt.Sprintf("A/%d/%d", cap, cap*4), testLDBStoreCollectGarbage)
t.Run(fmt.Sprintf("B/%d/%d", cap, cap*4), testLDBStoreRemoveThenCollectGarbage)
cap = maxGCItems * 2
t.Run(fmt.Sprintf("A/%d/%d", cap, cap*4), testLDBStoreCollectGarbage)
t.Run(fmt.Sprintf("B/%d/%d", cap, cap*4), testLDBStoreRemoveThenCollectGarbage)
}
// TestLDBStoreCollectGarbage tests that we can put more chunks than LevelDB's capacity, and // TestLDBStoreCollectGarbage tests that we can put more chunks than LevelDB's capacity, and
// retrieve only some of them, because garbage collection must have cleared some of them // retrieve only some of them, because garbage collection must have cleared some of them
func TestLDBStoreCollectGarbage(t *testing.T) { func testLDBStoreCollectGarbage(t *testing.T) {
capacity := 500
n := 2000 params := strings.Split(t.Name(), "/")
capacity, err := strconv.Atoi(params[2])
if err != nil {
t.Fatal(err)
}
n, err := strconv.Atoi(params[3])
if err != nil {
t.Fatal(err)
}
ldb, cleanup := newLDBStore(t) ldb, cleanup := newLDBStore(t)
ldb.setCapacity(uint64(capacity)) ldb.setCapacity(uint64(capacity))
@ -384,11 +405,17 @@ func TestLDBStoreAddRemove(t *testing.T) {
} }
// TestLDBStoreRemoveThenCollectGarbage tests that we can delete chunks and that we can trigger garbage collection // TestLDBStoreRemoveThenCollectGarbage tests that we can delete chunks and that we can trigger garbage collection
func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) { func testLDBStoreRemoveThenCollectGarbage(t *testing.T) {
//capacity := 11
//surplus := 4 params := strings.Split(t.Name(), "/")
capacity := 10000 capacity, err := strconv.Atoi(params[2])
surplus := 10000 if err != nil {
t.Fatal(err)
}
surplus, err := strconv.Atoi(params[3])
if err != nil {
t.Fatal(err)
}
ldb, cleanup := newLDBStore(t) ldb, cleanup := newLDBStore(t)
ldb.setCapacity(uint64(capacity)) ldb.setCapacity(uint64(capacity))
@ -425,8 +452,6 @@ func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) {
cleanup() cleanup()
ldb, cleanup = newLDBStore(t) ldb, cleanup = newLDBStore(t)
//capacity = 10
capacity = 10000
ldb.setCapacity(uint64(capacity)) ldb.setCapacity(uint64(capacity))
defer cleanup() defer cleanup()