Skip to content

Commit 37b9eb2

Browse files
authored
Fix Reset() and add TestResetFullQueue (#213)
1 parent 15aeb0b commit 37b9eb2

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

queue/bytes_queue.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (q *BytesQueue) Reset() {
7878
q.head = leftMarginIndex
7979
q.rightMargin = leftMarginIndex
8080
q.count = 0
81+
q.full = false
8182
}
8283

8384
// Push copies entry at the end of queue and moves tail pointer. Allocates more space if needed.

queue/bytes_queue_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ func TestPeek(t *testing.T) {
7171
assertEqual(t, entry, read)
7272
}
7373

74+
func TestResetFullQueue(t *testing.T) {
75+
t.Parallel()
76+
77+
// given
78+
queue := NewBytesQueue(10, 20, false)
79+
80+
// when
81+
queue.Push(blob('a', 3))
82+
queue.Push(blob('b', 4))
83+
84+
// when
85+
assertEqual(t, blob('a', 3), pop(queue)) // space freed at the beginning
86+
_, err := queue.Push(blob('a', 3)) // will set q.full to true
87+
88+
// then
89+
assertEqual(t, err, nil)
90+
91+
// when
92+
queue.Reset()
93+
queue.Push(blob('c', 8)) // should not trigger a re-allocation
94+
95+
// then
96+
assertEqual(t, blob('c', 8), pop(queue))
97+
assertEqual(t, queue.Capacity(), 10)
98+
}
99+
74100
func TestReset(t *testing.T) {
75101
t.Parallel()
76102

0 commit comments

Comments
 (0)