forked from mtusi2026/mtusi2026lab5
53 lines
1.9 KiB
Go
53 lines
1.9 KiB
Go
package postgres
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/config"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/media"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/objectstore"
|
|
)
|
|
|
|
// TestMeasuredMissingInventory measures bounded real PostgreSQL/S3 scans with injected missing originals.
|
|
func TestMeasuredMissingInventory(t *testing.T) {
|
|
if os.Getenv("S3_ENDPOINT") == "" {
|
|
t.Skip("isolated S3 required for measured reconciliation")
|
|
}
|
|
for _, size := range []struct {
|
|
name string
|
|
count int
|
|
}{{"100 records", 100}, {"1000 records", 1000}} {
|
|
t.Run(size.name, func(t *testing.T) {
|
|
s := testStore(t)
|
|
user := fixtureUser(t, s)
|
|
chat := fixtureChat(t, s, user)
|
|
_, err := s.pool.Exec(t.Context(), `WITH ids AS (SELECT gen_random_uuid() AS id FROM generate_series(1,@count::int))
|
|
INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,status,object_key,created_at,updated_at)
|
|
SELECT id,@chat,@owner,'synthetic.png','image/png',32,'synthetic','ready','original/'||id::text,clock_timestamp()-interval '2 hours',clock_timestamp() FROM ids`, pgx.StrictNamedArgs{"count": size.count, "chat": chat.ID, "owner": user.ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := config.NewObjectStore()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
objects, err := objectstore.New(cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
started := time.Now()
|
|
result, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, false)
|
|
elapsed := time.Since(started)
|
|
if err != nil || result.Scanned != size.count || result.Missing != size.count || result.Changed != 0 {
|
|
t.Fatalf("inventory=%+v err=%v", result, err)
|
|
}
|
|
t.Logf("records=%d missing=%d elapsed_ms=%.3f apply=false", result.Scanned, result.Missing, float64(elapsed.Microseconds())/1000)
|
|
})
|
|
}
|
|
}
|