2014-06-16 15:24:37 +04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2014-08-28 13:00:37 +04:00
|
|
|
"fmt"
|
2014-06-16 15:24:37 +04:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-05-04 17:42:28 +03:00
|
|
|
|
|
|
|
"github.com/siddontang/goredis"
|
2014-06-16 15:24:37 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func now() int64 {
|
|
|
|
return time.Now().Unix()
|
|
|
|
}
|
|
|
|
|
2014-08-28 13:00:37 +04:00
|
|
|
func TestExpire(t *testing.T) {
|
|
|
|
// test for kv, list, hash, set, zset, bitmap in all
|
2015-02-05 10:37:17 +03:00
|
|
|
ttlType := []string{"k", "l", "h", "s", "z"}
|
2014-06-16 15:24:37 +04:00
|
|
|
|
2014-08-28 13:00:37 +04:00
|
|
|
var (
|
|
|
|
expire string
|
|
|
|
expireat string
|
|
|
|
ttl string
|
|
|
|
persist string
|
|
|
|
key string
|
|
|
|
)
|
2014-06-16 15:24:37 +04:00
|
|
|
|
2014-08-15 11:51:41 +04:00
|
|
|
c := getTestConn()
|
|
|
|
defer c.Close()
|
|
|
|
|
2014-08-28 13:00:37 +04:00
|
|
|
idx := 1
|
|
|
|
for _, tt := range ttlType {
|
|
|
|
if tt == "k" {
|
|
|
|
expire = "expire"
|
|
|
|
expireat = "expireat"
|
|
|
|
ttl = "ttl"
|
|
|
|
persist = "persist"
|
|
|
|
|
|
|
|
} else {
|
|
|
|
expire = fmt.Sprintf("%sexpire", tt)
|
|
|
|
expireat = fmt.Sprintf("%sexpireat", tt)
|
|
|
|
ttl = fmt.Sprintf("%sttl", tt)
|
|
|
|
persist = fmt.Sprintf("%spersist", tt)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tt {
|
|
|
|
case "k":
|
|
|
|
key = "kv_ttl"
|
|
|
|
c.Do("set", key, "123")
|
|
|
|
case "l":
|
|
|
|
key = "list_ttl"
|
|
|
|
c.Do("rpush", key, "123")
|
|
|
|
case "h":
|
|
|
|
key = "hash_ttl"
|
|
|
|
c.Do("hset", key, "a", "123")
|
|
|
|
case "s":
|
|
|
|
key = "set_ttl"
|
|
|
|
c.Do("sadd", key, "123")
|
|
|
|
case "z":
|
|
|
|
key = "zset_ttl"
|
|
|
|
c.Do("zadd", key, 123, "a")
|
|
|
|
case "b":
|
|
|
|
key = "bitmap_ttl"
|
|
|
|
c.Do("bsetbit", key, 0, 1)
|
|
|
|
}
|
|
|
|
// expire + ttl
|
|
|
|
exp := int64(10)
|
2015-03-11 06:54:02 +03:00
|
|
|
if n, err := goredis.Int(c.Do(expire, key, exp)); err != nil {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if n != 1 {
|
|
|
|
t.Fatal(n)
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
if ttl, err := goredis.Int64(c.Do(ttl, key)); err != nil {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(err)
|
2014-11-01 18:28:28 +03:00
|
|
|
} else if ttl == -1 {
|
|
|
|
t.Fatal("no ttl")
|
2014-08-28 13:00:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// expireat + ttl
|
|
|
|
tm := now() + 3
|
2015-03-11 06:54:02 +03:00
|
|
|
if n, err := goredis.Int(c.Do(expireat, key, tm)); err != nil {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if n != 1 {
|
|
|
|
t.Fatal(n)
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
if ttl, err := goredis.Int64(c.Do(ttl, key)); err != nil {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(err)
|
2014-11-01 18:28:28 +03:00
|
|
|
} else if ttl == -1 {
|
|
|
|
t.Fatal("no ttl")
|
2014-08-28 13:00:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
kErr := "not_exist_ttl"
|
|
|
|
|
|
|
|
// err - expire, expireat
|
2015-03-11 06:54:02 +03:00
|
|
|
if n, err := goredis.Int(c.Do(expire, kErr, tm)); err != nil || n != 0 {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(false)
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
if n, err := goredis.Int(c.Do(expireat, kErr, tm)); err != nil || n != 0 {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(false)
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
if n, err := goredis.Int(c.Do(ttl, kErr)); err != nil || n != -1 {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(false)
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
if n, err := goredis.Int(c.Do(persist, key)); err != nil {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if n != 1 {
|
|
|
|
t.Fatal(n)
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
if n, err := goredis.Int(c.Do(expire, key, 10)); err != nil {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if n != 1 {
|
|
|
|
t.Fatal(n)
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
if n, err := goredis.Int(c.Do(persist, key)); err != nil {
|
2014-08-28 13:00:37 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if n != 1 {
|
|
|
|
t.Fatal(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx++
|
2014-08-15 11:51:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|