| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Jan Schär | 75ea9f4 | 2024-07-29 17:01:41 +0200 | [diff] [blame] | 4 | package cache |
| 5 | |
| 6 | // Taken and modified from CoreDNS, under Apache 2.0. |
| 7 | |
| 8 | import ( |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestCacheAddAndGet(t *testing.T) { |
| 13 | const N = shardSize * 4 |
| 14 | c := New[int](N) |
| 15 | c.Put(1, 1) |
| 16 | |
| 17 | if _, found := c.Get(1); !found { |
| 18 | t.Fatal("Failed to find inserted record") |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func TestCacheSharding(t *testing.T) { |
| 23 | c := New[int](shardSize) |
| 24 | for i := 0; i < shardSize*2; i++ { |
| 25 | c.Put(uint64(i), 1) |
| 26 | } |
| 27 | for i := range c.shards { |
| 28 | if len(c.shards[i].items) == 0 { |
| 29 | t.Errorf("Failed to populate shard: %d", i) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func BenchmarkCache(b *testing.B) { |
| 35 | b.ReportAllocs() |
| 36 | |
| 37 | c := New[int](4) |
| 38 | for n := 0; n < b.N; n++ { |
| 39 | c.Put(1, 1) |
| 40 | c.Get(1) |
| 41 | } |
| 42 | } |