File tree Expand file tree Collapse file tree 2 files changed +28
-3
lines changed
Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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+
213233func TestEmptyGetWithDispose (t * testing.T ) {
214234 q := New (10 )
215235 var wg sync.WaitGroup
You can’t perform that action at this time.
0 commit comments