From eaf2373adf964b7565bc717fe632470d5d3e81e8 Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Sun, 29 Jan 2012 05:30:21 +0100 Subject: [PATCH] Add a test for file serialization and one to ensure objects expire even after having been serialized and reloaded --- cache_test.go | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/cache_test.go b/cache_test.go index e2b73a2..6ad4a34 100644 --- a/cache_test.go +++ b/cache_test.go @@ -2,6 +2,7 @@ package cache import ( "bytes" + "os" "testing" "time" ) @@ -466,6 +467,8 @@ func TestCacheSerialization(t *testing.T) { func testFillAndSerialize(t *testing.T, tc *Cache) { tc.Set("a", "a", 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: 2}, @@ -482,7 +485,6 @@ func testFillAndSerialize(t *testing.T, tc *Cache) { &TestStruct{Num: 4716}, }, }, 0) - tc.Set("c", "c", 0) // ordering should be meaningless, but just in case fp := &bytes.Buffer{} err := tc.Save(fp) @@ -520,6 +522,12 @@ func testFillAndSerialize(t *testing.T, tc *Cache) { 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") if !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) { tc := New(0, 0) ch := make(chan bool, 1)