forked from mtusi2026/mtusi2026lab5
fix: enforce pipeline lifecycle limits
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/messenger/attachment/transform"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/model"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/objectstore"
|
||||
)
|
||||
|
||||
@@ -46,11 +49,61 @@ func TestProcessingErrorClassification(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
ProcessingTimeout: 20 * time.Second, ReclaimInterval: 5 * time.Second,
|
||||
MaxAttempts: 5, RetryBase: time.Second, RetryMax: 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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user