2016-05-02 04:06:08 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2020-02-29 09:14:44 +00:00
|
|
|
"sync"
|
2017-01-19 09:08:12 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
"github.com/appleboy/gorush/storage"
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2016-05-02 04:06:08 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRedisServerError(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
|
|
|
cfg.Stat.Redis.Addr = "redis:6370"
|
2016-05-02 04:06:08 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
redis := New(cfg)
|
2016-05-02 11:42:21 +00:00
|
|
|
err := redis.Init()
|
2016-05-02 04:06:08 +00:00
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedisEngine(t *testing.T) {
|
|
|
|
var val int64
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
|
|
|
cfg.Stat.Redis.Addr = "redis:6379"
|
2016-05-02 04:06:08 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
redis := New(cfg)
|
2017-08-24 03:25:05 +00:00
|
|
|
err := redis.Init()
|
|
|
|
assert.Nil(t, err)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
redis.Add(storage.HuaweiSuccessKey, 10)
|
|
|
|
val = redis.Get(storage.HuaweiSuccessKey)
|
2016-05-02 04:34:35 +00:00
|
|
|
assert.Equal(t, int64(10), val)
|
2022-02-03 09:03:37 +00:00
|
|
|
redis.Add(storage.HuaweiSuccessKey, 10)
|
|
|
|
val = redis.Get(storage.HuaweiSuccessKey)
|
2016-05-02 04:34:35 +00:00
|
|
|
assert.Equal(t, int64(20), val)
|
2016-05-02 04:06:08 +00:00
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
redis.Set(storage.HuaweiSuccessKey, 0)
|
|
|
|
val = redis.Get(storage.HuaweiSuccessKey)
|
2016-08-03 06:30:21 +00:00
|
|
|
assert.Equal(t, int64(0), val)
|
2020-02-29 09:14:44 +00:00
|
|
|
|
|
|
|
// test concurrency issues
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2022-02-03 09:03:37 +00:00
|
|
|
redis.Add(storage.HuaweiSuccessKey, 1)
|
2020-02-29 09:14:44 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2022-02-03 09:03:37 +00:00
|
|
|
val = redis.Get(storage.HuaweiSuccessKey)
|
2020-02-29 09:14:44 +00:00
|
|
|
assert.Equal(t, int64(10), val)
|
2020-04-23 07:39:24 +00:00
|
|
|
|
|
|
|
assert.NoError(t, redis.Close())
|
2016-05-02 04:06:08 +00:00
|
|
|
}
|