lab5: асинхронная очередь, worker и идемпотентный pipeline
API принимает 202 после durable staging и TX intent (job+outbox); отдельные relay и worker публикуют/обрабатывают задания с publisher confirm, lease, retry/backoff, DLQ и replay. Добавлены версии pipeline, варианты thumbnail/crop/optimized, presign в verified staging, status-aware sweeper и admission control.
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
package imageproc_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gen2brain/webp"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/imageproc"
|
||||
)
|
||||
|
||||
func writePNG(t *testing.T, width, height int, alpha bool) string {
|
||||
t.Helper()
|
||||
|
||||
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
for x := range width {
|
||||
for y := range height {
|
||||
a := uint8(255)
|
||||
if alpha && x < width/2 && y < height/2 {
|
||||
a = 0
|
||||
}
|
||||
img.Set(x, y, color.NRGBA{R: 200, G: 40, B: 20, A: a})
|
||||
}
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
require.NoError(t, png.Encode(&buf, img))
|
||||
|
||||
path := filepath.Join(t.TempDir(), "source.png")
|
||||
require.NoError(t, os.WriteFile(path, buf.Bytes(), 0o600))
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
func writeJPEG(t *testing.T, width, height, orientation int) string {
|
||||
t.Helper()
|
||||
|
||||
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
for x := range width {
|
||||
for y := range height {
|
||||
img.Set(x, y, color.RGBA{R: 200, G: 40, B: 20, A: 255})
|
||||
}
|
||||
}
|
||||
|
||||
var raw bytes.Buffer
|
||||
require.NoError(t, jpeg.Encode(&raw, img, &jpeg.Options{Quality: 95}))
|
||||
|
||||
data := append([]byte(nil), raw.Bytes()...)
|
||||
if orientation > 1 {
|
||||
data = insertEXIFOrientation(t, data, uint16(orientation))
|
||||
}
|
||||
|
||||
path := filepath.Join(t.TempDir(), "source.jpg")
|
||||
require.NoError(t, os.WriteFile(path, data, 0o600))
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
func insertEXIFOrientation(t *testing.T, jpegBytes []byte, orientation uint16) []byte {
|
||||
t.Helper()
|
||||
|
||||
// TIFF: II, 42, IFD0 at offset 8, one entry (0x0112 SHORT 1 = orientation).
|
||||
tiff := make([]byte, 8+2+12+4)
|
||||
binary.LittleEndian.PutUint16(tiff[0:2], 0x4949)
|
||||
binary.LittleEndian.PutUint16(tiff[2:4], 42)
|
||||
binary.LittleEndian.PutUint32(tiff[4:8], 8)
|
||||
binary.LittleEndian.PutUint16(tiff[8:10], 1)
|
||||
binary.LittleEndian.PutUint16(tiff[10:12], 0x0112)
|
||||
binary.LittleEndian.PutUint16(tiff[12:14], 3)
|
||||
binary.LittleEndian.PutUint32(tiff[14:18], 1)
|
||||
binary.LittleEndian.PutUint16(tiff[18:20], orientation)
|
||||
|
||||
payload := append([]byte("Exif\x00\x00"), tiff...)
|
||||
segment := make([]byte, 4+len(payload))
|
||||
segment[0] = 0xff
|
||||
segment[1] = 0xe1
|
||||
binary.BigEndian.PutUint16(segment[2:4], uint16(len(payload)+2))
|
||||
copy(segment[4:], payload)
|
||||
|
||||
// Insert immediately after SOI (before the first DQT/SOF).
|
||||
out := append([]byte(nil), jpegBytes[:2]...)
|
||||
out = append(out, segment...)
|
||||
out = append(out, jpegBytes[2:]...)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func decodeConfig(t *testing.T, data []byte) image.Config {
|
||||
t.Helper()
|
||||
|
||||
cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
require.NoError(t, err)
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func TestThumbnailContract(t *testing.T) {
|
||||
params, ok := imageproc.Pipeline(1)
|
||||
require.True(t, ok)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
width int
|
||||
height int
|
||||
expected [2]int
|
||||
}{
|
||||
{"320x200", 320, 200, [2]int{256, 160}},
|
||||
{"no upscale 64x64", 64, 64, [2]int{64, 64}},
|
||||
{"no upscale 64x40", 64, 40, [2]int{64, 40}},
|
||||
{"floor 1000x10", 1000, 10, [2]int{256, 2}},
|
||||
{"height limited", 100, 320, [2]int{80, 256}},
|
||||
{"minimum 1x1", 1, 1, [2]int{1, 1}},
|
||||
}
|
||||
|
||||
for _, test := range cases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
img, _, err := imageproc.Load(writePNG(t, test.width, test.height, false), 40_000_000)
|
||||
require.NoError(t, err)
|
||||
|
||||
thumb, err := imageproc.Thumbnail(img, params)
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg := decodeConfig(t, thumb.Data)
|
||||
require.Equal(t, test.expected[0], cfg.Width)
|
||||
require.Equal(t, test.expected[1], cfg.Height)
|
||||
require.Equal(t, test.expected[0], thumb.Width)
|
||||
require.Equal(t, test.expected[1], thumb.Height)
|
||||
require.Equal(t, "image/jpeg", thumb.ContentType)
|
||||
require.NotContains(t, string(thumb.Data), "Exif")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCropAndOptimized(t *testing.T) {
|
||||
params, ok := imageproc.Pipeline(1)
|
||||
require.True(t, ok)
|
||||
|
||||
img, _, err := imageproc.Load(writePNG(t, 320, 200, false), 40_000_000)
|
||||
require.NoError(t, err)
|
||||
|
||||
crop, err := imageproc.Crop(img, params)
|
||||
require.NoError(t, err)
|
||||
cfg := decodeConfig(t, crop.Data)
|
||||
require.Equal(t, 128, cfg.Width)
|
||||
require.Equal(t, 128, cfg.Height)
|
||||
|
||||
optimized, err := imageproc.Optimized(img, params)
|
||||
require.NoError(t, err)
|
||||
cfg = decodeConfig(t, optimized.Data)
|
||||
require.Equal(t, 320, cfg.Width)
|
||||
require.Equal(t, 200, cfg.Height)
|
||||
require.Equal(t, "image/webp", optimized.ContentType)
|
||||
|
||||
decoded, err := webp.Decode(bytes.NewReader(optimized.Data))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 320, decoded.Bounds().Dx())
|
||||
require.Equal(t, 200, decoded.Bounds().Dy())
|
||||
}
|
||||
|
||||
func TestEXIFOrientationChangesThumbnailDimensions(t *testing.T) {
|
||||
params, ok := imageproc.Pipeline(1)
|
||||
require.True(t, ok)
|
||||
|
||||
for _, orientation := range []int{6, 8} {
|
||||
img, _, err := imageproc.Load(writeJPEG(t, 320, 200, orientation), 40_000_000)
|
||||
require.NoError(t, err)
|
||||
|
||||
thumb, err := imageproc.Thumbnail(img, params)
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg := decodeConfig(t, thumb.Data)
|
||||
require.Equal(t, 160, cfg.Width, "orientation %d", orientation)
|
||||
require.Equal(t, 256, cfg.Height, "orientation %d", orientation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlphaCompositedOnWhite(t *testing.T) {
|
||||
params, ok := imageproc.Pipeline(1)
|
||||
require.True(t, ok)
|
||||
|
||||
img, _, err := imageproc.Load(writePNG(t, 100, 100, true), 40_000_000)
|
||||
require.NoError(t, err)
|
||||
|
||||
thumb, err := imageproc.Thumbnail(img, params)
|
||||
require.NoError(t, err)
|
||||
|
||||
decoded, err := jpeg.Decode(bytes.NewReader(thumb.Data))
|
||||
require.NoError(t, err)
|
||||
|
||||
r, g, b, _ := decoded.At(0, 0).RGBA()
|
||||
require.Greater(t, r, uint32(0xf000), "transparent corner must be white")
|
||||
require.Greater(t, g, uint32(0xf000), "transparent corner must be white")
|
||||
require.Greater(t, b, uint32(0xf000), "transparent corner must be white")
|
||||
}
|
||||
|
||||
func TestPipelineVersionsDiffer(t *testing.T) {
|
||||
v1, ok := imageproc.Pipeline(1)
|
||||
require.True(t, ok)
|
||||
v2, ok := imageproc.Pipeline(2)
|
||||
require.True(t, ok)
|
||||
require.NotEqual(t, v1, v2)
|
||||
|
||||
_, ok = imageproc.Pipeline(3)
|
||||
require.False(t, ok)
|
||||
}
|
||||
|
||||
func TestVariantNames(t *testing.T) {
|
||||
require.Equal(t, "image/jpeg", domain.VariantThumbnail.ContentType())
|
||||
require.Equal(t, "image/jpeg", domain.VariantCrop.ContentType())
|
||||
require.Equal(t, "image/webp", domain.VariantOptimized.ContentType())
|
||||
require.Equal(t, "thumbnail.jpg", domain.VariantThumbnail.ObjectName())
|
||||
require.Equal(t, "optimized.webp", domain.VariantOptimized.ObjectName())
|
||||
}
|
||||
Reference in New Issue
Block a user