package attachment import ( "context" "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/config" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgxpool" "os" "testing" "time" ) // The HTTP/S3 fault suite supplies real processing; this test isolates the DB fence. func TestFenceIntegration(t *testing.T) { if os.Getenv("INTEGRATION_TESTS") != "1" { t.Skip("run make test-integration") } cfg, e := config.Load() if e != nil { t.Fatal(e) } pc, e := pgxpool.ParseConfig("sslmode=disable") if e != nil { t.Fatal(e) } pc.ConnConfig.Host = cfg.PostgresHost pc.ConnConfig.Port = cfg.PostgresPort pc.ConnConfig.User = cfg.PostgresUser pc.ConnConfig.Password = cfg.PostgresPassword pc.ConnConfig.Database = cfg.PostgresDatabase pool, e := pgxpool.NewWithConfig(context.Background(), pc) if e != nil { t.Fatal(e) } defer pool.Close() ctx := context.Background() user, chat, att, id := uuid.NewString(), uuid.NewString(), uuid.NewString(), uuid.NewString() defer func() { pool.Exec(ctx, `DELETE FROM attachment_outbox WHERE job_id=$1`, id) pool.Exec(ctx, `DELETE FROM attachment_jobs WHERE id=$1`, id) pool.Exec(ctx, `DELETE FROM attachments WHERE id=$1`, att) pool.Exec(ctx, `DELETE FROM chat_members WHERE chat_id=$1`, chat) pool.Exec(ctx, `DELETE FROM chats WHERE id=$1`, chat) pool.Exec(ctx, `DELETE FROM users WHERE id=$1`, user) }() if _, e = pool.Exec(ctx, `INSERT INTO users(id,username,display_name,password_hash) VALUES($1,$2,'Fence','test')`, user, "fence_"+user[:8]); e != nil { t.Fatal(e) } if _, e = pool.Exec(ctx, `INSERT INTO chats(id,title) VALUES($1,'Fence')`, chat); e != nil { t.Fatal(e) } if _, e = pool.Exec(ctx, `INSERT INTO chat_members(chat_id,user_id,ordinal) VALUES($1,$2,0)`, chat, user); e != nil { t.Fatal(e) } if _, e = pool.Exec(ctx, `INSERT INTO attachments(id,chat_id,uploader_id,filename,object_key,content_type,size_bytes,sha256,width,height,status,staging_key) VALUES($1,$2,$3,'x.png',$4,'image/png',1,repeat('a',64),1,1,'queued',$5)`, att, chat, user, effectKey(att, 1, "original"), "staging/"+att); e != nil { t.Fatal(e) } if _, e = pool.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,state,max_attempts) VALUES($1,$2,'queued',5)`, id, att); e != nil { t.Fatal(e) } options, _ := LoadOptions() s := NewAsync(pool, nil, options) message := JobMessage{SchemaVersion: 1, JobID: id, AttachmentID: att, PipelineVersion: 1, Attempt: 1, CreatedAt: time.Now()} first, ok, e := s.claim(ctx, message) if e != nil || !ok { t.Fatalf("claim: %v %v", ok, e) } if _, ok, e = s.claim(ctx, message); e != nil || ok { t.Fatalf("duplicate claimed active lease: %v %v", ok, e) } if _, e = pool.Exec(ctx, `UPDATE attachment_jobs SET lease_until=clock_timestamp()-interval '1 second' WHERE id=$1`, id); e != nil { t.Fatal(e) } if durable, e := s.finish(ctx, first, []Variant{}, "", false); e != nil || durable { t.Fatalf("expired fence published: %v %v", durable, e) } if e = s.recover(ctx); e != nil { t.Fatal(e) } message.Attempt = 2 second, ok, e := s.claim(ctx, message) if e != nil || !ok || second.Fence <= first.Fence { t.Fatalf("reclaim: %+v %v %v", second, ok, e) } if durable, e := s.finish(ctx, first, []Variant{}, "", false); e != nil || durable { t.Fatalf("stale fence published: %v %v", durable, e) } if _, e = pool.Exec(ctx, `UPDATE attachments SET pipeline_version=2 WHERE id=$1`, att); e != nil { t.Fatal(e) } if durable, e := s.finish(ctx, second, []Variant{}, "", false); e == nil || durable { t.Fatal("stale pipeline version accepted") } var state string if e = pool.QueryRow(ctx, `SELECT state FROM attachment_jobs WHERE id=$1`, id).Scan(&state); e != nil || state != "processing" { t.Fatalf("partial result committed: %s %v", state, e) } }