2012-01-02 02:01:04 -08:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2012-01-28 18:16:59 -08:00
|
|
|
"bytes"
|
2012-02-11 15:15:25 -08:00
|
|
|
"io/ioutil"
|
2012-06-21 19:06:06 -07:00
|
|
|
"runtime"
|
2012-06-22 01:24:09 -07:00
|
|
|
"strconv"
|
2012-02-21 09:46:25 -08:00
|
|
|
"sync"
|
2012-01-02 02:01:04 -08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2012-01-28 19:35:47 -08:00
|
|
|
type TestStruct struct {
|
2012-02-11 16:54:16 -08:00
|
|
|
Num int
|
2012-01-28 19:35:47 -08:00
|
|
|
Children []*TestStruct
|
|
|
|
}
|
|
|
|
|
2012-01-02 02:01:04 -08:00
|
|
|
func TestCache(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
2012-01-02 02:01:04 -08:00
|
|
|
|
|
|
|
a, found := tc.Get("a")
|
|
|
|
if found || a != nil {
|
|
|
|
t.Error("Getting A found value that shouldn't exist:", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, found := tc.Get("b")
|
|
|
|
if found || b != nil {
|
|
|
|
t.Error("Getting B found value that shouldn't exist:", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, found := tc.Get("c")
|
|
|
|
if found || c != nil {
|
|
|
|
t.Error("Getting C found value that shouldn't exist:", c)
|
|
|
|
}
|
|
|
|
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("a", 1, DefaultExpiration)
|
|
|
|
tc.Set("b", "b", DefaultExpiration)
|
|
|
|
tc.Set("c", 3.5, DefaultExpiration)
|
2012-01-02 02:01:04 -08:00
|
|
|
|
|
|
|
x, found := tc.Get("a")
|
|
|
|
if !found {
|
|
|
|
t.Error("a was not found while getting a2")
|
|
|
|
}
|
|
|
|
if x == nil {
|
|
|
|
t.Error("x for a is nil")
|
2012-01-02 05:11:17 -08:00
|
|
|
} else if a2 := x.(int); a2+2 != 3 {
|
2012-01-02 02:01:04 -08:00
|
|
|
t.Error("a2 (which should be 1) plus 2 does not equal 3; value:", a2)
|
|
|
|
}
|
|
|
|
|
|
|
|
x, found = tc.Get("b")
|
|
|
|
if !found {
|
|
|
|
t.Error("b was not found while getting b2")
|
|
|
|
}
|
|
|
|
if x == nil {
|
|
|
|
t.Error("x for b is nil")
|
2012-01-02 05:11:17 -08:00
|
|
|
} else if b2 := x.(string); b2+"B" != "bB" {
|
2012-01-02 02:01:04 -08:00
|
|
|
t.Error("b2 (which should be b) plus B does not equal bB; value:", b2)
|
|
|
|
}
|
|
|
|
|
|
|
|
x, found = tc.Get("c")
|
|
|
|
if !found {
|
|
|
|
t.Error("c was not found while getting c2")
|
|
|
|
}
|
|
|
|
if x == nil {
|
|
|
|
t.Error("x for c is nil")
|
2012-01-02 05:11:17 -08:00
|
|
|
} else if c2 := x.(float64); c2+1.2 != 4.7 {
|
2012-01-02 02:01:04 -08:00
|
|
|
t.Error("c2 (which should be 3.5) plus 1.2 does not equal 4.7; value:", c2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheTimes(t *testing.T) {
|
|
|
|
var found bool
|
|
|
|
|
2012-01-04 00:11:27 -08:00
|
|
|
tc := New(50*time.Millisecond, 1*time.Millisecond)
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("a", 1, DefaultExpiration)
|
|
|
|
tc.Set("b", 2, NoExpiration)
|
2012-01-04 00:11:27 -08:00
|
|
|
tc.Set("c", 3, 20*time.Millisecond)
|
|
|
|
tc.Set("d", 4, 70*time.Millisecond)
|
2012-01-02 02:01:04 -08:00
|
|
|
|
2012-01-04 00:11:27 -08:00
|
|
|
<-time.After(25 * time.Millisecond)
|
2012-01-02 02:01:04 -08:00
|
|
|
_, found = tc.Get("c")
|
|
|
|
if found {
|
|
|
|
t.Error("Found c when it should have been automatically deleted")
|
|
|
|
}
|
|
|
|
|
2012-01-04 00:11:27 -08:00
|
|
|
<-time.After(30 * time.Millisecond)
|
2012-01-02 02:01:04 -08:00
|
|
|
_, found = tc.Get("a")
|
|
|
|
if found {
|
|
|
|
t.Error("Found a when it should have been automatically deleted")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, found = tc.Get("b")
|
|
|
|
if !found {
|
|
|
|
t.Error("Did not find b even though it was set to never expire")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, found = tc.Get("d")
|
|
|
|
if !found {
|
|
|
|
t.Error("Did not find d even though it was set to expire later than the default")
|
|
|
|
}
|
|
|
|
|
2012-01-04 00:11:27 -08:00
|
|
|
<-time.After(20 * time.Millisecond)
|
2012-01-02 02:01:04 -08:00
|
|
|
_, found = tc.Get("d")
|
|
|
|
if found {
|
|
|
|
t.Error("Found d when it should have been automatically deleted (later than the default)")
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 02:32:05 -08:00
|
|
|
|
2014-12-21 22:12:10 -08:00
|
|
|
func TestNewFrom(t *testing.T) {
|
2015-11-28 11:47:46 -08:00
|
|
|
m := map[string]Item{
|
|
|
|
"a": Item{
|
2014-12-21 23:48:52 -08:00
|
|
|
Object: 1,
|
2015-11-30 10:54:01 -08:00
|
|
|
Expiration: 0,
|
2014-12-21 22:12:10 -08:00
|
|
|
},
|
2015-11-28 11:47:46 -08:00
|
|
|
"b": Item{
|
2014-12-21 23:48:52 -08:00
|
|
|
Object: 2,
|
2015-11-30 10:54:01 -08:00
|
|
|
Expiration: 0,
|
2014-12-21 22:12:10 -08:00
|
|
|
},
|
|
|
|
}
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := NewFrom(DefaultExpiration, 0, m)
|
2014-12-21 22:12:10 -08:00
|
|
|
a, found := tc.Get("a")
|
|
|
|
if !found {
|
|
|
|
t.Fatal("Did not find a")
|
|
|
|
}
|
|
|
|
if a.(int) != 1 {
|
|
|
|
t.Fatal("a is not 1")
|
|
|
|
}
|
|
|
|
b, found := tc.Get("b")
|
|
|
|
if !found {
|
|
|
|
t.Fatal("Did not find b")
|
|
|
|
}
|
|
|
|
if b.(int) != 2 {
|
|
|
|
t.Fatal("b is not 2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-02 02:32:05 -08:00
|
|
|
func TestStorePointerToStruct(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("foo", &TestStruct{Num: 1}, DefaultExpiration)
|
2012-01-02 02:32:05 -08:00
|
|
|
x, found := tc.Get("foo")
|
|
|
|
if !found {
|
|
|
|
t.Fatal("*TestStruct was not found for foo")
|
|
|
|
}
|
|
|
|
foo := x.(*TestStruct)
|
|
|
|
foo.Num++
|
|
|
|
|
|
|
|
y, found := tc.Get("foo")
|
|
|
|
if !found {
|
|
|
|
t.Fatal("*TestStruct was not found for foo (second time)")
|
|
|
|
}
|
|
|
|
bar := y.(*TestStruct)
|
|
|
|
if bar.Num != 2 {
|
|
|
|
t.Fatal("TestStruct.Num is not 2")
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 04:52:43 -08:00
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementWithInt(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint", 1, DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Increment("tint", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint was not found")
|
|
|
|
}
|
|
|
|
if x.(int) != 3 {
|
|
|
|
t.Error("tint is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementWithInt8(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint8", int8(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Increment("tint8", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint8")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint8 was not found")
|
|
|
|
}
|
|
|
|
if x.(int8) != 3 {
|
|
|
|
t.Error("tint8 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementWithInt16(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint16", int16(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Increment("tint16", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint16")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint16 was not found")
|
|
|
|
}
|
|
|
|
if x.(int16) != 3 {
|
|
|
|
t.Error("tint16 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementWithInt32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint32", int32(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Increment("tint32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint32")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint32 was not found")
|
|
|
|
}
|
|
|
|
if x.(int32) != 3 {
|
|
|
|
t.Error("tint32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementWithInt64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint64", int64(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Increment("tint64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint64")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint64 was not found")
|
|
|
|
}
|
|
|
|
if x.(int64) != 3 {
|
|
|
|
t.Error("tint64 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementWithUint(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint", uint(1), DefaultExpiration)
|
2012-01-02 04:52:43 -08:00
|
|
|
err := tc.Increment("tuint", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tuint")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint was not found")
|
|
|
|
}
|
|
|
|
if x.(uint) != 3 {
|
|
|
|
t.Error("tuint is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementWithUintptr(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuintptr", uintptr(1), DefaultExpiration)
|
2012-01-02 04:52:43 -08:00
|
|
|
err := tc.Increment("tuintptr", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x, found := tc.Get("tuintptr")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuintptr was not found")
|
|
|
|
}
|
|
|
|
if x.(uintptr) != 3 {
|
|
|
|
t.Error("tuintptr is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementWithUint8(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint8", uint8(1), DefaultExpiration)
|
2012-01-02 04:52:43 -08:00
|
|
|
err := tc.Increment("tuint8", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tuint8")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint8 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint8) != 3 {
|
|
|
|
t.Error("tuint8 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementWithUint16(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint16", uint16(1), DefaultExpiration)
|
2012-01-02 04:52:43 -08:00
|
|
|
err := tc.Increment("tuint16", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x, found := tc.Get("tuint16")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint16 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint16) != 3 {
|
|
|
|
t.Error("tuint16 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementWithUint32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint32", uint32(1), DefaultExpiration)
|
2012-01-02 04:52:43 -08:00
|
|
|
err := tc.Increment("tuint32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tuint32")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint32 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint32) != 3 {
|
|
|
|
t.Error("tuint32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementWithUint64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint64", uint64(1), DefaultExpiration)
|
2012-01-02 04:52:43 -08:00
|
|
|
err := tc.Increment("tuint64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x, found := tc.Get("tuint64")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint64 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint64) != 3 {
|
|
|
|
t.Error("tuint64 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementWithFloat32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float32", float32(1.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Increment("float32", 2)
|
2012-01-02 04:52:43 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
x, found := tc.Get("float32")
|
2012-01-02 04:52:43 -08:00
|
|
|
if !found {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("float32 was not found")
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
if x.(float32) != 3.5 {
|
|
|
|
t.Error("float32 is not 3.5:", x)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementWithFloat64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float64", float64(1.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Increment("float64", 2)
|
2012-01-02 04:52:43 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
x, found := tc.Get("float64")
|
2012-01-02 04:52:43 -08:00
|
|
|
if !found {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("float64 was not found")
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
if x.(float64) != 3.5 {
|
|
|
|
t.Error("float64 is not 3.5:", x)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementFloatWithFloat32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float32", float32(1.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.IncrementFloat("float32", 2)
|
2012-01-02 04:52:43 -08:00
|
|
|
if err != nil {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("Error incrementfloating:", err)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
x, found := tc.Get("float32")
|
2012-01-02 04:52:43 -08:00
|
|
|
if !found {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("float32 was not found")
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
if x.(float32) != 3.5 {
|
|
|
|
t.Error("float32 is not 3.5:", x)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestIncrementFloatWithFloat64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float64", float64(1.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.IncrementFloat("float64", 2)
|
2012-01-02 04:52:43 -08:00
|
|
|
if err != nil {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("Error incrementfloating:", err)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
x, found := tc.Get("float64")
|
2012-01-02 04:52:43 -08:00
|
|
|
if !found {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("float64 was not found")
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
if x.(float64) != 3.5 {
|
|
|
|
t.Error("float64 is not 3.5:", x)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestDecrementWithInt(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int", int(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("int", 2)
|
2012-01-02 04:52:43 -08:00
|
|
|
if err != nil {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("Error decrementing:", err)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
x, found := tc.Get("int")
|
2012-01-02 04:52:43 -08:00
|
|
|
if !found {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("int was not found")
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
if x.(int) != 3 {
|
|
|
|
t.Error("int is not 3:", x)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestDecrementWithInt8(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int8", int8(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("int8", 2)
|
2012-01-02 04:52:43 -08:00
|
|
|
if err != nil {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("Error decrementing:", err)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
x, found := tc.Get("int8")
|
2012-01-02 04:52:43 -08:00
|
|
|
if !found {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("int8 was not found")
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
if x.(int8) != 3 {
|
|
|
|
t.Error("int8 is not 3:", x)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestDecrementWithInt16(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int16", int16(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("int16", 2)
|
2012-01-02 04:52:43 -08:00
|
|
|
if err != nil {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("Error decrementing:", err)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
x, found := tc.Get("int16")
|
2012-01-02 04:52:43 -08:00
|
|
|
if !found {
|
2013-04-18 11:24:30 -07:00
|
|
|
t.Error("int16 was not found")
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
2013-04-18 11:24:30 -07:00
|
|
|
if x.(int16) != 3 {
|
|
|
|
t.Error("int16 is not 3:", x)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:24:30 -07:00
|
|
|
func TestDecrementWithInt32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int32", int32(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("int32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("int32")
|
|
|
|
if !found {
|
|
|
|
t.Error("int32 was not found")
|
|
|
|
}
|
|
|
|
if x.(int32) != 3 {
|
|
|
|
t.Error("int32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementWithInt64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int64", int64(5), DefaultExpiration)
|
2012-01-02 04:52:43 -08:00
|
|
|
err := tc.Decrement("int64", 2)
|
|
|
|
if err != nil {
|
2012-01-02 08:26:17 -08:00
|
|
|
t.Error("Error decrementing:", err)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
|
|
|
x, found := tc.Get("int64")
|
|
|
|
if !found {
|
|
|
|
t.Error("int64 was not found")
|
|
|
|
}
|
|
|
|
if x.(int64) != 3 {
|
|
|
|
t.Error("int64 is not 3:", x)
|
2013-04-18 11:24:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementWithUint(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint", uint(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("uint", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint was not found")
|
|
|
|
}
|
|
|
|
if x.(uint) != 3 {
|
|
|
|
t.Error("uint is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementWithUintptr(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uintptr", uintptr(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("uintptr", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uintptr")
|
|
|
|
if !found {
|
|
|
|
t.Error("uintptr was not found")
|
|
|
|
}
|
|
|
|
if x.(uintptr) != 3 {
|
|
|
|
t.Error("uintptr is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementWithUint8(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint8", uint8(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("uint8", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint8")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint8 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint8) != 3 {
|
|
|
|
t.Error("uint8 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementWithUint16(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint16", uint16(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("uint16", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint16")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint16 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint16) != 3 {
|
|
|
|
t.Error("uint16 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementWithUint32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint32", uint32(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("uint32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint32")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint32 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint32) != 3 {
|
|
|
|
t.Error("uint32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementWithUint64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint64", uint64(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("uint64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint64")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint64 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint64) != 3 {
|
|
|
|
t.Error("uint64 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementWithFloat32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float32", float32(5.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("float32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("float32")
|
|
|
|
if !found {
|
|
|
|
t.Error("float32 was not found")
|
|
|
|
}
|
|
|
|
if x.(float32) != 3.5 {
|
|
|
|
t.Error("float32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementWithFloat64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float64", float64(5.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.Decrement("float64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("float64")
|
|
|
|
if !found {
|
|
|
|
t.Error("float64 was not found")
|
|
|
|
}
|
|
|
|
if x.(float64) != 3.5 {
|
|
|
|
t.Error("float64 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementFloatWithFloat32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float32", float32(5.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.DecrementFloat("float32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("float32")
|
|
|
|
if !found {
|
|
|
|
t.Error("float32 was not found")
|
|
|
|
}
|
|
|
|
if x.(float32) != 3.5 {
|
|
|
|
t.Error("float32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementFloatWithFloat64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float64", float64(5.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
err := tc.DecrementFloat("float64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("float64")
|
|
|
|
if !found {
|
|
|
|
t.Error("float64 was not found")
|
|
|
|
}
|
|
|
|
if x.(float64) != 3.5 {
|
|
|
|
t.Error("float64 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementInt(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint", 1, DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementInt("tint", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint was not found")
|
|
|
|
}
|
|
|
|
if x.(int) != 3 {
|
|
|
|
t.Error("tint is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementInt8(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint8", int8(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementInt8("tint8", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint8")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint8 was not found")
|
|
|
|
}
|
|
|
|
if x.(int8) != 3 {
|
|
|
|
t.Error("tint8 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementInt16(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint16", int16(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementInt16("tint16", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint16")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint16 was not found")
|
|
|
|
}
|
|
|
|
if x.(int16) != 3 {
|
|
|
|
t.Error("tint16 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementInt32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint32", int32(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementInt32("tint32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint32")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint32 was not found")
|
|
|
|
}
|
|
|
|
if x.(int32) != 3 {
|
|
|
|
t.Error("tint32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementInt64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tint64", int64(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementInt64("tint64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tint64")
|
|
|
|
if !found {
|
|
|
|
t.Error("tint64 was not found")
|
|
|
|
}
|
|
|
|
if x.(int64) != 3 {
|
|
|
|
t.Error("tint64 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUint(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint", uint(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementUint("tuint", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tuint")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint was not found")
|
|
|
|
}
|
|
|
|
if x.(uint) != 3 {
|
|
|
|
t.Error("tuint is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUintptr(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuintptr", uintptr(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementUintptr("tuintptr", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tuintptr")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuintptr was not found")
|
|
|
|
}
|
|
|
|
if x.(uintptr) != 3 {
|
|
|
|
t.Error("tuintptr is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUint8(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint8", uint8(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementUint8("tuint8", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tuint8")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint8 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint8) != 3 {
|
|
|
|
t.Error("tuint8 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUint16(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint16", uint16(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementUint16("tuint16", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tuint16")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint16 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint16) != 3 {
|
|
|
|
t.Error("tuint16 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUint32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint32", uint32(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementUint32("tuint32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tuint32")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint32 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint32) != 3 {
|
|
|
|
t.Error("tuint32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUint64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("tuint64", uint64(1), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementUint64("tuint64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("tuint64")
|
|
|
|
if !found {
|
|
|
|
t.Error("tuint64 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint64) != 3 {
|
|
|
|
t.Error("tuint64 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementFloat32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float32", float32(1.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementFloat32("float32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3.5 {
|
|
|
|
t.Error("Returned number is not 3.5:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("float32")
|
|
|
|
if !found {
|
|
|
|
t.Error("float32 was not found")
|
|
|
|
}
|
|
|
|
if x.(float32) != 3.5 {
|
|
|
|
t.Error("float32 is not 3.5:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementFloat64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float64", float64(1.5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.IncrementFloat64("float64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3.5 {
|
|
|
|
t.Error("Returned number is not 3.5:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("float64")
|
|
|
|
if !found {
|
|
|
|
t.Error("float64 was not found")
|
|
|
|
}
|
|
|
|
if x.(float64) != 3.5 {
|
|
|
|
t.Error("float64 is not 3.5:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementInt8(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int8", int8(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementInt8("int8", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("int8")
|
|
|
|
if !found {
|
|
|
|
t.Error("int8 was not found")
|
|
|
|
}
|
|
|
|
if x.(int8) != 3 {
|
|
|
|
t.Error("int8 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementInt16(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int16", int16(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementInt16("int16", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("int16")
|
|
|
|
if !found {
|
|
|
|
t.Error("int16 was not found")
|
|
|
|
}
|
|
|
|
if x.(int16) != 3 {
|
|
|
|
t.Error("int16 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementInt32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int32", int32(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementInt32("int32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("int32")
|
|
|
|
if !found {
|
|
|
|
t.Error("int32 was not found")
|
|
|
|
}
|
|
|
|
if x.(int32) != 3 {
|
|
|
|
t.Error("int32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementInt64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int64", int64(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementInt64("int64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("int64")
|
|
|
|
if !found {
|
|
|
|
t.Error("int64 was not found")
|
|
|
|
}
|
|
|
|
if x.(int64) != 3 {
|
|
|
|
t.Error("int64 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementUint(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint", uint(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementUint("uint", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint was not found")
|
|
|
|
}
|
|
|
|
if x.(uint) != 3 {
|
|
|
|
t.Error("uint is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementUintptr(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uintptr", uintptr(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementUintptr("uintptr", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uintptr")
|
|
|
|
if !found {
|
|
|
|
t.Error("uintptr was not found")
|
|
|
|
}
|
|
|
|
if x.(uintptr) != 3 {
|
|
|
|
t.Error("uintptr is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementUint8(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint8", uint8(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementUint8("uint8", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint8")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint8 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint8) != 3 {
|
|
|
|
t.Error("uint8 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementUint16(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint16", uint16(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementUint16("uint16", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint16")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint16 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint16) != 3 {
|
|
|
|
t.Error("uint16 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementUint32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint32", uint32(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementUint32("uint32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint32")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint32 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint32) != 3 {
|
|
|
|
t.Error("uint32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementUint64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint64", uint64(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementUint64("uint64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("uint64")
|
|
|
|
if !found {
|
|
|
|
t.Error("uint64 was not found")
|
|
|
|
}
|
|
|
|
if x.(uint64) != 3 {
|
|
|
|
t.Error("uint64 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementFloat32(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float32", float32(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementFloat32("float32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("float32")
|
|
|
|
if !found {
|
|
|
|
t.Error("float32 was not found")
|
|
|
|
}
|
|
|
|
if x.(float32) != 3 {
|
|
|
|
t.Error("float32 is not 3:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementFloat64(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("float64", float64(5), DefaultExpiration)
|
2013-04-18 11:24:30 -07:00
|
|
|
n, err := tc.DecrementFloat64("float64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error decrementing:", err)
|
|
|
|
}
|
|
|
|
if n != 3 {
|
|
|
|
t.Error("Returned number is not 3:", n)
|
|
|
|
}
|
|
|
|
x, found := tc.Get("float64")
|
|
|
|
if !found {
|
|
|
|
t.Error("float64 was not found")
|
|
|
|
}
|
|
|
|
if x.(float64) != 3 {
|
|
|
|
t.Error("float64 is not 3:", x)
|
2012-01-02 04:52:43 -08:00
|
|
|
}
|
|
|
|
}
|
2012-01-02 05:04:47 -08:00
|
|
|
|
|
|
|
func TestAdd(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
err := tc.Add("foo", "bar", DefaultExpiration)
|
2012-01-02 05:04:47 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Couldn't add foo even though it shouldn't exist")
|
|
|
|
}
|
2014-12-21 22:46:22 -08:00
|
|
|
err = tc.Add("foo", "baz", DefaultExpiration)
|
2012-01-02 05:04:47 -08:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Successfully added another foo when it should have returned an error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplace(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
err := tc.Replace("foo", "bar", DefaultExpiration)
|
2012-01-02 05:04:47 -08:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Replaced foo when it shouldn't exist")
|
|
|
|
}
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("foo", "bar", DefaultExpiration)
|
|
|
|
err = tc.Replace("foo", "bar", DefaultExpiration)
|
2012-01-02 05:04:47 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Couldn't replace existing key foo")
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 05:18:25 -08:00
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("foo", "bar", DefaultExpiration)
|
2012-01-02 05:18:25 -08:00
|
|
|
tc.Delete("foo")
|
|
|
|
x, found := tc.Get("foo")
|
|
|
|
if found {
|
|
|
|
t.Error("foo was found, but it should have been deleted")
|
|
|
|
}
|
|
|
|
if x != nil {
|
|
|
|
t.Error("x is not nil:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 11:32:01 -07:00
|
|
|
func TestItemCount(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("foo", "1", DefaultExpiration)
|
|
|
|
tc.Set("bar", "2", DefaultExpiration)
|
|
|
|
tc.Set("baz", "3", DefaultExpiration)
|
2013-04-18 11:32:01 -07:00
|
|
|
if n := tc.ItemCount(); n != 3 {
|
|
|
|
t.Errorf("Item count is not 3: %d", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-02 05:18:25 -08:00
|
|
|
func TestFlush(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("foo", "bar", DefaultExpiration)
|
|
|
|
tc.Set("baz", "yes", DefaultExpiration)
|
2012-01-02 05:18:25 -08:00
|
|
|
tc.Flush()
|
|
|
|
x, found := tc.Get("foo")
|
|
|
|
if found {
|
|
|
|
t.Error("foo was found, but it should have been deleted")
|
|
|
|
}
|
|
|
|
if x != nil {
|
|
|
|
t.Error("x is not nil:", x)
|
|
|
|
}
|
|
|
|
x, found = tc.Get("baz")
|
|
|
|
if found {
|
|
|
|
t.Error("baz was found, but it should have been deleted")
|
|
|
|
}
|
|
|
|
if x != nil {
|
|
|
|
t.Error("x is not nil:", x)
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 00:55:32 -08:00
|
|
|
|
2012-01-04 01:48:21 -08:00
|
|
|
func TestIncrementOverflowInt(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("int8", int8(127), DefaultExpiration)
|
2012-01-04 01:48:21 -08:00
|
|
|
err := tc.Increment("int8", 1)
|
|
|
|
if err != nil {
|
2012-02-13 12:37:17 -08:00
|
|
|
t.Error("Error incrementing int8:", err)
|
2012-01-04 01:48:21 -08:00
|
|
|
}
|
|
|
|
x, _ := tc.Get("int8")
|
|
|
|
int8 := x.(int8)
|
|
|
|
if int8 != -128 {
|
2012-02-13 12:37:17 -08:00
|
|
|
t.Error("int8 did not overflow as expected; value:", int8)
|
2012-01-04 01:48:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementOverflowUint(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint8", uint8(255), DefaultExpiration)
|
2012-01-04 01:48:21 -08:00
|
|
|
err := tc.Increment("uint8", 1)
|
|
|
|
if err != nil {
|
2012-02-13 12:37:17 -08:00
|
|
|
t.Error("Error incrementing int8:", err)
|
2012-01-04 01:48:21 -08:00
|
|
|
}
|
|
|
|
x, _ := tc.Get("uint8")
|
|
|
|
uint8 := x.(uint8)
|
|
|
|
if uint8 != 0 {
|
2012-02-13 12:37:17 -08:00
|
|
|
t.Error("uint8 did not overflow as expected; value:", uint8)
|
2012-01-04 01:48:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementUnderflowUint(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("uint8", uint8(0), DefaultExpiration)
|
2012-01-04 01:48:21 -08:00
|
|
|
err := tc.Decrement("uint8", 1)
|
|
|
|
if err != nil {
|
2012-02-13 12:37:17 -08:00
|
|
|
t.Error("Error decrementing int8:", err)
|
2012-01-04 01:48:21 -08:00
|
|
|
}
|
|
|
|
x, _ := tc.Get("uint8")
|
|
|
|
uint8 := x.(uint8)
|
|
|
|
if uint8 != 255 {
|
2012-02-13 12:37:17 -08:00
|
|
|
t.Error("uint8 did not underflow as expected; value:", uint8)
|
2012-01-04 01:48:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 19:00:08 -08:00
|
|
|
func TestOnEvicted(t *testing.T) {
|
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("foo", 3, DefaultExpiration)
|
|
|
|
if tc.onEvicted != nil {
|
|
|
|
t.Fatal("tc.onEvicted is not nil")
|
|
|
|
}
|
|
|
|
works := false
|
|
|
|
tc.OnEvicted(func(k string, v interface{}) {
|
|
|
|
if k == "foo" && v.(int) == 3 {
|
|
|
|
works = true
|
|
|
|
}
|
2015-11-28 09:22:52 -08:00
|
|
|
tc.Set("bar", 4, DefaultExpiration)
|
2015-11-27 19:00:08 -08:00
|
|
|
})
|
|
|
|
tc.Delete("foo")
|
2015-11-28 09:22:52 -08:00
|
|
|
x, _ := tc.Get("bar")
|
2015-11-27 19:00:08 -08:00
|
|
|
if !works {
|
|
|
|
t.Error("works bool not true")
|
|
|
|
}
|
2015-11-28 09:22:52 -08:00
|
|
|
if x.(int) != 4 {
|
|
|
|
t.Error("bar was not 4")
|
|
|
|
}
|
2015-11-27 19:00:08 -08:00
|
|
|
}
|
|
|
|
|
2012-01-28 18:16:59 -08:00
|
|
|
func TestCacheSerialization(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
2012-01-28 18:16:59 -08:00
|
|
|
testFillAndSerialize(t, tc)
|
|
|
|
|
|
|
|
// Check if gob.Register behaves properly even after multiple gob.Register
|
|
|
|
// on c.Items (many of which will be the same type)
|
|
|
|
testFillAndSerialize(t, tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFillAndSerialize(t *testing.T, tc *Cache) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("a", "a", DefaultExpiration)
|
|
|
|
tc.Set("b", "b", DefaultExpiration)
|
|
|
|
tc.Set("c", "c", DefaultExpiration)
|
2012-01-28 20:30:21 -08:00
|
|
|
tc.Set("expired", "foo", 1*time.Millisecond)
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("*struct", &TestStruct{Num: 1}, DefaultExpiration)
|
2012-01-28 18:16:59 -08:00
|
|
|
tc.Set("[]struct", []TestStruct{
|
|
|
|
{Num: 2},
|
|
|
|
{Num: 3},
|
2014-12-21 22:46:22 -08:00
|
|
|
}, DefaultExpiration)
|
2012-01-28 18:16:59 -08:00
|
|
|
tc.Set("[]*struct", []*TestStruct{
|
|
|
|
&TestStruct{Num: 4},
|
|
|
|
&TestStruct{Num: 5},
|
2014-12-21 22:46:22 -08:00
|
|
|
}, DefaultExpiration)
|
2012-01-28 19:35:47 -08:00
|
|
|
tc.Set("structception", &TestStruct{
|
|
|
|
Num: 42,
|
2012-01-28 19:43:26 -08:00
|
|
|
Children: []*TestStruct{
|
|
|
|
&TestStruct{Num: 6174},
|
|
|
|
&TestStruct{Num: 4716},
|
|
|
|
},
|
2014-12-21 22:46:22 -08:00
|
|
|
}, DefaultExpiration)
|
2012-01-28 18:16:59 -08:00
|
|
|
|
|
|
|
fp := &bytes.Buffer{}
|
|
|
|
err := tc.Save(fp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Couldn't save cache to fp:", err)
|
|
|
|
}
|
|
|
|
|
2014-12-21 22:46:22 -08:00
|
|
|
oc := New(DefaultExpiration, 0)
|
2012-01-28 18:16:59 -08:00
|
|
|
err = oc.Load(fp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Couldn't load cache from fp:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a, found := oc.Get("a")
|
|
|
|
if !found {
|
|
|
|
t.Error("a was not found")
|
|
|
|
}
|
|
|
|
if a.(string) != "a" {
|
|
|
|
t.Error("a is not a")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, found := oc.Get("b")
|
|
|
|
if !found {
|
|
|
|
t.Error("b was not found")
|
|
|
|
}
|
|
|
|
if b.(string) != "b" {
|
|
|
|
t.Error("b is not b")
|
|
|
|
}
|
|
|
|
|
|
|
|
c, found := oc.Get("c")
|
|
|
|
if !found {
|
|
|
|
t.Error("c was not found")
|
|
|
|
}
|
|
|
|
if c.(string) != "c" {
|
|
|
|
t.Error("c is not c")
|
|
|
|
}
|
|
|
|
|
2012-02-11 16:54:16 -08:00
|
|
|
<-time.After(5 * time.Millisecond)
|
2012-01-28 20:30:21 -08:00
|
|
|
_, found = oc.Get("expired")
|
|
|
|
if found {
|
|
|
|
t.Error("expired was found")
|
|
|
|
}
|
|
|
|
|
2012-01-28 18:16:59 -08:00
|
|
|
s1, found := oc.Get("*struct")
|
|
|
|
if !found {
|
|
|
|
t.Error("*struct was not found")
|
|
|
|
}
|
|
|
|
if s1.(*TestStruct).Num != 1 {
|
|
|
|
t.Error("*struct.Num is not 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
s2, found := oc.Get("[]struct")
|
|
|
|
if !found {
|
|
|
|
t.Error("[]struct was not found")
|
|
|
|
}
|
|
|
|
s2r := s2.([]TestStruct)
|
|
|
|
if len(s2r) != 2 {
|
|
|
|
t.Error("Length of s2r is not 2")
|
|
|
|
}
|
|
|
|
if s2r[0].Num != 2 {
|
|
|
|
t.Error("s2r[0].Num is not 2")
|
|
|
|
}
|
|
|
|
if s2r[1].Num != 3 {
|
|
|
|
t.Error("s2r[1].Num is not 3")
|
|
|
|
}
|
|
|
|
|
|
|
|
s3, found := oc.get("[]*struct")
|
|
|
|
if !found {
|
|
|
|
t.Error("[]*struct was not found")
|
|
|
|
}
|
|
|
|
s3r := s3.([]*TestStruct)
|
|
|
|
if len(s3r) != 2 {
|
|
|
|
t.Error("Length of s3r is not 2")
|
|
|
|
}
|
|
|
|
if s3r[0].Num != 4 {
|
|
|
|
t.Error("s3r[0].Num is not 4")
|
|
|
|
}
|
|
|
|
if s3r[1].Num != 5 {
|
|
|
|
t.Error("s3r[1].Num is not 5")
|
|
|
|
}
|
2012-01-28 19:35:47 -08:00
|
|
|
|
|
|
|
s4, found := oc.get("structception")
|
|
|
|
if !found {
|
|
|
|
t.Error("structception was not found")
|
|
|
|
}
|
|
|
|
s4r := s4.(*TestStruct)
|
2012-01-28 19:43:26 -08:00
|
|
|
if len(s4r.Children) != 2 {
|
|
|
|
t.Error("Length of s4r.Children is not 2")
|
2012-01-28 19:35:47 -08:00
|
|
|
}
|
|
|
|
if s4r.Children[0].Num != 6174 {
|
|
|
|
t.Error("s4r.Children[0].Num is not 6174")
|
|
|
|
}
|
2012-01-28 19:43:26 -08:00
|
|
|
if s4r.Children[1].Num != 4716 {
|
|
|
|
t.Error("s4r.Children[1].Num is not 4716")
|
|
|
|
}
|
2012-01-28 18:16:59 -08:00
|
|
|
}
|
|
|
|
|
2012-01-28 20:30:21 -08:00
|
|
|
func TestFileSerialization(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Add("a", "a", DefaultExpiration)
|
|
|
|
tc.Add("b", "b", DefaultExpiration)
|
2012-02-11 15:15:25 -08:00
|
|
|
f, err := ioutil.TempFile("", "go-cache-cache.dat")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Couldn't create cache file:", err)
|
|
|
|
}
|
|
|
|
fname := f.Name()
|
|
|
|
f.Close()
|
2012-01-28 20:30:21 -08:00
|
|
|
tc.SaveFile(fname)
|
|
|
|
|
2014-12-21 22:46:22 -08:00
|
|
|
oc := New(DefaultExpiration, 0)
|
2012-01-28 20:30:21 -08:00
|
|
|
oc.Add("a", "aa", 0) // this should not be overwritten
|
2012-02-11 15:15:25 -08:00
|
|
|
err = oc.LoadFile(fname)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2012-01-28 20:30:21 -08:00
|
|
|
a, found := oc.Get("a")
|
|
|
|
if !found {
|
|
|
|
t.Error("a was not found")
|
|
|
|
}
|
|
|
|
astr := a.(string)
|
|
|
|
if astr != "aa" {
|
|
|
|
if astr == "a" {
|
|
|
|
t.Error("a was overwritten")
|
|
|
|
} else {
|
|
|
|
t.Error("a is not aa")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b, found := oc.Get("b")
|
|
|
|
if !found {
|
|
|
|
t.Error("b was not found")
|
|
|
|
}
|
|
|
|
if b.(string) != "b" {
|
|
|
|
t.Error("b is not b")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-28 19:04:33 -08:00
|
|
|
func TestSerializeUnserializable(t *testing.T) {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
2012-01-28 19:04:33 -08:00
|
|
|
ch := make(chan bool, 1)
|
|
|
|
ch <- true
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("chan", ch, DefaultExpiration)
|
2012-01-28 19:04:33 -08:00
|
|
|
fp := &bytes.Buffer{}
|
|
|
|
err := tc.Save(fp) // this should fail gracefully
|
|
|
|
if err.Error() != "gob NewTypeObject can't handle type: chan bool" {
|
|
|
|
t.Error("Error from Save was not gob NewTypeObject can't handle type chan bool:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 07:45:30 -08:00
|
|
|
func BenchmarkCacheGetExpiring(b *testing.B) {
|
2015-11-30 13:04:57 -08:00
|
|
|
benchmarkCacheGet(b, 5*time.Minute)
|
2015-11-30 07:45:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCacheGetNotExpiring(b *testing.B) {
|
|
|
|
benchmarkCacheGet(b, NoExpiration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkCacheGet(b *testing.B, exp time.Duration) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2015-11-30 07:45:30 -08:00
|
|
|
tc := New(exp, 0)
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("foo", "bar", DefaultExpiration)
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-01-04 00:55:32 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tc.Get("foo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 17:40:26 -07:00
|
|
|
func BenchmarkRWMutexMapGet(b *testing.B) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2012-06-21 19:06:06 -07:00
|
|
|
m := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
2013-06-30 17:31:46 -07:00
|
|
|
mu := sync.RWMutex{}
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-06-21 19:06:06 -07:00
|
|
|
for i := 0; i < b.N; i++ {
|
2013-06-30 17:31:46 -07:00
|
|
|
mu.RLock()
|
2012-06-21 19:06:06 -07:00
|
|
|
_, _ = m["foo"]
|
2013-06-30 17:31:46 -07:00
|
|
|
mu.RUnlock()
|
2012-06-21 19:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 06:55:58 -08:00
|
|
|
func BenchmarkRWMutexInterfaceMapGetStruct(b *testing.B) {
|
2015-12-03 06:40:14 -08:00
|
|
|
b.StopTimer()
|
|
|
|
s := struct{name string}{name: "foo"}
|
|
|
|
m := map[interface{}]string{
|
|
|
|
s: "bar",
|
|
|
|
}
|
|
|
|
mu := sync.RWMutex{}
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mu.RLock()
|
|
|
|
_, _ = m[s]
|
|
|
|
mu.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 06:55:58 -08:00
|
|
|
func BenchmarkRWMutexInterfaceMapGetString(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
m := map[interface{}]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
mu := sync.RWMutex{}
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mu.RLock()
|
|
|
|
_, _ = m["foo"]
|
|
|
|
mu.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 07:45:30 -08:00
|
|
|
func BenchmarkCacheGetConcurrentExpiring(b *testing.B) {
|
2015-11-30 13:04:57 -08:00
|
|
|
benchmarkCacheGetConcurrent(b, 5*time.Minute)
|
2015-11-30 07:45:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCacheGetConcurrentNotExpiring(b *testing.B) {
|
|
|
|
benchmarkCacheGetConcurrent(b, NoExpiration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkCacheGetConcurrent(b *testing.B, exp time.Duration) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2015-11-30 07:45:30 -08:00
|
|
|
tc := New(exp, 0)
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("foo", "bar", DefaultExpiration)
|
2012-02-21 09:46:25 -08:00
|
|
|
wg := new(sync.WaitGroup)
|
2012-06-21 19:06:06 -07:00
|
|
|
workers := runtime.NumCPU()
|
|
|
|
each := b.N / workers
|
|
|
|
wg.Add(workers)
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-06-21 19:06:06 -07:00
|
|
|
for i := 0; i < workers; i++ {
|
2012-02-21 09:46:25 -08:00
|
|
|
go func() {
|
2012-06-21 19:06:06 -07:00
|
|
|
for j := 0; j < each; j++ {
|
2012-06-21 17:36:24 -07:00
|
|
|
tc.Get("foo")
|
|
|
|
}
|
2012-02-21 09:46:25 -08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2013-06-30 17:40:26 -07:00
|
|
|
func BenchmarkRWMutexMapGetConcurrent(b *testing.B) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2012-01-04 00:55:32 -08:00
|
|
|
m := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
2013-06-30 17:31:46 -07:00
|
|
|
mu := sync.RWMutex{}
|
2012-06-21 19:06:06 -07:00
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
workers := runtime.NumCPU()
|
|
|
|
each := b.N / workers
|
|
|
|
wg.Add(workers)
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-06-21 19:06:06 -07:00
|
|
|
for i := 0; i < workers; i++ {
|
|
|
|
go func() {
|
|
|
|
for j := 0; j < each; j++ {
|
2013-06-30 17:31:46 -07:00
|
|
|
mu.RLock()
|
2012-06-21 19:06:06 -07:00
|
|
|
_, _ = m["foo"]
|
2013-06-30 17:31:46 -07:00
|
|
|
mu.RUnlock()
|
2012-06-21 19:06:06 -07:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2012-01-04 00:55:32 -08:00
|
|
|
}
|
2012-06-21 19:06:06 -07:00
|
|
|
wg.Wait()
|
2012-01-04 00:55:32 -08:00
|
|
|
}
|
|
|
|
|
2015-11-30 07:45:30 -08:00
|
|
|
func BenchmarkCacheGetManyConcurrentExpiring(b *testing.B) {
|
2015-11-30 13:04:57 -08:00
|
|
|
benchmarkCacheGetManyConcurrent(b, 5*time.Minute)
|
2015-11-30 07:45:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCacheGetManyConcurrentNotExpiring(b *testing.B) {
|
|
|
|
benchmarkCacheGetManyConcurrent(b, NoExpiration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
|
2012-06-22 01:24:09 -07:00
|
|
|
// This is the same as BenchmarkCacheGetConcurrent, but its result
|
2014-12-22 07:37:59 -08:00
|
|
|
// can be compared against BenchmarkShardedCacheGetManyConcurrent
|
|
|
|
// in sharded_test.go.
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
|
|
|
n := 10000
|
2015-11-30 07:45:30 -08:00
|
|
|
tc := New(exp, 0)
|
2012-06-22 01:24:09 -07:00
|
|
|
keys := make([]string, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
k := "foo" + strconv.Itoa(n)
|
|
|
|
keys[i] = k
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set(k, "bar", DefaultExpiration)
|
2012-06-22 01:24:09 -07:00
|
|
|
}
|
|
|
|
each := b.N / n
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(n)
|
|
|
|
for _, v := range keys {
|
|
|
|
go func() {
|
|
|
|
for j := 0; j < each; j++ {
|
|
|
|
tc.Get(v)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2015-11-30 07:45:30 -08:00
|
|
|
func BenchmarkCacheSetExpiring(b *testing.B) {
|
2015-11-30 13:04:57 -08:00
|
|
|
benchmarkCacheSet(b, 5*time.Minute)
|
2015-11-30 07:45:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCacheSetNotExpiring(b *testing.B) {
|
|
|
|
benchmarkCacheSet(b, NoExpiration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkCacheSet(b *testing.B, exp time.Duration) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2015-11-30 07:45:30 -08:00
|
|
|
tc := New(exp, 0)
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-01-04 00:55:32 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("foo", "bar", DefaultExpiration)
|
2012-01-04 00:55:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 17:40:26 -07:00
|
|
|
func BenchmarkRWMutexMapSet(b *testing.B) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2012-01-04 00:55:32 -08:00
|
|
|
m := map[string]string{}
|
2013-06-30 17:40:26 -07:00
|
|
|
mu := sync.RWMutex{}
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-01-04 00:55:32 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2012-06-21 19:06:06 -07:00
|
|
|
mu.Lock()
|
2012-01-04 00:55:32 -08:00
|
|
|
m["foo"] = "bar"
|
2012-06-21 19:06:06 -07:00
|
|
|
mu.Unlock()
|
2012-01-04 00:55:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCacheSetDelete(b *testing.B) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-01-04 00:55:32 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.Set("foo", "bar", DefaultExpiration)
|
2012-01-04 00:55:32 -08:00
|
|
|
tc.Delete("foo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 17:40:26 -07:00
|
|
|
func BenchmarkRWMutexMapSetDelete(b *testing.B) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2012-06-21 19:06:06 -07:00
|
|
|
m := map[string]string{}
|
2013-06-30 17:40:26 -07:00
|
|
|
mu := sync.RWMutex{}
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-06-21 19:06:06 -07:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mu.Lock()
|
|
|
|
m["foo"] = "bar"
|
|
|
|
mu.Unlock()
|
|
|
|
mu.Lock()
|
|
|
|
delete(m, "foo")
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 01:06:31 -08:00
|
|
|
func BenchmarkCacheSetDeleteSingleLock(b *testing.B) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2014-12-21 22:46:22 -08:00
|
|
|
tc := New(DefaultExpiration, 0)
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-01-04 01:06:31 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2015-11-27 10:03:24 -08:00
|
|
|
tc.mu.Lock()
|
2014-12-21 22:46:22 -08:00
|
|
|
tc.set("foo", "bar", DefaultExpiration)
|
2012-01-04 01:06:31 -08:00
|
|
|
tc.delete("foo")
|
2015-11-27 10:03:24 -08:00
|
|
|
tc.mu.Unlock()
|
2012-01-04 01:06:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 17:40:26 -07:00
|
|
|
func BenchmarkRWMutexMapSetDeleteSingleLock(b *testing.B) {
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StopTimer()
|
2012-01-04 00:55:32 -08:00
|
|
|
m := map[string]string{}
|
2013-06-30 17:40:26 -07:00
|
|
|
mu := sync.RWMutex{}
|
2012-06-22 01:24:09 -07:00
|
|
|
b.StartTimer()
|
2012-01-04 00:55:32 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2012-06-21 19:06:06 -07:00
|
|
|
mu.Lock()
|
2012-01-04 00:55:32 -08:00
|
|
|
m["foo"] = "bar"
|
|
|
|
delete(m, "foo")
|
2012-06-21 19:06:06 -07:00
|
|
|
mu.Unlock()
|
2012-01-04 00:55:32 -08:00
|
|
|
}
|
|
|
|
}
|
2015-11-28 11:35:38 -08:00
|
|
|
|
2015-11-28 11:56:23 -08:00
|
|
|
func BenchmarkIncrementInt(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
tc := New(DefaultExpiration, 0)
|
|
|
|
tc.Set("foo", 0, DefaultExpiration)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tc.IncrementInt("foo", 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 07:45:30 -08:00
|
|
|
func BenchmarkDeleteExpiredLoop(b *testing.B) {
|
2015-11-28 11:35:38 -08:00
|
|
|
b.StopTimer()
|
2015-11-30 13:04:57 -08:00
|
|
|
tc := New(5*time.Minute, 0)
|
2015-11-28 11:35:38 -08:00
|
|
|
tc.mu.Lock()
|
2015-11-28 12:13:26 -08:00
|
|
|
for i := 0; i < 100000; i++ {
|
2015-11-28 11:35:38 -08:00
|
|
|
tc.set(strconv.Itoa(i), "bar", DefaultExpiration)
|
|
|
|
}
|
|
|
|
tc.mu.Unlock()
|
|
|
|
b.StartTimer()
|
2015-11-28 12:13:26 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tc.DeleteExpired()
|
|
|
|
}
|
2015-11-28 11:35:38 -08:00
|
|
|
}
|