From f72602631221679f99e89b28007613a335a00566 Mon Sep 17 00:00:00 2001 From: lash Date: Thu, 4 Oct 2018 08:45:51 +0200 Subject: [PATCH] swarm/storage: Add GC test over and under maxGCItems --- swarm/storage/ldbstore.go | 3 --- swarm/storage/ldbstore_test.go | 45 ++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/swarm/storage/ldbstore.go b/swarm/storage/ldbstore.go index 3f67cd409d..c4fe1ce24d 100644 --- a/swarm/storage/ldbstore.go +++ b/swarm/storage/ldbstore.go @@ -575,9 +575,6 @@ func (s *LDBStore) delete(idx *dpaDBIndex, idxKey []byte, po uint8) { gcIdxKey := getGCIdxKey(idx) batch.Delete(gcIdxKey) batch.Delete(getDataKey(idx.Idx, po)) - if s.entryCnt == 0 { - panic("") - } s.entryCnt-- dbEntryCount.Dec(1) cntKey := make([]byte, 2) diff --git a/swarm/storage/ldbstore_test.go b/swarm/storage/ldbstore_test.go index 9bd17ba5e1..9acac83ebd 100644 --- a/swarm/storage/ldbstore_test.go +++ b/swarm/storage/ldbstore_test.go @@ -22,6 +22,8 @@ import ( "fmt" "io/ioutil" "os" + "strconv" + "strings" "testing" "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 // retrieve only some of them, because garbage collection must have cleared some of them -func TestLDBStoreCollectGarbage(t *testing.T) { - capacity := 500 - n := 2000 +func testLDBStoreCollectGarbage(t *testing.T) { + + 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.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 -func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) { - //capacity := 11 - //surplus := 4 - capacity := 10000 - surplus := 10000 +func testLDBStoreRemoveThenCollectGarbage(t *testing.T) { + + params := strings.Split(t.Name(), "/") + capacity, err := strconv.Atoi(params[2]) + if err != nil { + t.Fatal(err) + } + surplus, err := strconv.Atoi(params[3]) + if err != nil { + t.Fatal(err) + } ldb, cleanup := newLDBStore(t) ldb.setCapacity(uint64(capacity)) @@ -425,8 +452,6 @@ func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) { cleanup() ldb, cleanup = newLDBStore(t) - //capacity = 10 - capacity = 10000 ldb.setCapacity(uint64(capacity)) defer cleanup()