package worker import ( "context" "errors" "sync/atomic" "testing" "time" "github.com/stretchr/testify/require" "gitea.privcloud.tech/dizi/mtusi2026lab6/internal/messenger/attachment/transform" "gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model" "gitea.privcloud.tech/dizi/mtusi2026lab6/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 TestLeaseHeartbeatRenewsUntilCanceled(t *testing.T) { var renewals atomic.Int32 repository := &repositoryStub{renew: func(context.Context, model.AttachmentJobLease, time.Duration) (bool, error) { renewals.Add(1) return true, nil }} config := validConfig() config.HeartbeatInterval = 5 * time.Millisecond worker := &Worker{repository: repository, config: config} ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) go func() { done <- worker.maintainLease(ctx, cancel, testLease()) }() require.Eventually(t, func() bool { return renewals.Load() >= 2 }, time.Second, time.Millisecond) cancel() require.NoError(t, <-done) } func TestLeaseHeartbeatCancelsProcessingWhenLeaseIsLost(t *testing.T) { repository := &repositoryStub{renew: func(context.Context, model.AttachmentJobLease, time.Duration) (bool, error) { return false, nil }} config := validConfig() config.HeartbeatInterval = time.Millisecond worker := &Worker{repository: repository, config: config} ctx, cancel := context.WithCancel(context.Background()) err := worker.maintainLease(ctx, cancel, testLease()) require.ErrorIs(t, err, model.ErrStaleLease) require.ErrorIs(t, ctx.Err(), context.Canceled) } type repositoryStub struct { Repository renew func(context.Context, model.AttachmentJobLease, time.Duration) (bool, error) } func (s *repositoryStub) RenewAttachmentJobLease( ctx context.Context, lease model.AttachmentJobLease, duration time.Duration, ) (bool, error) { return s.renew(ctx, lease, duration) } func testLease() model.AttachmentJobLease { return model.AttachmentJobLease{Job: model.AttachmentJob{ ID: "11111111-1111-4111-8111-111111111111", LeaseToken: "lease-token", LeaseOwner: "worker-1", }} } func validConfig() Config { return Config{ WorkerID: "worker-1", Concurrency: 2, LeaseDuration: 30 * time.Second, HeartbeatInterval: 5 * 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, } }