| 7 | ) |
| 8 | |
| 9 | func TestCacheCapacity(t *testing.T) { |
| 10 | var tests = []struct { |
| 11 | addCount, capacity, size int |
| 12 | evictions uint64 |
| 13 | }{ |
| 14 | {1, 0, 0, 0}, |
| 15 | {1, 2, 1, 0}, |
| 16 | {2, 2, 2, 0}, |
| 17 | {3, 2, 2, 1}, |
| 18 | {10, 5, 5, 5}, |
| 19 | } |
| 20 | for i, tt := range tests { |
| 21 | c := NewCache(tt.capacity) |
| 22 | var responses []Response |
| 23 | for i := 0; i < tt.addCount; i++ { |
| 24 | ip := net.ParseIP(fmt.Sprintf("192.0.2.%d", i)) |
| 25 | r := Response{IP: ip} |
| 26 | responses = append(responses, r) |
| 27 | c.Set(ip, r) |
| 28 | } |
| 29 | if got := len(c.entries); got != tt.size { |
| 30 | t.Errorf("#%d: len(entries) = %d, want %d", i, got, tt.size) |
| 31 | } |
| 32 | if got := c.evictions; got != tt.evictions { |
| 33 | t.Errorf("#%d: evictions = %d, want %d", i, got, tt.evictions) |
| 34 | } |
| 35 | if tt.capacity > 0 && tt.addCount > tt.capacity && tt.capacity == tt.size { |
| 36 | lastAdded := responses[tt.addCount-1] |
| 37 | if _, ok := c.Get(lastAdded.IP); !ok { |
| 38 | t.Errorf("#%d: Get(%s) = (_, %t), want (_, %t)", i, lastAdded.IP.String(), ok, !ok) |
| 39 | } |
| 40 | firstAdded := responses[0] |
| 41 | if _, ok := c.Get(firstAdded.IP); ok { |
| 42 | t.Errorf("#%d: Get(%s) = (_, %t), want (_, %t)", i, firstAdded.IP.String(), ok, !ok) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestCacheDuplicate(t *testing.T) { |
| 49 | c := NewCache(10) |