Перенести решение лабораторной 1 как основу для лабораторной 2

This commit is contained in:
2026-10-06 11:54:05 +03:00
parent 65003b6119
commit 5c3f2bfad0
54 changed files with 4472 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
// Package store holds the repository implementations of the users module.
// inmem.go is the in-process one: state lives only in this process and
// disappears with it by design of the lab.
package store
import (
"context"
"sync"
"github.com/google/uuid"
"messenger/internal/user"
)
type InMemUsers struct {
mu sync.RWMutex
byID map[uuid.UUID]user.User
byUsername map[string]struct{}
}
var _ user.Repository = (*InMemUsers)(nil)
func NewInMemUsers() *InMemUsers {
return &InMemUsers{
byID: make(map[uuid.UUID]user.User),
byUsername: make(map[string]struct{}),
}
}
// Create checks and inserts under the same write lock, so concurrent
// registrations of one username admit exactly one winner.
func (r *InMemUsers) Create(_ context.Context, u user.User) error {
r.mu.Lock()
defer r.mu.Unlock()
if _, taken := r.byUsername[u.Username]; taken {
return user.ErrUsernameTaken
}
r.byUsername[u.Username] = struct{}{}
r.byID[u.ID] = u
return nil
}
func (r *InMemUsers) GetByID(_ context.Context, id uuid.UUID) (user.User, error) {
r.mu.RLock()
defer r.mu.RUnlock()
u, ok := r.byID[id]
if !ok {
return user.User{}, user.ErrNotFound
}
return u, nil
}
func (r *InMemUsers) ExistingIDs(_ context.Context, ids []uuid.UUID) ([]uuid.UUID, error) {
r.mu.RLock()
defer r.mu.RUnlock()
found := make([]uuid.UUID, 0, len(ids))
for _, id := range ids {
if _, ok := r.byID[id]; ok {
found = append(found, id)
}
}
return found, nil
}
+97
View File
@@ -0,0 +1,97 @@
package store_test
import (
"context"
"errors"
"sync"
"testing"
"github.com/google/uuid"
"messenger/internal/platform/ident"
"messenger/internal/user"
"messenger/internal/user/store"
)
func mustUser(t *testing.T, username string) user.User {
t.Helper()
u, err := user.New(ident.New(), username, "Display")
if err != nil {
t.Fatal(err)
}
return u
}
func TestCreateThenGetByID(t *testing.T) {
repo := store.NewInMemUsers()
ctx := context.Background()
u := mustUser(t, "alice")
if err := repo.Create(ctx, u); err != nil {
t.Fatalf("Create error: %v", err)
}
got, err := repo.GetByID(ctx, u.ID)
if err != nil {
t.Fatalf("GetByID error: %v", err)
}
if got != u {
t.Fatalf("GetByID = %+v, want %+v", got, u)
}
}
func TestGetByIDUnknownReturnsErrNotFound(t *testing.T) {
_, err := store.NewInMemUsers().GetByID(context.Background(), ident.New())
if !errors.Is(err, user.ErrNotFound) {
t.Fatalf("err = %v, want ErrNotFound", err)
}
}
func TestCreateRejectsDuplicateUsername(t *testing.T) {
repo := store.NewInMemUsers()
ctx := context.Background()
if err := repo.Create(ctx, mustUser(t, "alice")); err != nil {
t.Fatal(err)
}
err := repo.Create(ctx, mustUser(t, "alice"))
if !errors.Is(err, user.ErrUsernameTaken) {
t.Fatalf("err = %v, want ErrUsernameTaken", err)
}
}
func TestCreateIsAtomicUnderConcurrency(t *testing.T) {
repo := store.NewInMemUsers()
ctx := context.Background()
const workers = 50
var wg sync.WaitGroup
var mu sync.Mutex
created := 0
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := repo.Create(ctx, mustUser(t, "same_name")); err == nil {
mu.Lock()
created++
mu.Unlock()
}
}()
}
wg.Wait()
if created != 1 {
t.Fatalf("exactly one Create must win, got %d", created)
}
}
func TestExistingIDsReturnsOnlyKnownIDs(t *testing.T) {
repo := store.NewInMemUsers()
ctx := context.Background()
alice, bob := mustUser(t, "alice"), mustUser(t, "bob")
_ = repo.Create(ctx, alice)
_ = repo.Create(ctx, bob)
got, err := repo.ExistingIDs(ctx, []uuid.UUID{ident.New(), bob.ID, alice.ID, bob.ID})
if err != nil {
t.Fatal(err)
}
if len(got) != 3 || got[0] != bob.ID || got[1] != alice.ID || got[2] != bob.ID {
t.Fatalf("ExistingIDs must keep input order and drop unknown ids, got %v", got)
}
}