| 1273 | } |
| 1274 | |
| 1275 | func testFillAndSerialize(t *testing.T, tc *Cache) { |
| 1276 | tc.Set("a", "a", DefaultExpiration) |
| 1277 | tc.Set("b", "b", DefaultExpiration) |
| 1278 | tc.Set("c", "c", DefaultExpiration) |
| 1279 | tc.Set("expired", "foo", 1*time.Millisecond) |
| 1280 | tc.Set("*struct", &TestStruct{Num: 1}, DefaultExpiration) |
| 1281 | tc.Set("[]struct", []TestStruct{ |
| 1282 | {Num: 2}, |
| 1283 | {Num: 3}, |
| 1284 | }, DefaultExpiration) |
| 1285 | tc.Set("[]*struct", []*TestStruct{ |
| 1286 | &TestStruct{Num: 4}, |
| 1287 | &TestStruct{Num: 5}, |
| 1288 | }, DefaultExpiration) |
| 1289 | tc.Set("structception", &TestStruct{ |
| 1290 | Num: 42, |
| 1291 | Children: []*TestStruct{ |
| 1292 | &TestStruct{Num: 6174}, |
| 1293 | &TestStruct{Num: 4716}, |
| 1294 | }, |
| 1295 | }, DefaultExpiration) |
| 1296 | |
| 1297 | fp := &bytes.Buffer{} |
| 1298 | err := tc.Save(fp) |
| 1299 | if err != nil { |
| 1300 | t.Fatal("Couldn't save cache to fp:", err) |
| 1301 | } |
| 1302 | |
| 1303 | oc := New(DefaultExpiration, 0) |
| 1304 | err = oc.Load(fp) |
| 1305 | if err != nil { |
| 1306 | t.Fatal("Couldn't load cache from fp:", err) |
| 1307 | } |
| 1308 | |
| 1309 | a, found := oc.Get("a") |
| 1310 | if !found { |
| 1311 | t.Error("a was not found") |
| 1312 | } |
| 1313 | if a.(string) != "a" { |
| 1314 | t.Error("a is not a") |
| 1315 | } |
| 1316 | |
| 1317 | b, found := oc.Get("b") |
| 1318 | if !found { |
| 1319 | t.Error("b was not found") |
| 1320 | } |
| 1321 | if b.(string) != "b" { |
| 1322 | t.Error("b is not b") |
| 1323 | } |
| 1324 | |
| 1325 | c, found := oc.Get("c") |
| 1326 | if !found { |
| 1327 | t.Error("c was not found") |
| 1328 | } |
| 1329 | if c.(string) != "c" { |
| 1330 | t.Error("c is not c") |
| 1331 | } |
| 1332 | |