feat: add tryEnqueue function. (#406)
* feat: add tryEnqueue function. tryEnqueue tries to enqueue a job to the given job channel. Returns true if the operation was successful, and false if enqueuing would not have been possible without blocking. Job is not enqueued in the latter case. Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> * remove inital worker Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
5b526a73bd
commit
27ec42543b
|
@ -25,8 +25,6 @@ func TestCorrectConf(t *testing.T) {
|
|||
func TestSenMultipleNotifications(t *testing.T) {
|
||||
PushConf, _ = config.LoadConf("")
|
||||
|
||||
InitWorkers(int64(2), 2)
|
||||
|
||||
PushConf.Ios.Enabled = true
|
||||
PushConf.Ios.KeyPath = "../certificate/certificate-valid.pem"
|
||||
err := InitAPNSClient()
|
||||
|
|
|
@ -57,7 +57,9 @@ func queueNotification(req RequestPush) (int, []LogPushEntry) {
|
|||
notification.log = &log
|
||||
notification.AddWaitCount()
|
||||
}
|
||||
QueueNotification <- *notification
|
||||
if !tryEnqueue(*notification, QueueNotification) {
|
||||
LogError.Error("max capacity reached")
|
||||
}
|
||||
count += len(notification.Tokens)
|
||||
// Count topic message
|
||||
if notification.To != "" {
|
||||
|
@ -73,3 +75,15 @@ func queueNotification(req RequestPush) (int, []LogPushEntry) {
|
|||
|
||||
return count, log
|
||||
}
|
||||
|
||||
// tryEnqueue tries to enqueue a job to the given job channel. Returns true if
|
||||
// the operation was successful, and false if enqueuing would not have been
|
||||
// possible without blocking. Job is not enqueued in the latter case.
|
||||
func tryEnqueue(job PushNotification, jobChan chan<- PushNotification) bool {
|
||||
select {
|
||||
case jobChan <- job:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package gorush
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTryEnqueue(t *testing.T) {
|
||||
chn := make(chan PushNotification, 2)
|
||||
assert.True(t, tryEnqueue(PushNotification{}, chn))
|
||||
assert.Equal(t, 1, len(chn))
|
||||
assert.True(t, tryEnqueue(PushNotification{}, chn))
|
||||
assert.Equal(t, 2, len(chn))
|
||||
assert.False(t, tryEnqueue(PushNotification{}, chn))
|
||||
assert.Equal(t, 2, len(chn))
|
||||
}
|
Loading…
Reference in New Issue