Files
sub2api/backend/internal/securityaudit/prompt_worker_wake_test.go
T
ychy7001 390ba9905a fix(securityaudit): 审计任务入队后立即唤醒本进程 worker
异步审计 worker 每 500ms 轮询一次数据库认领任务,本进程刚入队的任务
平均要等约 250ms 才被处理。入队成功和租约回收后改为立即通知本进程的
worker 尝试认领;worker 启动时先认领一次遗留任务。500ms 轮询保留不变,
继续兜底到期重试和其他副本入队的任务。
2026-10-26 17:31:33 +08:00

82 lines
2.8 KiB
Go

package securityaudit
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestEnqueuerNotifiesOnlyAfterPublish(t *testing.T) {
cases := []struct {
name string
publishErr error
want int
}{
{name: "published", want: 1},
{name: "publish failed", publishErr: errors.New("db down"), want: 0},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
repo := &fakeJobRepository{createJob: &Job{ID: 41}, publishErr: tc.publishErr}
enqueuer := NewEnqueuer(&fakeConfigStore{cfg: asyncConfig(), active: true}, repo, &fakePayloadStore{values: map[int64]string{}})
notified := 0
enqueuer.notify = func() { notified++ }
_ = enqueuer.Enqueue(context.Background(), asyncRequest())
require.Equal(t, tc.want, notified)
})
}
}
// Both cases must finish before the first fallback tick (workerIdlePoll), so they
// exercise the start-up drain and the wake path rather than polling.
func TestWorkerClaimsWithoutWaitingForIdlePoll(t *testing.T) {
newRunner := func(repo *fakeJobRepository) *Runner {
payload := &fakePayloadStore{values: map[int64]string{51: "abc"}}
return NewRunner(&fakeConfigStore{cfg: asyncConfig(), active: true}, repo, payload, PromptScannerFunc(func(context.Context, ActiveEndpoint, string, []string) (*NormalizedResult, error) {
return integrationResult(EventPass), nil
}), NewAtomicMetrics())
}
completed := func(repo *fakeJobRepository) func() bool {
return func() bool { repo.mu.Lock(); defer repo.mu.Unlock(); return repo.completeCount == 1 }
}
stop := func(t *testing.T, runner *Runner) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, runner.Shutdown(ctx))
}
t.Run("jobs left from a previous run are drained at start", func(t *testing.T) {
repo := &fakeJobRepository{claimQueue: []*Job{workerJob(1, 3)}}
runner := newRunner(repo)
require.NoError(t, runner.Start(context.Background()))
defer stop(t, runner)
require.Eventually(t, completed(repo), workerIdlePoll/2, 5*time.Millisecond)
})
t.Run("notify wakes an idle worker", func(t *testing.T) {
repo := &fakeJobRepository{}
runner := newRunner(repo)
require.NoError(t, runner.Start(context.Background()))
defer stop(t, runner)
// wait for the start-up drain to find an empty queue
require.Eventually(t, func() bool { repo.mu.Lock(); defer repo.mu.Unlock(); return repo.claims > 0 }, workerIdlePoll/2, time.Millisecond)
repo.mu.Lock()
repo.claimQueue = append(repo.claimQueue, workerJob(1, 3))
repo.mu.Unlock()
runner.Notify()
require.Eventually(t, completed(repo), workerIdlePoll/2, 5*time.Millisecond)
})
t.Run("notify never blocks", func(t *testing.T) {
runner := newRunner(&fakeJobRepository{})
for i := 0; i < 3; i++ {
runner.Notify()
}
var nilRunner *Runner
nilRunner.Notify()
})
}