| 69 | } |
| 70 | |
| 71 | func TestCacheTimes(t *testing.T) { |
| 72 | var found bool |
| 73 | |
| 74 | tc := New(50*time.Millisecond, 1*time.Millisecond) |
| 75 | tc.Set("a", 1, DefaultExpiration) |
| 76 | tc.Set("b", 2, NoExpiration) |
| 77 | tc.Set("c", 3, 20*time.Millisecond) |
| 78 | tc.Set("d", 4, 70*time.Millisecond) |
| 79 | |
| 80 | <-time.After(25 * time.Millisecond) |
| 81 | _, found = tc.Get("c") |
| 82 | if found { |
| 83 | t.Error("Found c when it should have been automatically deleted") |
| 84 | } |
| 85 | |
| 86 | <-time.After(30 * time.Millisecond) |
| 87 | _, found = tc.Get("a") |
| 88 | if found { |
| 89 | t.Error("Found a when it should have been automatically deleted") |
| 90 | } |
| 91 | |
| 92 | _, found = tc.Get("b") |
| 93 | if !found { |
| 94 | t.Error("Did not find b even though it was set to never expire") |
| 95 | } |
| 96 | |
| 97 | _, found = tc.Get("d") |
| 98 | if !found { |
| 99 | t.Error("Did not find d even though it was set to expire later than the default") |
| 100 | } |
| 101 | |
| 102 | <-time.After(20 * time.Millisecond) |
| 103 | _, found = tc.Get("d") |
| 104 | if found { |
| 105 | t.Error("Found d when it should have been automatically deleted (later than the default)") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestNewFrom(t *testing.T) { |
| 110 | m := map[string]Item{ |