Merge pull request #24 from appleboy/Goroutines

support go goroutines.
This commit is contained in:
Bo-Yi Wu 2016-04-03 21:24:12 +08:00
commit 1f71f6cef2
3 changed files with 76 additions and 50 deletions

View File

@ -115,24 +115,20 @@ func InitAPNSClient() error {
}
func pushNotification(notification RequestPushNotification) bool {
var (
success bool
)
switch notification.Platform {
case PlatFormIos:
if !PushConf.Ios.Enabled {
return false
}
success = pushNotificationIos(notification)
go PushToIOS(notification)
case PlatFormAndroid:
if !PushConf.Android.Enabled {
return false
}
success = pushNotificationAndroid(notification)
go PushToAndroid(notification)
}
return success
return true
}
// The iOS Notification Payload
@ -226,7 +222,7 @@ func GetIOSNotification(req RequestPushNotification) *apns.Notification {
return notification
}
func pushNotificationIos(req RequestPushNotification) bool {
func PushToIOS(req RequestPushNotification) bool {
notification := GetIOSNotification(req)
@ -303,7 +299,7 @@ func GetAndroidNotification(req RequestPushNotification) gcm.HttpMessage {
return notification
}
func pushNotificationAndroid(req RequestPushNotification) bool {
func PushToAndroid(req RequestPushNotification) bool {
notification := GetAndroidNotification(req)

View File

@ -6,6 +6,7 @@ import (
"github.com/google/go-gcm"
"github.com/stretchr/testify/assert"
"log"
"os"
"testing"
)
@ -203,3 +204,70 @@ func TestAndroidNotificationStructure(t *testing.T) {
assert.Equal(t, test, notification.Notification.Title)
assert.Equal(t, "Welcome", notification.Notification.Body)
}
func TestPushToIOS(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem"
InitAPNSClient()
req := RequestPushNotification{
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
Platform: 1,
Message: "Welcome",
}
success := PushToIOS(req)
assert.False(t, success)
}
func TestPushToAndroidWrongAPIKey(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") + "a"
req := RequestPushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: 2,
Message: "Welcome",
}
success := PushToAndroid(req)
assert.False(t, success)
}
func TestPushToAndroidWrongToken(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY")
req := RequestPushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: 2,
Message: "Welcome",
}
success := PushToAndroid(req)
assert.True(t, success)
}
func TestPushToAndroidRightToken(t *testing.T) {
PushConf = BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY")
android_token := os.Getenv("ANDROID_TEST_TOKEN")
req := RequestPushNotification{
Tokens: []string{android_token, "bbbbb"},
Platform: 2,
Message: "Welcome",
}
success := PushToAndroid(req)
assert.True(t, success)
}

View File

@ -26,6 +26,8 @@ func TestPrintGoPushVersion(t *testing.T) {
func TestRunNormalServer(t *testing.T) {
initTest()
gin.SetMode(gin.TestMode)
router := gin.New()
go func() {
@ -199,47 +201,7 @@ func TestDisabledAndroidPushHandler(t *testing.T) {
})
}
func TestAndroidWrongAPIKey(t *testing.T) {
initTest()
PushConf.Android.Enabled = true
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") + "a"
r := gofight.New()
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{"aaaaaa", "bbbbb"},
"platform": 2,
"message": "Welcome",
}).
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestAndroidWrongToken(t *testing.T) {
initTest()
PushConf.Android.Enabled = true
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY")
r := gofight.New()
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{"aaaaaa", "bbbbb"},
"platform": 2,
"message": "Welcome",
}).
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestAndroidRightToken(t *testing.T) {
func TestAndroidPushHandler(t *testing.T) {
initTest()
PushConf.Android.Enabled = true