lab2 fix
Application / Lint (pull_request) Canceled after 0s
Application / Unit tests (pull_request) Canceled after 0s
Application / Docker build (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
Vladislav Kunetskiy
2026-10-10 00:50:24 +03:00
parent 1e38dbdf35
commit da9fcf346a
36 changed files with 995 additions and 1038 deletions
+142 -8
View File
@@ -4,6 +4,7 @@ package redis_test
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
@@ -15,8 +16,9 @@ import (
"mtusi-messenger/internal/domain"
"mtusi-messenger/internal/platform/cache"
cachemocks "mtusi-messenger/internal/platform/cache/mocks"
storageredis "mtusi-messenger/internal/storage/redis"
"mtusi-messenger/internal/storage/redis/mocks"
storagemocks "mtusi-messenger/internal/storage/redis/mocks"
)
type ChatRepositorySuite struct {
@@ -65,7 +67,8 @@ func (s *ChatRepositorySuite) TestFirstPageIsCached() {
alice := uuid.New()
page := domain.Page[domain.Chat]{Items: []domain.Chat{}}
source := mocks.NewMockChatSource(s.T())
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Twice()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).Return(page, nil).Once()
repo := s.newRepo(source)
@@ -92,10 +95,14 @@ func (s *ChatRepositorySuite) TestCreateInvalidatesMemberCaches() {
}
page := domain.Page[domain.Chat]{Items: []domain.Chat{chat}}
source := mocks.NewMockChatSource(s.T())
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Once()
source.EXPECT().ListVersion(mock.Anything, bob).Return(int64(0), nil).Once()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).Return(empty, nil).Once()
source.EXPECT().ListVisible(mock.Anything, bob, (*domain.Cursor)(nil), 50).Return(empty, nil).Once()
source.EXPECT().Create(mock.Anything, mock.AnythingOfType("domain.ChatDraft")).Return(chat, nil).Once()
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(1), nil).Once()
source.EXPECT().ListVersion(mock.Anything, bob).Return(int64(1), nil).Once()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).Return(page, nil).Once()
source.EXPECT().ListVisible(mock.Anything, bob, (*domain.Cursor)(nil), 50).Return(page, nil).Once()
@@ -122,13 +129,34 @@ func (s *ChatRepositorySuite) TestCreateInvalidatesMemberCaches() {
s.Len(bobPage.Items, 1, "other member must observe the new chat immediately")
}
func (s *ChatRepositorySuite) TestEvictedCacheEntryReloadsFromSource() {
ctx := s.T().Context()
alice := uuid.New()
empty := domain.Page[domain.Chat]{Items: []domain.Chat{}}
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Twice()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).Return(empty, nil).Twice()
repo := s.newRepo(source)
_, err := repo.ListVisible(ctx, alice, nil, 50)
s.Require().NoError(err)
// Simulate a Redis flush/restart by dropping all cache entries.
s.Require().NoError(flushRedisDB(ctx))
_, err = repo.ListVisible(ctx, alice, nil, 50)
s.Require().NoError(err)
}
func (s *ChatRepositorySuite) TestDeeperPagesBypassCache() {
ctx := s.T().Context()
alice := uuid.New()
cursor := &domain.Cursor{Scope: alice, CreatedAt: time.Now().UTC(), ID: uuid.New()}
empty := domain.Page[domain.Chat]{Items: []domain.Chat{}}
source := mocks.NewMockChatSource(s.T())
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVisible(mock.Anything, alice, cursor, 50).Return(empty, nil).Twice()
repo := s.newRepo(source)
@@ -144,7 +172,8 @@ func (s *ChatRepositorySuite) TestLimitIsPartOfCacheKey() {
alice := uuid.New()
empty := domain.Page[domain.Chat]{Items: []domain.Chat{}}
source := mocks.NewMockChatSource(s.T())
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Twice()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 10).Return(empty, nil).Once()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).Return(empty, nil).Once()
@@ -161,7 +190,8 @@ func (s *ChatRepositorySuite) TestConcurrentColdReadsAreDeduplicated() {
alice := uuid.New()
empty := domain.Page[domain.Chat]{Items: []domain.Chat{}}
source := mocks.NewMockChatSource(s.T())
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Maybe()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).
RunAndReturn(func(context.Context, uuid.UUID, *domain.Cursor, int) (domain.Page[domain.Chat], error) {
time.Sleep(25 * time.Millisecond)
@@ -193,6 +223,7 @@ func (s *ChatRepositorySuite) TestConcurrentColdReadsAreDeduplicated() {
wg.Wait()
s.Require().Zero(failures.Load())
s.T().Logf("distributed lock: %d concurrent cold reads -> 1 source load", workers)
}
func (s *ChatRepositorySuite) TestMultipleInstancesShareDistributedLock() {
@@ -200,7 +231,8 @@ func (s *ChatRepositorySuite) TestMultipleInstancesShareDistributedLock() {
alice := uuid.New()
empty := domain.Page[domain.Chat]{Items: []domain.Chat{}}
source := mocks.NewMockChatSource(s.T())
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Maybe()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).
RunAndReturn(func(context.Context, uuid.UUID, *domain.Cursor, int) (domain.Page[domain.Chat], error) {
time.Sleep(25 * time.Millisecond)
@@ -241,6 +273,108 @@ func (s *ChatRepositorySuite) TestMultipleInstancesShareDistributedLock() {
wg.Wait()
s.Require().Zero(failures.Load())
s.T().Logf("multi-instance distributed lock: %d instances x %d reads -> 1 source load",
instances, workersPerInstance)
}
func (s *ChatRepositorySuite) TestCacheUnavailableFallsBackToSource() {
ctx := s.T().Context()
alice := uuid.New()
page := domain.Page[domain.Chat]{Items: []domain.Chat{}}
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Once()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).Return(page, nil).Once()
lockStore := cachemocks.NewMockLockStore(s.T())
lockStore.EXPECT().Get(mock.Anything, mock.Anything).Return(nil, errors.New("redis down")).Maybe()
distributed, err := cache.NewDistributed(lockStore, cache.DistributedConfig{}, nil)
s.Require().NoError(err)
repo := storageredis.NewChatRepository(source, distributed, time.Minute, nil)
got, err := repo.ListVisible(ctx, alice, nil, 50)
s.Require().NoError(err, "cache failure must not fail the read")
s.Empty(got.Items)
}
func (s *ChatRepositorySuite) TestBrokenCacheEntryIsDroppedAndRepopulated() {
ctx := s.T().Context()
alice := uuid.New()
page := domain.Page[domain.Chat]{Items: []domain.Chat{}}
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Times(3)
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).Return(page, nil).Twice()
repo := s.newRepo(source)
key := "chat_list:" + alice.String() + ":0:50"
s.Require().NoError(s.cache.Set(ctx, key, []byte("{not json"), time.Minute))
// 1st: garbage -> dropped -> source.
first, err := repo.ListVisible(ctx, alice, nil, 50)
s.Require().NoError(err, "a corrupted cache entry must not fail the read")
// 2nd: miss -> source -> repopulate.
second, err := repo.ListVisible(ctx, alice, nil, 50)
s.Require().NoError(err)
// 3rd: served from cache, no source call.
third, err := repo.ListVisible(ctx, alice, nil, 50)
s.Require().NoError(err)
s.Equal(first, second)
s.Equal(second, third)
}
func (s *ChatRepositorySuite) TestSlowLoaderBeyondCacheBudgetReturnsData() {
ctx := s.T().Context()
alice := uuid.New()
page := domain.Page[domain.Chat]{Items: []domain.Chat{}}
release := make(chan struct{})
var calls atomic.Int32
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Once()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).
RunAndReturn(func(context.Context, uuid.UUID, *domain.Cursor, int) (domain.Page[domain.Chat], error) {
if calls.Add(1) == 1 {
<-release // the loader outlives the caller's cache budget
}
return page, nil
}).Maybe()
repo := s.newRepo(source)
started := time.Now()
got, err := repo.ListVisible(ctx, alice, nil, 50)
s.Require().NoError(err, "budget expiry must fall back to the source, not fail")
s.Empty(got.Items)
s.GreaterOrEqual(time.Since(started), 450*time.Millisecond, "the caller must be bounded by the cache budget")
// Let the background loader finish before the test (and its mock) is torn down.
close(release)
time.Sleep(200 * time.Millisecond)
}
func (s *ChatRepositorySuite) TestSourceErrorIsNotRetried() {
ctx := s.T().Context()
alice := uuid.New()
sentinel := errors.New("postgres down")
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVersion(mock.Anything, alice).Return(int64(0), nil).Once()
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).
Return(domain.Page[domain.Chat]{}, sentinel).Once()
repo := s.newRepo(source)
_, err := repo.ListVisible(ctx, alice, nil, 50)
s.Require().ErrorIs(err, sentinel, "source error must propagate")
}
func (s *ChatRepositorySuite) TestConcurrentColdReadsWithoutProtectionStampede() {
@@ -251,7 +385,7 @@ func (s *ChatRepositorySuite) TestConcurrentColdReadsWithoutProtectionStampede()
var loads atomic.Int32
source := mocks.NewMockChatSource(s.T())
source := storagemocks.NewMockChatSource(s.T())
source.EXPECT().ListVisible(mock.Anything, alice, (*domain.Cursor)(nil), 50).
RunAndReturn(func(context.Context, uuid.UUID, *domain.Cursor, int) (domain.Page[domain.Chat], error) {
loads.Add(1)