Переведите приём и чтение вложений на асинхронный контракт
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
@@ -22,6 +23,7 @@ import (
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/pipeline"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
@@ -117,10 +119,10 @@ func TestAttachmentRealS3RoundTripValidationAndOutage(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res := ts.binary("POST", path, alice.token, "image/png", data, int64(len(data)))
|
||||
ts.expect(res, 201, "")
|
||||
ts.expect(res, 202, "")
|
||||
a := decode[attachment.Response](t, res)
|
||||
digest := sha256.Sum256(data)
|
||||
if a.SHA256 != hex.EncodeToString(digest[:]) || a.SizeBytes != int64(len(data)) || a.Filename != "фото.png" || a.Status != "ready" || a.ErrorCode != nil || len(a.Variants) != 0 {
|
||||
if a.SHA256 != hex.EncodeToString(digest[:]) || a.SizeBytes != int64(len(data)) || a.Filename != "фото.png" || a.Status != "queued" || a.ErrorCode != nil || len(a.Variants) != 0 {
|
||||
t.Fatalf("attachment: %+v", a)
|
||||
}
|
||||
base := "/api/v1/attachments/" + a.ID
|
||||
@@ -129,11 +131,9 @@ func TestAttachmentRealS3RoundTripValidationAndOutage(t *testing.T) {
|
||||
}
|
||||
ts.expect(ts.do("GET", base, bob.token, nil, nil), 200, "")
|
||||
got := ts.binary("GET", base+"/content", bob.token, "", nil, 0)
|
||||
ts.expect(got, 200, "")
|
||||
if !bytes.Equal(got.raw, data) {
|
||||
t.Fatal("download bytes changed")
|
||||
}
|
||||
ts.expect(ts.do("GET", base+"/content?variant=thumbnail", bob.token, nil, nil), 400, "invalid_request")
|
||||
ts.expect(got, 409, "conflict")
|
||||
ts.expect(ts.do("GET", base+"/content?variant=thumbnail", bob.token, nil, nil), 409, "conflict")
|
||||
ts.expect(ts.do("GET", base+"/content?variant=unknown", bob.token, nil, nil), 400, "invalid_request")
|
||||
msg := ts.do("POST", "/api/v1/chats/"+room.ID+"/messages", bob.token, map[string]any{"text": "", "attachment_ids": []string{a.ID}}, freshKey())
|
||||
ts.expect(msg, 201, "")
|
||||
if m := decode[chat.MessageResponse](t, msg); len(m.AttachmentIDs) != 1 || m.AttachmentIDs[0] != a.ID {
|
||||
@@ -168,7 +168,7 @@ func TestAttachmentRealS3RoundTripValidationAndOutage(t *testing.T) {
|
||||
if err := jpeg.Encode(&j, image.NewRGBA(image.Rect(0, 0, 5, 3)), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ts.expect(ts.binary("POST", path, bob.token, "image/jpeg", j.Bytes(), int64(j.Len())), 201, "")
|
||||
ts.expect(ts.binary("POST", path, bob.token, "image/jpeg", j.Bytes(), int64(j.Len())), 202, "")
|
||||
ts.expect(ts.binary("POST", path, eve.token, "image/png", data, int64(len(data))), 404, "not_found")
|
||||
// An interrupted read is never a 500 or a successfully published identity.
|
||||
before := 0
|
||||
@@ -183,6 +183,76 @@ func TestAttachmentRealS3RoundTripValidationAndOutage(t *testing.T) {
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM attachments`).Scan(&after); err != nil || before != after {
|
||||
t.Fatalf("interruption published identity: %d -> %d (%v)", before, after, err)
|
||||
}
|
||||
// Complete one claimed job with known bytes. Image encoding has its own
|
||||
// tests; this checks the published version, exact download and ACL on S3.
|
||||
repo := pipeline.NewStore(db.Pool)
|
||||
var job pipeline.Job
|
||||
found, err := repo.PublishOne(ctx, func(_ context.Context, j pipeline.Job) error { job = j; return nil })
|
||||
if err != nil || !found || job.AttachmentID.String() != a.ID {
|
||||
t.Fatalf("outbox: %+v %v", job, err)
|
||||
}
|
||||
claim, err := repo.Claim(ctx, job, time.Minute)
|
||||
if err != nil || claim == nil {
|
||||
t.Fatal("claim", err)
|
||||
}
|
||||
meta := decode[attachment.Response](t, ts.do("GET", base, bob.token, nil, nil))
|
||||
if meta.Status != "processing" || len(meta.Variants) != 0 {
|
||||
t.Fatal("processing snapshot", meta)
|
||||
}
|
||||
if err := s3.Store.Copy(ctx, claim.SourceKey, attachment.BlobKey(claim.Hash)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bodies := map[string][]byte{"original": data}
|
||||
var outputs []pipeline.Variant
|
||||
for _, name := range []string{"thumbnail", "crop", "optimized"} {
|
||||
body := []byte("published-" + name)
|
||||
mime := "image/jpeg"
|
||||
if name == "optimized" {
|
||||
mime = "image/webp"
|
||||
}
|
||||
digest := sha256.Sum256(body)
|
||||
key := pipeline.VariantKey(claim.AttachmentID, claim.PipelineVersion, name, digest[:])
|
||||
if err := s3.Store.Put(ctx, key, bytes.NewReader(body), int64(len(body)), mime); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outputs = append(outputs, pipeline.Variant{Name: name, Key: key, MIMEType: mime, Width: 1, Height: 1, SizeBytes: int64(len(body)), SHA256: digest[:]})
|
||||
bodies[name] = body
|
||||
}
|
||||
if ok, err := repo.Complete(ctx, claim, outputs); err != nil || !ok {
|
||||
t.Fatal("publish", err)
|
||||
}
|
||||
if err := repo.Reprocess(ctx, claim.AttachmentID, 2, func(ctx context.Context, key string) (bool, error) {
|
||||
_, err := s3.Store.Stat(ctx, key)
|
||||
if errors.Is(err, attachment.ErrObjectMissing) {
|
||||
return false, nil
|
||||
}
|
||||
return err == nil, err
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
meta = decode[attachment.Response](t, ts.do("GET", base, bob.token, nil, nil))
|
||||
if meta.Status != "ready" || len(meta.Variants) != 3 || meta.ErrorCode != nil || meta.SHA256 != a.SHA256 {
|
||||
t.Fatalf("ready snapshot during reprocess: %+v", meta)
|
||||
}
|
||||
for _, v := range meta.Variants {
|
||||
digest := sha256.Sum256(bodies[v.Name])
|
||||
if v.SHA256 != hex.EncodeToString(digest[:]) || v.SizeBytes != int64(len(bodies[v.Name])) {
|
||||
t.Fatal("variant checksum", v)
|
||||
}
|
||||
}
|
||||
for name, body := range bodies {
|
||||
target := base + "/content?variant=" + name
|
||||
ts.expect(ts.do("GET", target, eve.token, nil, nil), 404, "not_found")
|
||||
got := ts.binary("GET", target, bob.token, "", nil, 0)
|
||||
ts.expect(got, 200, "")
|
||||
if !bytes.Equal(got.raw, body) {
|
||||
t.Fatalf("wrong published bytes for %s", name)
|
||||
}
|
||||
}
|
||||
if err := s3.Store.Delete(ctx, outputs[0].Key); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ts.expect(ts.do("GET", base+"/content?variant="+outputs[0].Name, bob.token, nil, nil), 404, "not_found")
|
||||
if err := s3.Stop(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user