github.com/godaddy-x/freego@v1.0.156/redis_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/garyburd/redigo/redis" 6 "github.com/godaddy-x/freego/cache" 7 "github.com/godaddy-x/freego/utils" 8 "reflect" 9 "testing" 10 "time" 11 ) 12 13 var subkey = "test.subkey" 14 15 func init() { 16 initRedis() 17 } 18 19 func expectPushed(t *testing.T, c redis.PubSubConn, message string, expected interface{}) { 20 actual := c.Receive() 21 if !reflect.DeepEqual(actual, expected) { 22 t.Errorf("%s = %v, want %v", message, actual, expected) 23 } 24 } 25 26 func TestRedisPublish(t *testing.T) { 27 mgr, err := cache.NewRedis() 28 if err != nil { 29 panic(err) 30 } 31 b, err := mgr.Publish("test_123456_uid", "uid_orderNo_success") 32 if err != nil { 33 panic(err) 34 } 35 fmt.Println("send success: ", b) 36 } 37 38 func TestRedisSubscribe(t *testing.T) { 39 mgr, err := cache.NewRedis() 40 if err != nil { 41 panic(err) 42 } 43 mgr.Subscribe("test_123456_uid", 5, func(msg string) (bool, error) { 44 if err != nil { 45 fmt.Println("read error: ", err) 46 return false, err 47 } 48 fmt.Println("read msg:", msg) 49 return true, nil 50 }) 51 } 52 53 func TestRedisSubscribe2(t *testing.T) { 54 mgr, err := new(cache.RedisManager).Client() 55 if err != nil { 56 panic(err) 57 } 58 mgr.Subscribe(subkey, 0, func(msg string) (bool, error) { 59 fmt.Println("subscribe:", msg) 60 return false, nil 61 }) 62 } 63 64 func TestRedisSpinLocker(t *testing.T) { 65 mgr, err := new(cache.RedisManager).Client() 66 if err != nil { 67 panic(err) 68 } 69 if err := mgr.SpinLockWithTimeout("spinlock", 20, 20, func() error { 70 fmt.Println("test1 spin lock successful") 71 time.Sleep(15 * time.Second) 72 return nil 73 }); err != nil { 74 fmt.Println(err) 75 } 76 } 77 78 func TestRedisSpinLocker2(t *testing.T) { 79 mgr, err := new(cache.RedisManager).Client() 80 if err != nil { 81 panic(err) 82 } 83 if err := mgr.SpinLockWithTimeout("spinlock", 20, 20, func() error { 84 fmt.Println("test2 spin lock successful") 85 return nil 86 }); err != nil { 87 fmt.Println(err) 88 } 89 } 90 91 func TestRedisSpinLocker3(t *testing.T) { 92 mgr, err := new(cache.RedisManager).Client() 93 if err != nil { 94 panic(err) 95 } 96 if err := mgr.SpinLockWithTimeout("spinlock", 20, 20, func() error { 97 fmt.Println("test3 spin lock successful") 98 return nil 99 }); err != nil { 100 fmt.Println(err) 101 } 102 } 103 104 func TestRedisTryLocker1(t *testing.T) { 105 mgr, err := new(cache.RedisManager).Client() 106 if err != nil { 107 panic(err) 108 } 109 if err := mgr.TryLockWithTimeout("trylock", 20, func() error { 110 fmt.Println("test1 try lock successful") 111 time.Sleep(15 * time.Second) 112 return nil 113 }); err != nil { 114 fmt.Println(err) 115 } 116 } 117 118 func TestRedisTryLocker2(t *testing.T) { 119 mgr, err := new(cache.RedisManager).Client() 120 if err != nil { 121 panic(err) 122 } 123 if err := mgr.TryLockWithTimeout("trylock", 20, func() error { 124 fmt.Println("test2 try lock successful") 125 return nil 126 }); err != nil { 127 fmt.Println(err) 128 } 129 } 130 131 func TestRedisGetAndSet(t *testing.T) { 132 rds, err := cache.NewRedis() 133 if err != nil { 134 panic(err) 135 } 136 key := utils.MD5("123456") 137 if err := rds.Put(key, 1, 30); err != nil { 138 panic(err) 139 } 140 value, err := rds.Exists(key + "111") 141 if err != nil { 142 panic(err) 143 } 144 fmt.Println("---", value) 145 } 146 147 func TestLocalCacheGetAndSet(t *testing.T) { 148 rds := cache.NewLocalCache(10, 10) 149 key := utils.MD5("123456") 150 if err := rds.Put(key, 1, 30); err != nil { 151 panic(err) 152 } 153 value, err := rds.Exists(key) 154 if err != nil { 155 panic(err) 156 } 157 fmt.Println("---", value) 158 } 159 160 func BenchmarkRedisGetAndSet(b *testing.B) { 161 b.StopTimer() 162 b.StartTimer() 163 rds, err := cache.NewRedis() 164 if err != nil { 165 panic(err) 166 } 167 for i := 0; i < b.N; i++ { //use b.N for looping 168 key := utils.MD5(utils.NextSID()) 169 if err := rds.Put(key, 1, 30); err != nil { 170 panic(err) 171 } 172 _, err := rds.Exists(key) 173 if err != nil { 174 panic(err) 175 } 176 } 177 } 178 179 func BenchmarkLocalCacheGetAndSet(b *testing.B) { 180 b.StopTimer() 181 b.StartTimer() 182 rds := cache.NewLocalCache(10, 10) 183 for i := 0; i < b.N; i++ { //use b.N for looping 184 key := utils.MD5(utils.NextSID()) 185 if err := rds.Put(key, 1, 30); err != nil { 186 panic(err) 187 } 188 _, err := rds.Exists(key) 189 if err != nil { 190 panic(err) 191 } 192 } 193 }