Fixed #66 Support memory interface.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-05-02 11:04:19 +08:00
parent 36dc3335f8
commit 077f836d1c
3 changed files with 115 additions and 0 deletions

15
gorush/storage.go Normal file
View File

@ -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
}

67
storage/memory/memory.go Normal file
View File

@ -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
}

View File

@ -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)
}