forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store"
|
||||
)
|
||||
|
||||
// TestStore_UsersExist checks all-members semantics on an independent store per case.
|
||||
func TestStore_UsersExist(t *testing.T) {
|
||||
t.Run("Positive: existing user", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newTestStore(t, config.Default().Memory)
|
||||
user := seedUser(t, repo, "alice")
|
||||
ids := []uuid.UUID{user.ID}
|
||||
want := true
|
||||
|
||||
// Act.
|
||||
got, err := repo.UsersExist(t.Context(), ids)
|
||||
|
||||
// Assert.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("UsersExist(%v)=%v, want=%v", ids, got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Positive: duplicate IDs", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newTestStore(t, config.Default().Memory)
|
||||
user := seedUser(t, repo, "alice")
|
||||
ids := []uuid.UUID{user.ID}
|
||||
want := true
|
||||
|
||||
ids = append(ids, user.ID)
|
||||
|
||||
// Act.
|
||||
got, err := repo.UsersExist(t.Context(), ids)
|
||||
|
||||
// Assert.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("UsersExist(%v)=%v, want=%v", ids, got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Positive: empty set", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newTestStore(t, config.Default().Memory)
|
||||
var ids []uuid.UUID
|
||||
want := true
|
||||
|
||||
// Act.
|
||||
got, err := repo.UsersExist(t.Context(), ids)
|
||||
|
||||
// Assert.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("UsersExist(%v)=%v, want=%v", ids, got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: missing member", func(t *testing.T) {
|
||||
// Arrange.
|
||||
repo := newTestStore(t, config.Default().Memory)
|
||||
user := seedUser(t, repo, "alice")
|
||||
ids := []uuid.UUID{user.ID}
|
||||
want := false
|
||||
|
||||
ids = append(ids, uuid.New())
|
||||
|
||||
// Act.
|
||||
got, err := repo.UsersExist(t.Context(), ids)
|
||||
|
||||
// Assert.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("UsersExist(%v)=%v, want=%v", ids, got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestStore_CreateUser_Capacity checks that a refused user consumes no storage or username index.
|
||||
func TestStore_CreateUser_Capacity(t *testing.T) {
|
||||
// Arrange.
|
||||
cfg := config.Default().Memory
|
||||
cfg.MaxUsers = 1
|
||||
repo := newTestStore(t, cfg)
|
||||
original := seedUser(t, repo, "alice")
|
||||
rejected := dto.UserDTO{ID: uuid.New(), Username: "bob"}
|
||||
previousBytes := repo.usedBytes
|
||||
|
||||
// Act.
|
||||
got, err := repo.CreateUser(t.Context(), rejected)
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, store.ErrCapacity) || got != (dto.UserDTO{}) {
|
||||
t.Fatalf("result=%+v, error=%v", got, err)
|
||||
}
|
||||
if len(repo.users) != 1 || repo.users[original.ID] != original || len(repo.usernames) != 1 || repo.usernames[original.Username] != original.ID || repo.usedBytes != previousBytes {
|
||||
t.Fatal("refused registration changed storage or indexes")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user