From 1ccd81bbd00d0acdb9fe8318f5835f5ad7d30c3c Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 2 May 2016 12:34:35 +0800 Subject: [PATCH] fix wrong stat count for redis engine. Signed-off-by: Bo-Yi Wu --- storage/redis/redis.go | 23 ++++++++++++++++++----- storage/redis/redis_test.go | 24 ++++++++++++++---------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/storage/redis/redis.go b/storage/redis/redis.go index d17343a..01d3c61 100644 --- a/storage/redis/redis.go +++ b/storage/redis/redis.go @@ -52,24 +52,37 @@ func (s *Storage) initRedis() error { return nil } +func (s *Storage) resetRedis() { + RedisClient.Set(gorush.TotalCountKey, strconv.Itoa(0), 0) + RedisClient.Set(gorush.IosSuccessKey, strconv.Itoa(0), 0) + RedisClient.Set(gorush.IosErrorKey, strconv.Itoa(0), 0) + RedisClient.Set(gorush.AndroidSuccessKey, strconv.Itoa(0), 0) + RedisClient.Set(gorush.AndroidErrorKey, strconv.Itoa(0), 0) +} + func (s *Storage) addTotalCount(count int64) { - RedisClient.Set(gorush.TotalCountKey, strconv.Itoa(int(count)), 0) + total := s.getTotalCount() + count + RedisClient.Set(gorush.TotalCountKey, strconv.Itoa(int(total)), 0) } func (s *Storage) addIosSuccess(count int64) { - RedisClient.Set(gorush.IosSuccessKey, strconv.Itoa(int(count)), 0) + total := s.getIosSuccess() + count + RedisClient.Set(gorush.IosSuccessKey, strconv.Itoa(int(total)), 0) } func (s *Storage) addIosError(count int64) { - RedisClient.Set(gorush.IosErrorKey, strconv.Itoa(int(count)), 0) + total := s.getIosError() + count + RedisClient.Set(gorush.IosErrorKey, strconv.Itoa(int(total)), 0) } func (s *Storage) addAndroidSuccess(count int64) { - RedisClient.Set(gorush.AndroidSuccessKey, strconv.Itoa(int(count)), 0) + total := s.getAndroidSuccess() + count + RedisClient.Set(gorush.AndroidSuccessKey, strconv.Itoa(int(total)), 0) } func (s *Storage) addAndroidError(count int64) { - RedisClient.Set(gorush.AndroidErrorKey, strconv.Itoa(int(count)), 0) + total := s.getAndroidError() + count + RedisClient.Set(gorush.AndroidErrorKey, strconv.Itoa(int(total)), 0) } func (s *Storage) getTotalCount() int64 { diff --git a/storage/redis/redis_test.go b/storage/redis/redis_test.go index 1cb7e32..83abe66 100644 --- a/storage/redis/redis_test.go +++ b/storage/redis/redis_test.go @@ -23,24 +23,28 @@ func TestRedisEngine(t *testing.T) { redis := New(config, gorush.StatusApp{}) redis.initRedis() + redis.resetRedis() - redis.addTotalCount(1) + redis.addTotalCount(10) val = redis.getTotalCount() - assert.Equal(t, int64(1), val) + assert.Equal(t, int64(10), val) + redis.addTotalCount(10) + val = redis.getTotalCount() + assert.Equal(t, int64(20), val) - redis.addIosSuccess(2) + redis.addIosSuccess(20) val = redis.getIosSuccess() - assert.Equal(t, int64(2), val) + assert.Equal(t, int64(20), val) - redis.addIosError(3) + redis.addIosError(30) val = redis.getIosError() - assert.Equal(t, int64(3), val) + assert.Equal(t, int64(30), val) - redis.addAndroidSuccess(4) + redis.addAndroidSuccess(40) val = redis.getAndroidSuccess() - assert.Equal(t, int64(4), val) + assert.Equal(t, int64(40), val) - redis.addAndroidError(5) + redis.addAndroidError(50) val = redis.getAndroidError() - assert.Equal(t, int64(5), val) + assert.Equal(t, int64(50), val) }