From 88d8d541e42f193a6abd6c1918ac665bb7caf05a Mon Sep 17 00:00:00 2001 From: mangotree Date: Wed, 29 Apr 2020 14:14:09 +0800 Subject: [PATCH] fix: can not resuce bytes queue Change-Id: Id068d2a9baf44a9e4e8941464c0316aa0965ac4f --- bigcache_test.go | 38 ++++++++++++++++++++++++++++++++++++++ queue/bytes_queue.go | 11 ----------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/bigcache_test.go b/bigcache_test.go index 8a04060f..146e370a 100644 --- a/bigcache_test.go +++ b/bigcache_test.go @@ -747,6 +747,44 @@ func TestOldestEntryDeletionWhenMaxCacheSizeIsReached(t *testing.T) { assertEqual(t, blob('c', 1024*800), entry3) } +func TestNewEntryWriteWhenMaxCacheSizeIsReached(t *testing.T) { + t.Parallel() + + // given + cache, _ := NewBigCache(Config{ + Shards: 1, + LifeWindow: 5 * time.Second, + MaxEntriesInWindow: 2, + MaxEntrySize: 400, + HardMaxCacheSize: 1, + }) + + // when + key1Err := cache.Set("key1", blob('a', 1024*400)) + key2Err := cache.Set("key2", blob('b', 1024*400)) + key3Err := cache.Set("key3", blob('c', 1024*400)) + key4Err := cache.Set("key4", blob('d', 1024*400)) + + //then + noError(t, key1Err) + noError(t, key2Err) + noError(t, key3Err) + noError(t, key4Err) + + // when + _, key1Err = cache.Get("key1") + _, key2Err = cache.Get("key2") + entry3, _ := cache.Get("key3") + entry4, _ := cache.Get("key4") + + // then + assertEqual(t, key1Err, ErrEntryNotFound) + assertEqual(t, key2Err, ErrEntryNotFound) + assertEqual(t, blob('c', 1024*400), entry3) + assertEqual(t, blob('d', 1024*400), entry4) + +} + func TestRetrievingEntryShouldCopy(t *testing.T) { t.Parallel() diff --git a/queue/bytes_queue.go b/queue/bytes_queue.go index b45387b9..f2aa08b4 100644 --- a/queue/bytes_queue.go +++ b/queue/bytes_queue.go @@ -22,7 +22,6 @@ var ( // BytesQueue is a non-thread safe queue type of fifo based on bytes array. // For every push operation index of entry is returned. It can be used to read the entry later type BytesQueue struct { - full bool array []byte capacity int maxCapacity int @@ -78,7 +77,6 @@ func (q *BytesQueue) Reset() { q.head = leftMarginIndex q.rightMargin = leftMarginIndex q.count = 0 - q.full = false } // Push copies entry at the end of queue and moves tail pointer. Allocates more space if needed. @@ -143,9 +141,6 @@ func (q *BytesQueue) push(data []byte, len int) { if q.tail > q.head { q.rightMargin = q.tail } - if q.tail == q.head { - q.full = true - } q.count++ } @@ -238,9 +233,6 @@ func (q *BytesQueue) peek(index int) ([]byte, int, error) { // canInsertAfterTail returns true if it's possible to insert an entry of size of need after the tail of the queue func (q *BytesQueue) canInsertAfterTail(need int) bool { - if q.full { - return false - } if q.tail >= q.head { return q.capacity-q.tail >= need } @@ -253,9 +245,6 @@ func (q *BytesQueue) canInsertAfterTail(need int) bool { // canInsertBeforeHead returns true if it's possible to insert an entry of size of need before the head of the queue func (q *BytesQueue) canInsertBeforeHead(need int) bool { - if q.full { - return false - } if q.tail >= q.head { return q.head-leftMarginIndex == need || q.head-leftMarginIndex >= need+minimumHeaderSize }