| 1391 | } |
| 1392 | |
| 1393 | func TestFileSerialization(t *testing.T) { |
| 1394 | tc := New(DefaultExpiration, 0) |
| 1395 | tc.Add("a", "a", DefaultExpiration) |
| 1396 | tc.Add("b", "b", DefaultExpiration) |
| 1397 | f, err := ioutil.TempFile("", "go-cache-cache.dat") |
| 1398 | if err != nil { |
| 1399 | t.Fatal("Couldn't create cache file:", err) |
| 1400 | } |
| 1401 | fname := f.Name() |
| 1402 | f.Close() |
| 1403 | tc.SaveFile(fname) |
| 1404 | |
| 1405 | oc := New(DefaultExpiration, 0) |
| 1406 | oc.Add("a", "aa", 0) // this should not be overwritten |
| 1407 | err = oc.LoadFile(fname) |
| 1408 | if err != nil { |
| 1409 | t.Error(err) |
| 1410 | } |
| 1411 | a, found := oc.Get("a") |
| 1412 | if !found { |
| 1413 | t.Error("a was not found") |
| 1414 | } |
| 1415 | astr := a.(string) |
| 1416 | if astr != "aa" { |
| 1417 | if astr == "a" { |
| 1418 | t.Error("a was overwritten") |
| 1419 | } else { |
| 1420 | t.Error("a is not aa") |
| 1421 | } |
| 1422 | } |
| 1423 | b, found := oc.Get("b") |
| 1424 | if !found { |
| 1425 | t.Error("b was not found") |
| 1426 | } |
| 1427 | if b.(string) != "b" { |
| 1428 | t.Error("b is not b") |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | func TestSerializeUnserializable(t *testing.T) { |
| 1433 | tc := New(DefaultExpiration, 0) |