chore(queue): to handle panic cases from inside the worker (#594)
This commit is contained in:
@@ -13,6 +13,8 @@ var _ queue.Worker = (*Worker)(nil)
|
||||
// Option for queue system
|
||||
type Option func(*Worker)
|
||||
|
||||
var errMaxCapacity = errors.New("max capacity reached")
|
||||
|
||||
// Worker for simple queue using channel
|
||||
type Worker struct {
|
||||
queueNotification chan gorush.PushNotification
|
||||
@@ -49,7 +51,7 @@ func (s *Worker) Queue(job interface{}) error {
|
||||
case s.queueNotification <- job.(gorush.PushNotification):
|
||||
return nil
|
||||
default:
|
||||
return errors.New("max capacity reached")
|
||||
return errMaxCapacity
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
34
queue/simple/simple_test.go
Normal file
34
queue/simple/simple_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package simple
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/appleboy/gorush/gorush"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestQueueUsage(t *testing.T) {
|
||||
w := NewWorker()
|
||||
assert.Equal(t, runtime.NumCPU()<<1, w.Capacity())
|
||||
assert.Equal(t, 0, w.Usage())
|
||||
|
||||
w.Queue(gorush.PushNotification{})
|
||||
assert.Equal(t, 1, w.Usage())
|
||||
}
|
||||
|
||||
func TestMaxCapacity(t *testing.T) {
|
||||
w := NewWorker(WithQueueNum(2))
|
||||
assert.Equal(t, 2, w.Capacity())
|
||||
assert.Equal(t, 0, w.Usage())
|
||||
|
||||
assert.NoError(t, w.Queue(gorush.PushNotification{}))
|
||||
assert.Equal(t, 1, w.Usage())
|
||||
assert.NoError(t, w.Queue(gorush.PushNotification{}))
|
||||
assert.Equal(t, 2, w.Usage())
|
||||
assert.Error(t, w.Queue(gorush.PushNotification{}))
|
||||
assert.Equal(t, 2, w.Usage())
|
||||
|
||||
err := w.Queue(gorush.PushNotification{})
|
||||
assert.Equal(t, errMaxCapacity, err)
|
||||
}
|
||||
Reference in New Issue
Block a user