ledisdb/server/cmd_ttl_test.go

130 lines
2.3 KiB
Go
Raw Normal View History

2014-06-16 15:24:37 +04:00
package server
import (
2014-08-28 13:00:37 +04:00
"fmt"
2014-06-22 06:39:23 +04:00
"github.com/siddontang/ledisdb/client/go/ledis"
2014-06-16 15:24:37 +04:00
"testing"
"time"
)
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
ttlType := []string{"k", "l", "h", "s", "z", "b"}
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)
if n, err := ledis.Int(c.Do(expire, key, exp)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if ttl, err := ledis.Int64(c.Do(ttl, key)); err != nil {
t.Fatal(err)
} else if ttl != exp {
t.Fatal(ttl)
}
// expireat + ttl
tm := now() + 3
if n, err := ledis.Int(c.Do(expireat, key, tm)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if ttl, err := ledis.Int64(c.Do(ttl, key)); err != nil {
t.Fatal(err)
} else if ttl != 3 {
t.Fatal(ttl)
}
kErr := "not_exist_ttl"
// err - expire, expireat
if n, err := ledis.Int(c.Do(expire, kErr, tm)); err != nil || n != 0 {
t.Fatal(false)
}
if n, err := ledis.Int(c.Do(expireat, kErr, tm)); err != nil || n != 0 {
t.Fatal(false)
}
if n, err := ledis.Int(c.Do(ttl, kErr)); err != nil || n != -1 {
t.Fatal(false)
}
if n, err := ledis.Int(c.Do(persist, key)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do(expire, key, 10)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do(persist, key)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
idx++
2014-08-15 11:51:41 +04:00
}
}