Skip to content

Commit d580694

Browse files
author
Tony Lambiris
committed
Merge with upstream, apply PR Workiva#163
1 parent d119134 commit d580694

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

queue/queue.go

+64
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ func (items *items) getMatch(checker func(item interface{}) bool) []interface{}
164164
return returnItems
165165
}
166166

167+
func (items *items) getMatch(checker func(item interface{}) bool) []interface{} {
168+
length := len(*items)
169+
170+
if len(*items) == 0 {
171+
// returning nil here actually wraps that nil in a list
172+
// of interfaces... thanks go
173+
return []interface{}{}
174+
}
175+
176+
returnItems := make([]interface{}, 0, length)
177+
for _, item := range *items {
178+
if !checker(item) {
179+
returnItems = append(returnItems, item)
180+
}
181+
}
182+
183+
return returnItems
184+
}
185+
167186
type sema struct {
168187
ready chan bool
169188
response *sync.WaitGroup
@@ -369,6 +388,51 @@ func (q *Queue) Search(checker func(item interface{}) bool) []interface{} {
369388
return result
370389
}
371390

391+
// GetItem returns one item without deleting in this queue.
392+
func (q *Queue) GetItem(pos int) (interface{}, bool) {
393+
q.lock.Lock()
394+
defer q.lock.Unlock()
395+
if len(q.items) > pos {
396+
return q.items[pos], true
397+
}
398+
return nil, false
399+
}
400+
401+
// GetItems returns items in this queue.
402+
func (q *Queue) Clear(hint int64) {
403+
q.lock.Lock()
404+
defer q.lock.Unlock()
405+
q.items = make([]interface{}, 0, hint)
406+
}
407+
408+
// GetItems returns items in this queue.
409+
func (q *Queue) GetItems() []interface{} {
410+
q.lock.Lock()
411+
defer q.lock.Unlock()
412+
413+
return q.items
414+
}
415+
416+
// Search takes a function and returns a list of items that
417+
// match the checker. This does not wait and remove items.
418+
func (q *Queue) Search(checker func(item interface{}) bool) ([]interface{}) {
419+
if checker == nil {
420+
return nil
421+
}
422+
423+
q.lock.Lock()
424+
425+
if q.disposed {
426+
q.lock.Unlock()
427+
return nil
428+
}
429+
430+
result := q.items.getMatch(checker)
431+
q.lock.Unlock()
432+
return result
433+
}
434+
435+
372436
// GetItem returns one item without deleting in this queue.
373437
func (q *Queue) GetItem(pos int) (interface{}, bool) {
374438
q.lock.Lock()

queue/queue_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,57 @@ func TestGetEmpty(t *testing.T) {
190190
assert.Equal(t, `a`, result[0])
191191
}
192192

193+
func TestGetItems(t *testing.T) {
194+
q := New(10)
195+
196+
q.Put(`a`)
197+
198+
result := q.GetItems()
199+
200+
assert.Len(t, result, 1)
201+
assert.Equal(t, `a`, result[0])
202+
}
203+
204+
func TestSearch(t *testing.T) {
205+
q := New(10)
206+
207+
q.Put(`a`)
208+
q.Put(`b`)
209+
q.Put(`c`)
210+
211+
result := q.Search(func(item interface{}) bool {
212+
return item != `b`
213+
})
214+
215+
assert.Len(t, result, 1)
216+
assert.Equal(t, `b`, result[0])
217+
}
218+
219+
func TestGetItem(t *testing.T) {
220+
q := New(10)
221+
222+
q.Put(`a`)
223+
224+
result, ok := q.GetItem(0)
225+
if !assert.Equal(t, ok, true) {
226+
return
227+
}
228+
229+
assert.Equal(t, `a`, result)
230+
}
231+
232+
func TestClear(t *testing.T) {
233+
q := New(10)
234+
235+
q.Put(`a`)
236+
237+
result := q.GetItems()
238+
assert.Len(t, result, 1)
239+
q.Clear(10)
240+
result = q.GetItems()
241+
assert.Len(t, result, 0)
242+
}
243+
193244
func TestMultipleGetEmpty(t *testing.T) {
194245
q := New(10)
195246
var wg sync.WaitGroup

0 commit comments

Comments
 (0)