Skip to content

Commit

Permalink
Fix Reset() and add TestResetFullQueue (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
neal-zhu committed Mar 21, 2020
1 parent 8fecd16 commit c47ca6f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions queue/bytes_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ 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.
Expand Down
26 changes: 26 additions & 0 deletions queue/bytes_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ func TestPeek(t *testing.T) {
assertEqual(t, entry, read)
}

func TestResetFullQueue(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(10, 20, false)

// when
queue.Push(blob('a', 3))
queue.Push(blob('b', 4))

// when
assertEqual(t, blob('a', 3), pop(queue)) // space freed at the beginning
_, err := queue.Push(blob('a', 3)) // will set q.full to true

// then
assertEqual(t, err, nil)

// when
queue.Reset()
queue.Push(blob('c', 8)) // should not trigger a re-allocation

// then
assertEqual(t, blob('c', 8), pop(queue))
assertEqual(t, queue.Capacity(), 10)
}

func TestReset(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit c47ca6f

Please sign in to comment.