chore(queue): to handle panic cases from inside the worker (#594)

This commit is contained in:
Bo-Yi Wu
2021-07-17 08:20:45 +08:00
committed by GitHub
parent 4349a17017
commit 95866bf205
3 changed files with 55 additions and 8 deletions

View File

@@ -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
}
}

View 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)
}