Files
2026-08-25 17:59:42 +08:00

64 lines
2.0 KiB
Go

package worker
import (
"testing"
"juhe-factory/api/internal/model"
"github.com/google/uuid"
)
func TestSelectFairCandidatesHonorsTotalCapacity(t *testing.T) {
users := make([]uuid.UUID, 50)
for index := range users {
users[index] = uuid.New()
}
candidates := make([]model.GenerationTask, 0, 501)
for index := 0; index < 501; index++ {
candidates = append(candidates, model.GenerationTask{ID: uuid.New(), UserID: users[index%len(users)]})
}
selected := selectFairCandidates(candidates, map[uuid.UUID]int{}, 500, 10)
if len(selected) != 500 {
t.Fatalf("selected %d tasks, want 500", len(selected))
}
}
func TestSelectFairCandidatesHonorsUserCapacity(t *testing.T) {
userID := uuid.New()
candidates := make([]model.GenerationTask, 11)
for index := range candidates {
candidates[index] = model.GenerationTask{ID: uuid.New(), UserID: userID}
}
selected := selectFairCandidates(candidates, map[uuid.UUID]int{}, 500, 10)
if len(selected) != 10 {
t.Fatalf("selected %d tasks, want 10", len(selected))
}
}
func TestSelectFairCandidatesRoundRobin(t *testing.T) {
first, second := uuid.New(), uuid.New()
candidates := []model.GenerationTask{
{ID: uuid.New(), UserID: first}, {ID: uuid.New(), UserID: first},
{ID: uuid.New(), UserID: second}, {ID: uuid.New(), UserID: second},
}
selected := selectFairCandidates(candidates, map[uuid.UUID]int{}, 4, 10)
want := []uuid.UUID{first, second, first, second}
for index := range want {
if selected[index].UserID != want[index] {
t.Fatalf("position %d user %s, want %s", index, selected[index].UserID, want[index])
}
}
}
// TestCanRetrySubmission 验证提交超时会自动重试,并在第五次失败后停止。
func TestCanRetrySubmission(t *testing.T) {
for attempt := 1; attempt < maxSubmitAttempts; attempt++ {
if !canRetrySubmission(attempt) {
t.Fatalf("attempt %d should be retryable", attempt)
}
}
if canRetrySubmission(maxSubmitAttempts) {
t.Fatalf("attempt %d should reach retry limit", maxSubmitAttempts)
}
}