2021-07-17 00:20:45 +00:00
|
|
|
package simple
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/appleboy/gorush/gorush"
|
2021-07-17 12:14:19 +00:00
|
|
|
|
2021-07-17 00:20:45 +00:00
|
|
|
"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)
|
|
|
|
}
|