From c36344b3d1f62bb0036ff39ff7970bef99b294bc Mon Sep 17 00:00:00 2001 From: jules01 Date: Wed, 27 Sep 2023 09:57:01 +0300 Subject: [PATCH] - fixes after review --- leveldb/leveldbSerial_test.go | 27 +++++++++++++++++++++++++++ rtcache/removalTrackingCache.go | 4 ++-- storageUnit/storageunit_test.go | 4 ++-- testscommon/cacherMock.go | 5 ++++- types/interface.go | 2 +- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/leveldb/leveldbSerial_test.go b/leveldb/leveldbSerial_test.go index 7716e2b6..6c3d90e7 100644 --- a/leveldb/leveldbSerial_test.go +++ b/leveldb/leveldbSerial_test.go @@ -416,3 +416,30 @@ func TestSerialDB_PutRemoveGet(t *testing.T) { _ = ldb.Close() } + +func TestSerialDB_PutRemovePutHas(t *testing.T) { + ldb := createSerialLevelDb(t, 100000, 1000000, 10) + + key := []byte("key") + value := []byte("value") + + _ = ldb.Put(key, value) + + // manually put the pair in storage + ldb.PutBatch() + time.Sleep(time.Second) + assert.Nil(t, ldb.Has(key)) // key was found + + // we now remove the key + _ = ldb.Remove(key) + + // manually delete the key from the storage + ldb.PutBatch() + time.Sleep(time.Second) + assert.NotNil(t, ldb.Has(key)) // missing key + + _ = ldb.Put(key, value) // put the key again + assert.Nil(t, ldb.Has(key)) // key was found + + _ = ldb.Close() +} diff --git a/rtcache/removalTrackingCache.go b/rtcache/removalTrackingCache.go index 7e14f1b8..757976a6 100644 --- a/rtcache/removalTrackingCache.go +++ b/rtcache/removalTrackingCache.go @@ -71,8 +71,8 @@ func (cache *removalTrackingCache) GetRemovalStatus(key []byte) types.RemovalSta // Clear is used to completely clear both caches. func (cache *removalTrackingCache) Clear() { - cache.mutCriticalArea.RLock() - defer cache.mutCriticalArea.RUnlock() + cache.mutCriticalArea.Lock() + defer cache.mutCriticalArea.Unlock() cache.removalCache.Clear() cache.unexportedCacher.Clear() diff --git a/storageUnit/storageunit_test.go b/storageUnit/storageunit_test.go index 4e1186e3..98beefd8 100644 --- a/storageUnit/storageunit_test.go +++ b/storageUnit/storageunit_test.go @@ -148,7 +148,7 @@ func TestUnit_GetWithExplicitlyRemovedKeyShouldNotCallThePersister(t *testing.T) return nil }, GetCalled: func(key []byte) ([]byte, error) { - assert.Fail(t, "should have not called Has") + assert.Fail(t, "should have not called Get") return nil, nil }, } @@ -219,7 +219,7 @@ func TestUnit_HasWithExplicitlyRemovedKeyShouldNotCallThePersister(t *testing.T) return nil }, GetCalled: func(key []byte) ([]byte, error) { - assert.Fail(t, "should have not called Has") + assert.Fail(t, "should have not called Get") return nil, nil }, } diff --git a/testscommon/cacherMock.go b/testscommon/cacherMock.go index fe94d4d1..ee3fa9f4 100644 --- a/testscommon/cacherMock.go +++ b/testscommon/cacherMock.go @@ -67,10 +67,13 @@ func (mock *cacherMock) HasOrAdd(key []byte, value interface{}, _ int) (has, add defer mock.mut.Unlock() _, found := mock.data[string(key)] + if found { + return found, !found + } mock.data[string(key)] = value - return !found, found + return found, !found } // Remove - diff --git a/types/interface.go b/types/interface.go index 66892372..00f45ded 100644 --- a/types/interface.go +++ b/types/interface.go @@ -14,7 +14,7 @@ const ( UnknownRemovalStatus RemovalStatus = "unknown" // ExplicitlyRemovedStatus defines the explicitly removed status ExplicitlyRemovedStatus RemovalStatus = "explicitly removed" - // NotRemovedStatus the key is not removed + // NotRemovedStatus defines the not removed status NotRemovedStatus RemovalStatus = "not removed" )