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())
|
|
|
|
|
2021-07-17 17:58:46 +00:00
|
|
|
w.Queue(&gorush.PushNotification{})
|
2021-07-17 00:20:45 +00:00
|
|
|
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())
|
|
|
|
|
2021-07-17 17:58:46 +00:00
|
|
|
assert.NoError(t, w.Queue(&gorush.PushNotification{}))
|
2021-07-17 00:20:45 +00:00
|
|
|
assert.Equal(t, 1, w.Usage())
|
2021-07-17 17:58:46 +00:00
|
|
|
assert.NoError(t, w.Queue(&gorush.PushNotification{}))
|
2021-07-17 00:20:45 +00:00
|
|
|
assert.Equal(t, 2, w.Usage())
|
2021-07-17 17:58:46 +00:00
|
|
|
assert.Error(t, w.Queue(&gorush.PushNotification{}))
|
2021-07-17 00:20:45 +00:00
|
|
|
assert.Equal(t, 2, w.Usage())
|
|
|
|
|
2021-07-17 17:58:46 +00:00
|
|
|
err := w.Queue(&gorush.PushNotification{})
|
2021-07-17 00:20:45 +00:00
|
|
|
assert.Equal(t, errMaxCapacity, err)
|
|
|
|
}
|