forked from mtusi2026/mtusi2026lab5
lab5: очередь RabbitMQ, outbox, worker, варианты, fault-injection
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func solid(w, h int, c color.Color) *image.NRGBA {
|
||||
m := image.NewNRGBA(image.Rect(0, 0, w, h))
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
m.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func encPNG(t *testing.T, img image.Image) []byte {
|
||||
var b bytes.Buffer
|
||||
if err := png.Encode(&b, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func TestFitSize(t *testing.T) {
|
||||
cases := []struct {
|
||||
w, h, box int
|
||||
up bool
|
||||
ww, wh int
|
||||
}{
|
||||
{320, 200, 256, false, 256, 160}, // fixture курса
|
||||
{200, 320, 256, false, 160, 256},
|
||||
{100, 50, 256, false, 100, 50}, // без увеличения
|
||||
{5000, 1, 256, false, 256, 1}, // минимум 1 px
|
||||
{1, 5000, 256, false, 1, 256},
|
||||
{257, 255, 256, false, 256, 254}, // округление вниз
|
||||
{40, 30, 128, true, 128, 96},
|
||||
{3000, 2000, 1280, false, 1280, 853},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if w, h := FitSize(c.w, c.h, c.box, c.up); w != c.ww || h != c.wh {
|
||||
t.Errorf("FitSize(%d,%d,%d,%v)=%d×%d want %d×%d", c.w, c.h, c.box, c.up, w, h, c.ww, c.wh)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func jpegSize(t *testing.T, data []byte) (int, int) {
|
||||
cfg, err := jpeg.DecodeConfig(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return cfg.Width, cfg.Height
|
||||
}
|
||||
|
||||
func TestCourseFixtureThumbnail(t *testing.T) {
|
||||
data, err := os.ReadFile("../../tests/fixtures/sample.png")
|
||||
if err != nil {
|
||||
t.Skip("fixture not found")
|
||||
}
|
||||
img, err := Decode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
th, err := Thumbnail(img, ParamsFor(1, 80))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w, h := jpegSize(t, th.Data); w != 256 || h != 160 || th.Width != 256 || th.Height != 160 {
|
||||
t.Fatalf("thumbnail %d×%d (meta %d×%d), want 256×160", w, h, th.Width, th.Height)
|
||||
}
|
||||
again, _ := Thumbnail(img, ParamsFor(1, 80))
|
||||
if !bytes.Equal(th.Data, again.Data) {
|
||||
t.Error("thumbnail encoding is not deterministic")
|
||||
}
|
||||
cr, err := Crop(img, ParamsFor(1, 80))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w, h := jpegSize(t, cr.Data); w != 128 || h != 128 {
|
||||
t.Fatalf("crop %d×%d", w, h)
|
||||
}
|
||||
v2, _ := Thumbnail(img, ParamsFor(2, 80))
|
||||
if bytes.Equal(v2.Data, th.Data) {
|
||||
t.Error("pipeline v2 must produce a different thumbnail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmallImageNotUpscaledButCropIs(t *testing.T) {
|
||||
img, err := Decode(encPNG(t, solid(40, 30, color.RGBA{10, 20, 30, 255})))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
th, _ := Thumbnail(img, ParamsFor(1, 80))
|
||||
if th.Width != 40 || th.Height != 30 {
|
||||
t.Errorf("small thumbnail upscaled to %d×%d", th.Width, th.Height)
|
||||
}
|
||||
cr, _ := Crop(img, ParamsFor(1, 80))
|
||||
if w, h := jpegSize(t, cr.Data); w != 128 || h != 128 {
|
||||
t.Errorf("crop of small image %d×%d", w, h)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlphaOnWhite(t *testing.T) {
|
||||
img, err := Decode(encPNG(t, solid(10, 10, color.NRGBA{0, 0, 0, 0})))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c := img.RGBAAt(5, 5); c.R != 255 || c.G != 255 || c.B != 255 {
|
||||
t.Errorf("transparent pixel composited to %v, want white", c)
|
||||
}
|
||||
half, _ := Decode(encPNG(t, solid(4, 4, color.NRGBA{0, 0, 0, 128})))
|
||||
if c := half.RGBAAt(1, 1); c.R < 120 || c.R > 135 {
|
||||
t.Errorf("50%% black over white = %v, want ~127", c)
|
||||
}
|
||||
}
|
||||
|
||||
// withExif вставляет APP1 Exif с orientation сразу после SOI.
|
||||
func withExif(jpg []byte, orientation uint16) []byte {
|
||||
var tiff bytes.Buffer
|
||||
tiff.WriteString("MM\x00\x2a")
|
||||
binary.Write(&tiff, binary.BigEndian, uint32(8))
|
||||
binary.Write(&tiff, binary.BigEndian, uint16(1))
|
||||
binary.Write(&tiff, binary.BigEndian, uint16(0x0112))
|
||||
binary.Write(&tiff, binary.BigEndian, uint16(3))
|
||||
binary.Write(&tiff, binary.BigEndian, uint32(1))
|
||||
binary.Write(&tiff, binary.BigEndian, orientation)
|
||||
tiff.Write([]byte{0, 0})
|
||||
binary.Write(&tiff, binary.BigEndian, uint32(0))
|
||||
seg := append([]byte("Exif\x00\x00"), tiff.Bytes()...)
|
||||
var out bytes.Buffer
|
||||
out.Write(jpg[:2])
|
||||
out.Write([]byte{0xFF, 0xE1})
|
||||
binary.Write(&out, binary.BigEndian, uint16(len(seg)+2))
|
||||
out.Write(seg)
|
||||
out.Write(jpg[2:])
|
||||
return out.Bytes()
|
||||
}
|
||||
|
||||
func TestExifOrientation(t *testing.T) {
|
||||
// 300×100: слева красный, справа синий.
|
||||
src := image.NewRGBA(image.Rect(0, 0, 300, 100))
|
||||
for y := 0; y < 100; y++ {
|
||||
for x := 0; x < 300; x++ {
|
||||
c := color.RGBA{255, 0, 0, 255}
|
||||
if x >= 150 {
|
||||
c = color.RGBA{0, 0, 255, 255}
|
||||
}
|
||||
src.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
var b bytes.Buffer
|
||||
jpeg.Encode(&b, src, &jpeg.Options{Quality: 95})
|
||||
data := withExif(b.Bytes(), 6) // 90° по часовой
|
||||
if o := ExifOrientation(data); o != 6 {
|
||||
t.Fatalf("orientation %d, want 6", o)
|
||||
}
|
||||
img, err := Decode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if img.Bounds().Dx() != 100 || img.Bounds().Dy() != 300 {
|
||||
t.Fatalf("rotated size %v, want 100×300", img.Bounds())
|
||||
}
|
||||
// После поворота на 90° по часовой левая (красная) половина оказывается сверху.
|
||||
if c := img.RGBAAt(50, 20); c.R < 200 || c.B > 60 {
|
||||
t.Errorf("top after rotation = %v, want red", c)
|
||||
}
|
||||
th, _ := Thumbnail(img, ParamsFor(1, 80))
|
||||
if th.Width != 85 || th.Height != 256 {
|
||||
t.Errorf("thumbnail of rotated %d×%d, want 85×256", th.Width, th.Height)
|
||||
}
|
||||
if ExifOrientation(b.Bytes()) != 1 {
|
||||
t.Error("no exif must mean orientation 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeGarbage(t *testing.T) {
|
||||
if _, err := Decode([]byte("not an image")); err == nil {
|
||||
t.Fatal("garbage decoded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptimizedWebP(t *testing.T) {
|
||||
if _, err := exec.LookPath("cwebp"); err != nil {
|
||||
t.Skip("cwebp not installed (worker image has libwebp-tools)")
|
||||
}
|
||||
img, _ := Decode(encPNG(t, solid(2000, 1000, color.RGBA{200, 100, 50, 255})))
|
||||
out, err := Optimized(context.Background(), img, ParamsFor(1, 80), CWebP)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out.Width != 1280 || out.Height != 640 || out.ContentType != "image/webp" {
|
||||
t.Fatalf("optimized %+v", out)
|
||||
}
|
||||
again, _ := Optimized(context.Background(), img, ParamsFor(1, 80), CWebP)
|
||||
if !bytes.Equal(out.Data, again.Data) {
|
||||
t.Error("cwebp output not deterministic")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user