2012-01-02 18:01:04 +08:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
tc.Set("a", 1, 0)
|
|
|
|
tc.Set("b", "b", 0)
|
|
|
|
tc.Set("c", 3.5, 0)
|
|
|
|
|
|
|
|
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 21:11:17 +08:00
|
|
|
} else if a2 := x.(int); a2+2 != 3 {
|
2012-01-02 18: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 21:11:17 +08:00
|
|
|
} else if b2 := x.(string); b2+"B" != "bB" {
|
2012-01-02 18: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 21:11:17 +08:00
|
|
|
} else if c2 := x.(float64); c2+1.2 != 4.7 {
|
2012-01-02 18: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 16:11:27 +08:00
|
|
|
tc := New(50*time.Millisecond, 1*time.Millisecond)
|
2012-01-02 18:01:04 +08:00
|
|
|
tc.Set("a", 1, 0)
|
|
|
|
tc.Set("b", 2, -1)
|
2012-01-04 16:11:27 +08:00
|
|
|
tc.Set("c", 3, 20*time.Millisecond)
|
|
|
|
tc.Set("d", 4, 70*time.Millisecond)
|
2012-01-02 18:01:04 +08:00
|
|
|
|
2012-01-04 16:11:27 +08:00
|
|
|
<-time.After(25 * time.Millisecond)
|
2012-01-02 18:01:04 +08:00
|
|
|
_, found = tc.Get("c")
|
|
|
|
if found {
|
|
|
|
t.Error("Found c when it should have been automatically deleted")
|
|
|
|
}
|
|
|
|
|
2012-01-04 16:11:27 +08:00
|
|
|
<-time.After(30 * time.Millisecond)
|
2012-01-02 18: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 16:11:27 +08:00
|
|
|
<-time.After(20 * time.Millisecond)
|
2012-01-02 18: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 18:32:05 +08:00
|
|
|
|
|
|
|
type TestStruct struct {
|
|
|
|
Num int
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorePointerToStruct(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("foo", &TestStruct{Num: 1}, 0)
|
|
|
|
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 20:52:43 +08:00
|
|
|
|
|
|
|
func TestIncrementUint(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tuint", uint(1), 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUintptr(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tuintptr", uintptr(1), 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUint8(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tuint8", uint8(1), 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUint16(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tuint16", uint16(1), 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUint32(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tuint32", uint32(1), 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUint64(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tuint64", uint64(1), 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementInt(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tint", 1, 0)
|
|
|
|
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 TestIncrementInt8(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tint8", int8(1), 0)
|
|
|
|
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 TestIncrementInt16(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tint16", int16(1), 0)
|
|
|
|
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 TestIncrementInt32(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tint32", int32(1), 0)
|
|
|
|
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 TestIncrementInt64(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("tint64", int64(1), 0)
|
|
|
|
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 TestIncrementFloat32(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("float32", float32(1.5), 0)
|
|
|
|
err := tc.Increment("float32", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", 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.5:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementFloat64(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("float64", float64(1.5), 0)
|
|
|
|
err := tc.Increment("float64", 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error incrementing:", 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.5:", x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementInt64(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("int64", int64(5), 0)
|
|
|
|
err := tc.Decrement("int64", 2)
|
|
|
|
if err != nil {
|
2012-01-03 00:26:17 +08:00
|
|
|
t.Error("Error decrementing:", err)
|
2012-01-02 20: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)
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 21:04:47 +08:00
|
|
|
|
|
|
|
func TestAdd(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
err := tc.Add("foo", "bar", 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Couldn't add foo even though it shouldn't exist")
|
|
|
|
}
|
|
|
|
err = tc.Add("foo", "baz", 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Successfully added another foo when it should have returned an error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplace(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
err := tc.Replace("foo", "bar", 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Replaced foo when it shouldn't exist")
|
|
|
|
}
|
|
|
|
tc.Set("foo", "bar", 0)
|
|
|
|
err = tc.Replace("foo", "bar", 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Couldn't replace existing key foo")
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 21:18:25 +08:00
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("foo", "bar", 0)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlush(t *testing.T) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("foo", "bar", 0)
|
|
|
|
tc.Set("baz", "yes", 0)
|
|
|
|
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 16:55:32 +08:00
|
|
|
|
2012-01-04 17:06:31 +08:00
|
|
|
func BenchmarkCacheGet(b *testing.B) {
|
2012-01-04 16:55:32 +08:00
|
|
|
tc := New(0, 0)
|
|
|
|
tc.Set("foo", "bar", 0)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tc.Get("foo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 17:06:31 +08:00
|
|
|
func BenchmarkMapGet(b *testing.B) {
|
2012-01-04 16:55:32 +08:00
|
|
|
m := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, _ = m["foo"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCacheSet(b *testing.B) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tc.Set("foo", "bar", 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMapSet(b *testing.B) {
|
|
|
|
m := map[string]string{}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
m["foo"] = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCacheSetDelete(b *testing.B) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tc.Set("foo", "bar", 0)
|
|
|
|
tc.Delete("foo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 17:06:31 +08:00
|
|
|
func BenchmarkCacheSetDeleteSingleLock(b *testing.B) {
|
|
|
|
tc := New(0, 0)
|
|
|
|
tc.mu.Lock()
|
|
|
|
defer tc.mu.Unlock()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tc.set("foo", "bar", 0)
|
|
|
|
tc.delete("foo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 16:55:32 +08:00
|
|
|
func BenchmarkMapSetDelete(b *testing.B) {
|
|
|
|
m := map[string]string{}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
m["foo"] = "bar"
|
|
|
|
delete(m, "foo")
|
|
|
|
}
|
|
|
|
}
|