Files
mtusi2026lab5/internal/messenger/attachment/worker/worker_test.go
T

57 lines
1.8 KiB
Go

package worker
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/messenger/attachment/transform"
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/objectstore"
)
func TestConfigRequiresEnoughMemoryForMaximumImage(t *testing.T) {
config := validConfig()
require.NoError(t, validateConfig(config))
config.MemoryBytes = 384 * 1024 * 1024
require.ErrorContains(t, validateConfig(config), "memory must fit")
}
func TestRetryDelayIsBounded(t *testing.T) {
worker := &Worker{config: Config{RetryBase: time.Second, RetryMax: 4 * time.Second}}
for attempt := 1; attempt <= 10; attempt++ {
delay := worker.retryDelay("11111111-1111-4111-8111-111111111111", attempt)
require.GreaterOrEqual(t, delay, time.Second)
require.LessOrEqual(t, delay, 4*time.Second)
}
}
func TestProcessingErrorClassification(t *testing.T) {
tests := []struct {
err error
code string
permanent bool
}{
{err: transform.ErrInvalidImage, code: "invalid_image", permanent: true},
{err: transform.ErrPayloadTooLarge, code: "image_too_large", permanent: true},
{err: objectstore.ErrNotFound, code: "source_missing", permanent: true},
{err: errors.New("temporary S3 failure"), code: "processing_failed", permanent: false},
}
for _, test := range tests {
code, permanent := processingError(test.err)
require.Equal(t, test.code, code)
require.Equal(t, test.permanent, permanent)
}
}
func validConfig() Config {
return Config{
WorkerID: "worker-1", Concurrency: 2, LeaseDuration: 30 * time.Second,
ProcessingTimeout: 20 * time.Second, ReclaimInterval: 5 * time.Second,
MaxAttempts: 5, RetryBase: time.Second, RetryMax: 30 * time.Second,
MaxBytes: 10 * 1024 * 1024, MaxPixels: 40_000_000, MemoryBytes: 704 * 1024 * 1024,
}
}