| 6 | ) |
| 7 | |
| 8 | func TestCache(t *testing.T) { |
| 9 | t.Parallel() |
| 10 | |
| 11 | c := Cache{} |
| 12 | wg := sync.WaitGroup{} |
| 13 | |
| 14 | for i := 0; i < 100; i++ { |
| 15 | wg.Add(1) |
| 16 | j := i |
| 17 | go func() { |
| 18 | c.Store(j, j) |
| 19 | wg.Done() |
| 20 | }() |
| 21 | } |
| 22 | wg.Wait() |
| 23 | |
| 24 | if c.nentries != 100 { |
| 25 | t.Errorf("unexpected cache size; got %d, want 100", c.nentries) |
| 26 | } |
| 27 | |
| 28 | c.Store(100, 100) |
| 29 | if c.nentries != 1 { |
| 30 | t.Error("cache should have been cleared before adding next item") |
| 31 | } |
| 32 | _, ok := c.Load(100) |
| 33 | if !ok { |
| 34 | t.Error("item 100 should have been found in cache") |
| 35 | } |
| 36 | |
| 37 | c.Store(100, 101) |
| 38 | v, ok := c.Load(100) |
| 39 | if !ok || c.nentries != 1 { |
| 40 | t.Error("storing duplicate item should not have cleared cache or changed count") |
| 41 | } |
| 42 | if v != 100 { |
| 43 | t.Errorf("got c.Load(100) = %v, want 100", v) |
| 44 | } |
| 45 | } |