ledisdb/server/cmd_ttl_test.go

82 lines
1.4 KiB
Go
Raw Normal View History

2014-06-16 15:24:37 +04:00
package server
import (
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()
}
func TestKVExpire(t *testing.T) {
c := getTestConn()
defer c.Close()
k := "a_ttl"
c.Do("set", k, "123")
// expire + ttl
exp := int64(10)
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("expire", k, exp)); err != nil {
2014-06-16 15:24:37 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if ttl, err := ledis.Int64(c.Do("ttl", k)); err != nil {
2014-06-16 15:24:37 +04:00
t.Fatal(err)
} else if ttl != exp {
t.Fatal(ttl)
}
// expireat + ttl
tm := now() + 3
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("expireat", k, tm)); err != nil {
2014-06-16 15:24:37 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if ttl, err := ledis.Int64(c.Do("ttl", k)); err != nil {
2014-06-16 15:24:37 +04:00
t.Fatal(err)
} else if ttl != 3 {
t.Fatal(ttl)
}
kErr := "not_exist_ttl"
// err - expire, expireat
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("expire", kErr, tm)); err != nil || n != 0 {
2014-06-16 15:24:37 +04:00
t.Fatal(false)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("expireat", kErr, tm)); err != nil || n != 0 {
2014-06-16 15:24:37 +04:00
t.Fatal(false)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("ttl", kErr)); err != nil || n != -1 {
2014-06-16 15:24:37 +04:00
t.Fatal(false)
}
2014-06-24 08:44:44 +04:00
if n, err := ledis.Int(c.Do("persist", k)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("expire", k, 10)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if n, err := ledis.Int(c.Do("persist", k)); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-16 15:24:37 +04:00
}