diff --git a/gorush/status.go b/gorush/status.go index ccdc569..d75122f 100644 --- a/gorush/status.go +++ b/gorush/status.go @@ -44,18 +44,29 @@ func initApp() { RushStatus.Android.PushError = 0 } -func initRedis() { +func initRedis() error { RedisClient = redis.NewClient(&redis.Options{ Addr: PushConf.Stat.Redis.Addr, Password: PushConf.Stat.Redis.Password, DB: PushConf.Stat.Redis.DB, }) + _, err := RedisClient.Ping().Result() + + if err != nil { + // redis server error + LogError.Error("Can't connect redis server: " + err.Error()) + + return err + } + RushStatus.TotalCount = getTotalCount() RushStatus.Ios.PushSuccess = getIosSuccess() RushStatus.Ios.PushError = getIosError() RushStatus.Android.PushSuccess = getAndroidSuccess() RushStatus.Android.PushError = getAndroidError() + + return nil } // InitAppStatus for initialize app status diff --git a/gorush/status_test.go b/gorush/status_test.go index 371d6dd..c80c1f4 100644 --- a/gorush/status_test.go +++ b/gorush/status_test.go @@ -51,6 +51,22 @@ func TestAddAndroidError(t *testing.T) { assert.Equal(t, int64(1000), val) } +func TestRedisServerSuccess(t *testing.T) { + PushConf.Stat.Redis.Addr = "localhost:6379" + + err := initRedis() + + assert.NoError(t, err) +} + +func TestRedisServerError(t *testing.T) { + PushConf.Stat.Redis.Addr = "localhost:6370" + + err := initRedis() + + assert.Error(t, err) +} + func TestStatForRedisEngine(t *testing.T) { var val int64 PushConf.Stat.Engine = "redis"