| 1375 | } |
| 1376 | |
| 1377 | func TestFileSerialization(t *testing.T) { |
| 1378 | tc := New(DefaultExpiration, 0) |
| 1379 | tc.Add("a", "a", DefaultExpiration) |
| 1380 | tc.Add("b", "b", DefaultExpiration) |
| 1381 | f, err := ioutil.TempFile("", "go-cache-cache.dat") |
| 1382 | if err != nil { |
| 1383 | t.Fatal("Couldn't create cache file:", err) |
| 1384 | } |
| 1385 | fname := f.Name() |
| 1386 | f.Close() |
| 1387 | tc.SaveFile(fname) |
| 1388 | |
| 1389 | oc := New(DefaultExpiration, 0) |
| 1390 | oc.Add("a", "aa", 0) // this should not be overwritten |
| 1391 | err = oc.LoadFile(fname) |
| 1392 | if err != nil { |
| 1393 | t.Error(err) |
| 1394 | } |
| 1395 | a, found := oc.Get("a") |
| 1396 | if !found { |
| 1397 | t.Error("a was not found") |
| 1398 | } |
| 1399 | astr := a.(string) |
| 1400 | if astr != "aa" { |
| 1401 | if astr == "a" { |
| 1402 | t.Error("a was overwritten") |
| 1403 | } else { |
| 1404 | t.Error("a is not aa") |
| 1405 | } |
| 1406 | } |
| 1407 | b, found := oc.Get("b") |
| 1408 | if !found { |
| 1409 | t.Error("b was not found") |
| 1410 | } |
| 1411 | if b.(string) != "b" { |
| 1412 | t.Error("b is not b") |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | func TestSerializeUnserializable(t *testing.T) { |
| 1417 | tc := New(DefaultExpiration, 0) |