Skip to content

Commit 0d929fe

Browse files
Merge pull request #105 from dominicm/queue_return_items_disposed
Return items disposed when disposing queue
2 parents 064f3ea + 3e6fc13 commit 0d929fe

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

queue/queue.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,10 @@ func (q *Queue) Disposed() bool {
276276
return q.disposed
277277
}
278278

279-
// Dispose will dispose of this queue. Any subsequent
280-
// calls to Get or Put will return an error.
281-
func (q *Queue) Dispose() {
279+
// Dispose will dispose of this queue and returns
280+
// the items disposed. Any subsequent calls to Get
281+
// or Put will return an error.
282+
func (q *Queue) Dispose() []interface{} {
282283
q.lock.Lock()
283284
defer q.lock.Unlock()
284285

@@ -288,8 +289,12 @@ func (q *Queue) Dispose() {
288289
waiter.ready <- true
289290
}
290291

292+
disposedItems := q.items
293+
291294
q.items = nil
292295
q.waiters = nil
296+
297+
return disposedItems
293298
}
294299

295300
// New is a constructor for a new threadsafe queue.

queue/queue_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ func TestMultipleGetEmpty(t *testing.T) {
210210
}
211211
}
212212

213+
func TestDispose(t *testing.T) {
214+
// when the queue is empty
215+
q := New(10)
216+
itemsDisposed := q.Dispose()
217+
218+
assert.Empty(t, itemsDisposed)
219+
220+
// when the queue is not empty
221+
q = New(10)
222+
q.Put(`1`)
223+
itemsDisposed = q.Dispose()
224+
225+
expected := []interface{}{`1`}
226+
assert.Equal(t, expected, itemsDisposed)
227+
228+
// when the queue has been disposed
229+
itemsDisposed = q.Dispose()
230+
assert.Nil(t, itemsDisposed)
231+
}
232+
213233
func TestEmptyGetWithDispose(t *testing.T) {
214234
q := New(10)
215235
var wg sync.WaitGroup

0 commit comments

Comments
 (0)