diff --git a/Makefile b/Makefile index e0f36f0..63bddf8 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,10 @@ build: clean sh script/build.sh test: - cd gorush && go test -cover -v ./... + cd gorush && go test -cover -v -coverprofile=coverage.out + +html: test + cd gorush && go tool cover -html=coverage.out docker_build: clean tar -zcvf build.tar.gz gorush.go gorush diff --git a/gorush/notification_test.go b/gorush/notification_test.go index d4af358..bf86965 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -470,7 +470,7 @@ func TestGCMMessage(t *testing.T) { // the message must specify at least one registration ID req = PushNotification{ Message: "Test", - Tokens: []string{}, + Tokens: []string{}, } err = CheckMessage(req) @@ -479,7 +479,7 @@ func TestGCMMessage(t *testing.T) { // the token must not be empty req = PushNotification{ Message: "Test", - Tokens: []string{""}, + Tokens: []string{""}, } err = CheckMessage(req) @@ -487,9 +487,9 @@ func TestGCMMessage(t *testing.T) { // the message may specify at most 1000 registration IDs req = PushNotification{ - Message: "Test", + Message: "Test", Platform: PlatFormAndroid, - Tokens: make([]string, 1001), + Tokens: make([]string, 1001), } err = CheckMessage(req) @@ -498,9 +498,9 @@ func TestGCMMessage(t *testing.T) { // the message's TimeToLive field must be an integer // between 0 and 2419200 (4 weeks) req = PushNotification{ - Message: "Test", - Platform: PlatFormAndroid, - Tokens: []string{"XXXXXXXXX"}, + Message: "Test", + Platform: PlatFormAndroid, + Tokens: []string{"XXXXXXXXX"}, TimeToLive: 2419201, } @@ -509,9 +509,9 @@ func TestGCMMessage(t *testing.T) { // Pass req = PushNotification{ - Message: "Test", - Platform: PlatFormAndroid, - Tokens: []string{"XXXXXXXXX"}, + Message: "Test", + Platform: PlatFormAndroid, + Tokens: []string{"XXXXXXXXX"}, TimeToLive: 86400, } @@ -526,9 +526,9 @@ func TestCheckAndroidMessage(t *testing.T) { PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") req := PushNotification{ - Tokens: []string{"aaaaaa", "bbbbb"}, - Platform: PlatFormAndroid, - Message: "Welcome", + Tokens: []string{"aaaaaa", "bbbbb"}, + Platform: PlatFormAndroid, + Message: "Welcome", TimeToLive: 2419201, } diff --git a/gorush/storage.go b/gorush/storage.go new file mode 100644 index 0000000..a17e64c --- /dev/null +++ b/gorush/storage.go @@ -0,0 +1,15 @@ +package gorush + +// Storage interface +type Storage interface { + addTotalCount(int64) + addIosSuccess(int64) + addIosError(int64) + addAndroidSuccess(int64) + addAndroidError(int64) + getTotalCount() int64 + getIosSuccess() int64 + getIosError() int64 + getAndroidSuccess() int64 + getAndroidError() int64 +} diff --git a/storage/memory/memory.go b/storage/memory/memory.go new file mode 100644 index 0000000..d9cdec0 --- /dev/null +++ b/storage/memory/memory.go @@ -0,0 +1,67 @@ +package memory + +import ( + "github.com/appleboy/gorush/gorush" + "sync/atomic" +) + +// Storage implements the storage interface for gorush (https://github.com/appleboy/gorush) +func New(stat gorush.StatusApp) *Storage { + return &Storage{ + stat: stat, + } +} + +type Storage struct { + stat gorush.StatusApp +} + +func (s *Storage) addTotalCount(count int64) { + atomic.AddInt64(&s.stat.TotalCount, count) +} + +func (s *Storage) addIosSuccess(count int64) { + atomic.AddInt64(&s.stat.Ios.PushSuccess, count) +} + +func (s *Storage) addIosError(count int64) { + atomic.AddInt64(&s.stat.Ios.PushError, count) +} + +func (s *Storage) addAndroidSuccess(count int64) { + atomic.AddInt64(&s.stat.Android.PushSuccess, count) +} + +func (s *Storage) addAndroidError(count int64) { + atomic.AddInt64(&s.stat.Android.PushError, count) +} + +func (s *Storage) getTotalCount() int64 { + count := atomic.LoadInt64(&s.stat.TotalCount) + + return count +} + +func (s *Storage) getIosSuccess() int64 { + count := atomic.LoadInt64(&s.stat.Ios.PushSuccess) + + return count +} + +func (s *Storage) getIosError() int64 { + count := atomic.LoadInt64(&s.stat.Ios.PushError) + + return count +} + +func (s *Storage) getAndroidSuccess() int64 { + count := atomic.LoadInt64(&s.stat.Android.PushSuccess) + + return count +} + +func (s *Storage) getAndroidError() int64 { + count := atomic.LoadInt64(&s.stat.Android.PushError) + + return count +} diff --git a/storage/memory/memory_test.go b/storage/memory/memory_test.go new file mode 100644 index 0000000..54d670c --- /dev/null +++ b/storage/memory/memory_test.go @@ -0,0 +1,33 @@ +package memory + +import ( + "github.com/appleboy/gorush/gorush" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestMemoryEngine(t *testing.T) { + var val int64 + + memory := New(gorush.StatusApp{}) + + memory.addTotalCount(1) + val = memory.getTotalCount() + assert.Equal(t, int64(1), val) + + memory.addIosSuccess(2) + val = memory.getIosSuccess() + assert.Equal(t, int64(2), val) + + memory.addIosError(3) + val = memory.getIosError() + assert.Equal(t, int64(3), val) + + memory.addAndroidSuccess(4) + val = memory.getAndroidSuccess() + assert.Equal(t, int64(4), val) + + memory.addAndroidError(5) + val = memory.getAndroidError() + assert.Equal(t, int64(5), val) +}