chore(queue): add queue message interface (#602)
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -17,7 +17,7 @@ var errMaxCapacity = errors.New("max capacity reached")
|
||||
|
||||
// Worker for simple queue using channel
|
||||
type Worker struct {
|
||||
queueNotification chan gorush.PushNotification
|
||||
queueNotification chan queue.QueuedMessage
|
||||
}
|
||||
|
||||
// BeforeRun run script before start worker
|
||||
@@ -56,9 +56,9 @@ func (s *Worker) Usage() int {
|
||||
}
|
||||
|
||||
// Queue send notification to queue
|
||||
func (s *Worker) Queue(job interface{}) error {
|
||||
func (s *Worker) Queue(job queue.QueuedMessage) error {
|
||||
select {
|
||||
case s.queueNotification <- job.(gorush.PushNotification):
|
||||
case s.queueNotification <- job:
|
||||
return nil
|
||||
default:
|
||||
return errMaxCapacity
|
||||
@@ -68,14 +68,14 @@ func (s *Worker) Queue(job interface{}) error {
|
||||
// WithQueueNum setup the capcity of queue
|
||||
func WithQueueNum(num int) Option {
|
||||
return func(w *Worker) {
|
||||
w.queueNotification = make(chan gorush.PushNotification, num)
|
||||
w.queueNotification = make(chan queue.QueuedMessage, num)
|
||||
}
|
||||
}
|
||||
|
||||
// NewWorker for struc
|
||||
func NewWorker(opts ...Option) *Worker {
|
||||
w := &Worker{
|
||||
queueNotification: make(chan gorush.PushNotification, runtime.NumCPU()<<1),
|
||||
queueNotification: make(chan queue.QueuedMessage, runtime.NumCPU()<<1),
|
||||
}
|
||||
|
||||
// Loop through each option
|
||||
|
||||
@@ -14,7 +14,7 @@ func TestQueueUsage(t *testing.T) {
|
||||
assert.Equal(t, runtime.NumCPU()<<1, w.Capacity())
|
||||
assert.Equal(t, 0, w.Usage())
|
||||
|
||||
w.Queue(gorush.PushNotification{})
|
||||
w.Queue(&gorush.PushNotification{})
|
||||
assert.Equal(t, 1, w.Usage())
|
||||
}
|
||||
|
||||
@@ -23,13 +23,13 @@ func TestMaxCapacity(t *testing.T) {
|
||||
assert.Equal(t, 2, w.Capacity())
|
||||
assert.Equal(t, 0, w.Usage())
|
||||
|
||||
assert.NoError(t, w.Queue(gorush.PushNotification{}))
|
||||
assert.NoError(t, w.Queue(&gorush.PushNotification{}))
|
||||
assert.Equal(t, 1, w.Usage())
|
||||
assert.NoError(t, w.Queue(gorush.PushNotification{}))
|
||||
assert.NoError(t, w.Queue(&gorush.PushNotification{}))
|
||||
assert.Equal(t, 2, w.Usage())
|
||||
assert.Error(t, w.Queue(gorush.PushNotification{}))
|
||||
assert.Error(t, w.Queue(&gorush.PushNotification{}))
|
||||
assert.Equal(t, 2, w.Usage())
|
||||
|
||||
err := w.Queue(gorush.PushNotification{})
|
||||
err := w.Queue(&gorush.PushNotification{})
|
||||
assert.Equal(t, errMaxCapacity, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user