Add a test for file serialization and one to ensure objects expire even after having been serialized and reloaded
This commit is contained in:
parent
bbb477e5bd
commit
eaf2373adf
|
@ -2,6 +2,7 @@ package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -466,6 +467,8 @@ func TestCacheSerialization(t *testing.T) {
|
||||||
func testFillAndSerialize(t *testing.T, tc *Cache) {
|
func testFillAndSerialize(t *testing.T, tc *Cache) {
|
||||||
tc.Set("a", "a", 0)
|
tc.Set("a", "a", 0)
|
||||||
tc.Set("b", "b", 0)
|
tc.Set("b", "b", 0)
|
||||||
|
tc.Set("c", "c", 0)
|
||||||
|
tc.Set("expired", "foo", 1*time.Millisecond)
|
||||||
tc.Set("*struct", &TestStruct{Num: 1}, 0)
|
tc.Set("*struct", &TestStruct{Num: 1}, 0)
|
||||||
tc.Set("[]struct", []TestStruct{
|
tc.Set("[]struct", []TestStruct{
|
||||||
{Num: 2},
|
{Num: 2},
|
||||||
|
@ -482,7 +485,6 @@ func testFillAndSerialize(t *testing.T, tc *Cache) {
|
||||||
&TestStruct{Num: 4716},
|
&TestStruct{Num: 4716},
|
||||||
},
|
},
|
||||||
}, 0)
|
}, 0)
|
||||||
tc.Set("c", "c", 0) // ordering should be meaningless, but just in case
|
|
||||||
|
|
||||||
fp := &bytes.Buffer{}
|
fp := &bytes.Buffer{}
|
||||||
err := tc.Save(fp)
|
err := tc.Save(fp)
|
||||||
|
@ -520,6 +522,12 @@ func testFillAndSerialize(t *testing.T, tc *Cache) {
|
||||||
t.Error("c is not c")
|
t.Error("c is not c")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
<-time.After(5*time.Millisecond)
|
||||||
|
_, found = oc.Get("expired")
|
||||||
|
if found {
|
||||||
|
t.Error("expired was found")
|
||||||
|
}
|
||||||
|
|
||||||
s1, found := oc.Get("*struct")
|
s1, found := oc.Get("*struct")
|
||||||
if !found {
|
if !found {
|
||||||
t.Error("*struct was not found")
|
t.Error("*struct was not found")
|
||||||
|
@ -574,6 +582,38 @@ func testFillAndSerialize(t *testing.T, tc *Cache) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFileSerialization(t *testing.T) {
|
||||||
|
tc := New(0, 0)
|
||||||
|
tc.Add("a", "a", 0)
|
||||||
|
tc.Add("b", "b", 0)
|
||||||
|
fname := "_test/cache.dat"
|
||||||
|
tc.SaveFile(fname)
|
||||||
|
|
||||||
|
oc := New(0, 0)
|
||||||
|
oc.Add("a", "aa", 0) // this should not be overwritten
|
||||||
|
oc.LoadFile(fname)
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
os.Remove(fname)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSerializeUnserializable(t *testing.T) {
|
func TestSerializeUnserializable(t *testing.T) {
|
||||||
tc := New(0, 0)
|
tc := New(0, 0)
|
||||||
ch := make(chan bool, 1)
|
ch := make(chan bool, 1)
|
||||||
|
|
Loading…
Reference in New Issue