package cache_test import ( "context" "sync" "sync/atomic" "testing" "time" "github.com/stretchr/testify/require" "mtusi-messenger/internal/platform/cache" ) type memoryCache struct { mu sync.Mutex data map[string][]byte } func newMemoryCache() *memoryCache { return &memoryCache{data: make(map[string][]byte)} } func (m *memoryCache) Get(_ context.Context, key string) ([]byte, error) { m.mu.Lock() defer m.mu.Unlock() value, ok := m.data[key] if !ok { return nil, cache.ErrCacheMiss } return value, nil } func (m *memoryCache) Set(_ context.Context, key string, value []byte, _ time.Duration) error { m.mu.Lock() defer m.mu.Unlock() m.data[key] = value return nil } func (m *memoryCache) Delete(_ context.Context, keys ...string) error { m.mu.Lock() defer m.mu.Unlock() for _, key := range keys { delete(m.data, key) } return nil } func (m *memoryCache) Ping(context.Context) error { return nil } func (m *memoryCache) Close() error { return nil } func TestNoOpCacheAlwaysMisses(t *testing.T) { t.Parallel() noop := cache.NoOpCache{} _, err := noop.Get(context.Background(), "key") require.ErrorIs(t, err, cache.ErrCacheMiss) require.NoError(t, noop.Set(context.Background(), "key", []byte("v"), time.Minute)) } func TestGetOrLoadCachesResult(t *testing.T) { t.Parallel() sf := cache.WithSingleflight(newMemoryCache()) ctx := context.Background() var calls atomic.Int32 loader := func(context.Context) ([]byte, error) { calls.Add(1) return []byte("payload"), nil } first, err := sf.GetOrLoad(ctx, "key", time.Minute, loader) require.NoError(t, err) require.Equal(t, []byte("payload"), first) second, err := sf.GetOrLoad(ctx, "key", time.Minute, loader) require.NoError(t, err) require.Equal(t, []byte("payload"), second) require.Equal(t, int32(1), calls.Load(), "loader must run once") } func TestGetOrLoadDeduplicatesConcurrentMisses(t *testing.T) { t.Parallel() sf := cache.WithSingleflight(newMemoryCache()) ctx := context.Background() var calls atomic.Int32 loader := func(context.Context) ([]byte, error) { calls.Add(1) time.Sleep(50 * time.Millisecond) return []byte("payload"), nil } const workers = 50 var ( wg sync.WaitGroup failures atomic.Int32 ) wg.Add(workers) for range workers { go func() { defer wg.Done() value, err := sf.GetOrLoad(ctx, "shared", time.Minute, loader) if err != nil || string(value) != "payload" { failures.Add(1) } }() } wg.Wait() require.Zero(t, failures.Load()) require.Equal(t, int32(1), calls.Load(), "singleflight must collapse concurrent misses") } func TestLeaderCancellationDoesNotCancelSharedLoad(t *testing.T) { t.Parallel() sf := cache.WithSingleflight(newMemoryCache()) ctx, cancel := context.WithCancel(t.Context()) defer cancel() started := make(chan struct{}) release := make(chan struct{}) result := make(chan error, 1) go func() { _, err := sf.GetOrLoad(ctx, "cancelled-leader", time.Minute, func(loadCtx context.Context) ([]byte, error) { close(started) <-release if err := loadCtx.Err(); err != nil { return nil, err } return []byte("payload"), nil }) result <- err }() <-started cancel() close(release) require.NoError(t, <-result) value, err := sf.GetOrLoad(t.Context(), "cancelled-leader", time.Minute, func(context.Context) ([]byte, error) { return []byte("unexpected reload"), nil }) require.NoError(t, err) require.Equal(t, "payload", string(value)) }