Skip to content

Commit

Permalink
Merge pull request #69 from marcoferrer/lifo-nil-pointer-fix
Browse files Browse the repository at this point in the history
bugfix for lifo queue nil pointer
  • Loading branch information
platinummonkey committed Oct 31, 2021
2 parents 5c06867 + 13f3d7f commit 5b2ca18
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
7 changes: 4 additions & 3 deletions limiter/lifo_blocking.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ func (q *lifoQueue) remove(id uint64) {
q.top.next = nil
q.top.prev = nil
q.top = next
q.top.id--
q.size--
return
}
next.prev = prev
prev.next = cur.next
if next != nil {
next.prev = nil
}
prev.next = next
q.size--
return

Expand Down
51 changes: 51 additions & 0 deletions limiter/lifo_blocking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,57 @@ func TestLifoQueue(t *testing.T) {
}
}

func TestLifoQueue_Remove(t *testing.T) {
t.Parallel()
asrt := assert.New(t)
q := lifoQueue{}

asrt.Equal(uint64(0), q.len())
size, ctx := q.peek()
asrt.Equal(uint64(0), size)
asrt.Nil(ctx)
asrt.Nil(q.pop())

for i := 1; i <= 10; i++ {
ctx := context.WithValue(context.Background(), testLifoQueueContextKey(i), i)
q.push(ctx)
}

// remove last
q.remove(1)
asrt.Equal(uint64(9), q.len())

// remove first
q.remove(q.len())
asrt.Equal(uint64(8), q.len())

// remove middle
q.remove(q.len() / 2)
asrt.Equal(uint64(7), q.len())

seenElements := make(map[uint64]struct{}, q.len())
var element *lifoElement
for {
element = q.pop()
if element == nil {
break
}
_, seen := seenElements[element.id]
asrt.False(seen, "no duplicate element ids allowed")
seenElements[element.id] = struct{}{}
}
asrt.Equal(uint64(0), q.len())
asrt.Equal(7, len(seenElements))

q = lifoQueue{}
ctx = context.WithValue(context.Background(), testLifoQueueContextKey(1), 1)
q.push(ctx)

// Remove very last item leaving queue empty
q.remove(1)
asrt.Equal(uint64(0), q.len())
}

func TestLifoBlockingListener(t *testing.T) {
t.Parallel()
delegateLimiter, _ := NewDefaultLimiterWithDefaults(
Expand Down

0 comments on commit 5b2ca18

Please sign in to comment.