| 61 | } |
| 62 | |
| 63 | func TestCacheResize(t *testing.T) { |
| 64 | c := NewCache(10) |
| 65 | for i := 1; i <= 20; i++ { |
| 66 | ip := net.ParseIP(fmt.Sprintf("192.0.2.%d", i)) |
| 67 | r := Response{IP: ip} |
| 68 | c.Set(ip, r) |
| 69 | } |
| 70 | if got, want := len(c.entries), 10; got != want { |
| 71 | t.Errorf("want %d entries, got %d", want, got) |
| 72 | } |
| 73 | if got, want := c.evictions, uint64(10); got != want { |
| 74 | t.Errorf("want %d evictions, got %d", want, got) |
| 75 | } |
| 76 | if err := c.Resize(5); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if got, want := c.evictions, uint64(0); got != want { |
| 80 | t.Errorf("want %d evictions, got %d", want, got) |
| 81 | } |
| 82 | r := Response{IP: net.ParseIP("192.0.2.42")} |
| 83 | c.Set(r.IP, r) |
| 84 | if got, want := len(c.entries), 5; got != want { |
| 85 | t.Errorf("want %d entries, got %d", want, got) |
| 86 | } |
| 87 | } |