Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBatchImageWorkerRuntime_QueueDisabledDoesNotStart(t *testing.T) {
|
|
queue := &blockingBatchImageRuntimeQueue{}
|
|
runtime := NewBatchImageWorkerRuntime(
|
|
NewBatchImageWorker(queue, &fakeBatchImageProcessor{}, BatchImageWorkerOptions{}),
|
|
&config.Config{BatchImage: config.BatchImageConfig{QueueEnabled: false}},
|
|
)
|
|
|
|
runtime.Start()
|
|
|
|
require.False(t, runtime.Running())
|
|
require.Zero(t, queue.reserveCalls.Load())
|
|
require.NotPanics(t, runtime.Stop)
|
|
}
|
|
|
|
func TestBatchImageWorkerRuntime_QueueEnabledStartsAndStops(t *testing.T) {
|
|
queue := &blockingBatchImageRuntimeQueue{}
|
|
processor := &fakeBatchImageProcessor{}
|
|
runtime := NewBatchImageWorkerRuntime(
|
|
NewBatchImageWorker(queue, processor, BatchImageWorkerOptions{
|
|
DelayedPollInterval: time.Hour,
|
|
RecoveryInterval: time.Hour,
|
|
}),
|
|
&config.Config{BatchImage: config.BatchImageConfig{QueueEnabled: true}},
|
|
)
|
|
|
|
runtime.Start()
|
|
|
|
require.Eventually(t, func() bool {
|
|
return runtime.Running() && queue.reserveCalls.Load() > 0
|
|
}, time.Second, 10*time.Millisecond)
|
|
require.Empty(t, processor.processed)
|
|
require.NotPanics(t, runtime.Stop)
|
|
require.False(t, runtime.Running())
|
|
require.NotPanics(t, runtime.Stop)
|
|
}
|
|
|
|
type blockingBatchImageRuntimeQueue struct {
|
|
reserveCalls atomic.Int64
|
|
}
|
|
|
|
func (q *blockingBatchImageRuntimeQueue) Enqueue(context.Context, string) error {
|
|
return nil
|
|
}
|
|
|
|
func (q *blockingBatchImageRuntimeQueue) Reserve(ctx context.Context, _ time.Duration) (ReservedBatchImageJob, error) {
|
|
q.reserveCalls.Add(1)
|
|
<-ctx.Done()
|
|
return ReservedBatchImageJob{}, ctx.Err()
|
|
}
|
|
|
|
func (q *blockingBatchImageRuntimeQueue) RequeueAfter(context.Context, string, time.Duration) error {
|
|
return nil
|
|
}
|
|
|
|
func (q *blockingBatchImageRuntimeQueue) Ack(context.Context, string) error {
|
|
return nil
|
|
}
|
|
|
|
func (q *blockingBatchImageRuntimeQueue) Heartbeat(context.Context, string) error {
|
|
return nil
|
|
}
|
|
|
|
func (q *blockingBatchImageRuntimeQueue) MoveDueDelayedToReady(context.Context, int) (int, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
func (q *blockingBatchImageRuntimeQueue) RecoverStaleActive(context.Context, time.Duration, int) (int, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
func (q *blockingBatchImageRuntimeQueue) TryAcquireJobLock(context.Context, string, time.Duration) (BatchImageJobLock, bool, error) {
|
|
return nil, false, nil
|
|
}
|