2018-11-08 13:53:27 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package testqueue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Queue is a threadsafe FIFO queue implementing storage.Queue
|
|
|
|
type Queue struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
s *list.List
|
|
|
|
}
|
|
|
|
|
|
|
|
//New returns a queue suitable for testing
|
|
|
|
func New() *Queue {
|
|
|
|
return &Queue{s: list.New(), mu: sync.Mutex{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Enqueue add a FIFO element
|
|
|
|
func (q *Queue) Enqueue(value storage.Value) error {
|
|
|
|
q.mu.Lock()
|
|
|
|
defer q.mu.Unlock()
|
|
|
|
q.s.PushBack(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Dequeue removes a FIFO element
|
|
|
|
func (q *Queue) Dequeue() (storage.Value, error) {
|
|
|
|
q.mu.Lock()
|
|
|
|
defer q.mu.Unlock()
|
|
|
|
for q.s.Len() > 0 {
|
|
|
|
e := q.s.Front() // First element
|
|
|
|
q.s.Remove(e) // Dequeue
|
|
|
|
return e.Value.(storage.Value), nil
|
|
|
|
}
|
2018-11-14 21:30:07 +00:00
|
|
|
return nil, storage.ErrEmptyQueue
|
2018-11-08 13:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Close closes the queue
|
|
|
|
func (q *Queue) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|