|
| 1 | +package ttlmap_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "sync" |
| 6 | + "sync/atomic" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/packaged/ttlmap" |
| 11 | +) |
| 12 | + |
| 13 | +func TestFetch_MissSingleflight(t *testing.T) { |
| 14 | + cache := ttlmap.New(ttlmap.WithDefaultTTL(100 * time.Millisecond)) |
| 15 | + |
| 16 | + var calls int32 |
| 17 | + source := func(key string) (int, error) { |
| 18 | + time.Sleep(10 * time.Millisecond) |
| 19 | + atomic.AddInt32(&calls, 1) |
| 20 | + return 123, nil |
| 21 | + } |
| 22 | + |
| 23 | + // Launch many concurrent callers for a missing key |
| 24 | + const n = 32 |
| 25 | + var wg sync.WaitGroup |
| 26 | + wg.Add(n) |
| 27 | + results := make([]int, n) |
| 28 | + for i := 0; i < n; i++ { |
| 29 | + go func(ix int) { |
| 30 | + defer wg.Done() |
| 31 | + v, err := ttlmap.Fetch[int](cache, "k1", source) |
| 32 | + if err != nil { |
| 33 | + t.Errorf("unexpected error: %v", err) |
| 34 | + return |
| 35 | + } |
| 36 | + results[ix] = v |
| 37 | + }(i) |
| 38 | + } |
| 39 | + wg.Wait() |
| 40 | + |
| 41 | + if atomic.LoadInt32(&calls) != 1 { |
| 42 | + t.Fatalf("expected single source call, got %d", calls) |
| 43 | + } |
| 44 | + for i := 0; i < n; i++ { |
| 45 | + if results[i] != 123 { |
| 46 | + t.Fatalf("unexpected result at %d: %d", i, results[i]) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestFetch_TypeMismatch(t *testing.T) { |
| 52 | + cache := ttlmap.New(ttlmap.WithDefaultTTL(100 * time.Millisecond)) |
| 53 | + cache.Set("k", "hello", nil) |
| 54 | + |
| 55 | + var called int32 |
| 56 | + src := func(key string) (int, error) { |
| 57 | + atomic.AddInt32(&called, 1) |
| 58 | + return 42, nil |
| 59 | + } |
| 60 | + _, err := ttlmap.Fetch[int](cache, "k", src) |
| 61 | + if err == nil { |
| 62 | + t.Fatalf("expected ErrTypeMismatch, got nil") |
| 63 | + } |
| 64 | + if err != ttlmap.ErrTypeMismatch { |
| 65 | + t.Fatalf("expected ErrTypeMismatch, got %v", err) |
| 66 | + } |
| 67 | + if atomic.LoadInt32(&called) != 0 { |
| 68 | + t.Fatalf("source should not be called on type mismatch hit") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestFetch_StaleWhileRevalidate(t *testing.T) { |
| 73 | + // Short TTL to trigger expiry |
| 74 | + cache := ttlmap.New(ttlmap.WithDefaultTTL(time.Second), ttlmap.WithMaxLifetime(2*time.Second)) |
| 75 | + ttl := 20 * time.Millisecond |
| 76 | + cache.Set("sk", 1, &ttl) |
| 77 | + |
| 78 | + src := func(key string) (int, error) { |
| 79 | + time.Sleep(15 * time.Millisecond) |
| 80 | + return 2, nil |
| 81 | + } |
| 82 | + // First call should return stale value 1 and trigger background refresh |
| 83 | + v1, err := ttlmap.Fetch[int](cache, "sk", src) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("unexpected err: %v", err) |
| 86 | + } |
| 87 | + if v1 != 1 { |
| 88 | + t.Fatalf("expected stale value 1, got %d", v1) |
| 89 | + } |
| 90 | + |
| 91 | + wg := sync.WaitGroup{} |
| 92 | + for i := 0; i < 100000; i++ { |
| 93 | + wg.Add(1) |
| 94 | + go func() { |
| 95 | + defer wg.Done() |
| 96 | + _, fetchErr := ttlmap.Fetch[int](cache, "sk", src) |
| 97 | + if fetchErr != nil { |
| 98 | + log.Print(fetchErr.Error()) |
| 99 | + } |
| 100 | + }() |
| 101 | + } |
| 102 | + wg.Wait() |
| 103 | + |
| 104 | + time.Sleep(10 * time.Millisecond) |
| 105 | + // Next call should observe refreshed value 2 |
| 106 | + v2, err := ttlmap.Fetch[int](cache, "sk", src) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("unexpected err: %v", err) |
| 109 | + } |
| 110 | + if v2 != 2 { |
| 111 | + t.Fatalf("expected refreshed value 2, got %d", v2) |
| 112 | + } |
| 113 | +} |
0 commit comments