185 lines
7.6 KiB
Go
185 lines
7.6 KiB
Go
package practice
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
func TestExecutorNeverTurnsAnInfrastructureFailureIntoAVerdict(t *testing.T) {
|
|
now := time.Date(2026, 10, 11, 12, 0, 0, 0, time.UTC)
|
|
cases := []struct {
|
|
name string
|
|
submitErr error
|
|
attempts int
|
|
wantOutcome ExecutionOutcome
|
|
wantGeneration uint32
|
|
wantFailed outbound.PracticeJobErrorClass
|
|
}{
|
|
{name: "unreachable runner keeps the run key", submitErr: outbound.ErrCodeRunnerBadResponse,
|
|
wantOutcome: OutcomeRetry, wantGeneration: 2},
|
|
{name: "infrastructure failure starts a new run", submitErr: outbound.ErrCodeRunnerInfrastructureFailure,
|
|
wantOutcome: OutcomeRetry, wantGeneration: 3},
|
|
{name: "reused key starts a new run", submitErr: outbound.ErrCodeRunKeyReused,
|
|
wantOutcome: OutcomeRetry, wantGeneration: 3},
|
|
{name: "invalid request fails without a verdict", submitErr: outbound.ErrInvalidCodeRunRequest,
|
|
wantOutcome: OutcomeFailed, wantFailed: outbound.PracticeJobInvalidRequest},
|
|
{name: "last attempt fails without a verdict", submitErr: outbound.ErrCodeRunnerUnavailable, attempts: 2,
|
|
wantOutcome: OutcomeFailed, wantFailed: outbound.PracticeJobRunnerUnavailable},
|
|
{name: "exhausted takeover fails at once", attempts: 3,
|
|
wantOutcome: OutcomeFailed, wantFailed: outbound.PracticeJobLeaseExpired},
|
|
}
|
|
for _, testCase := range cases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
store := &fakeJobStore{}
|
|
runner := &fakeRunner{submitErr: testCase.submitErr}
|
|
executor := testExecutor(t, runner, store, now)
|
|
job := outbound.ClaimedPracticeJob{SubmissionID: uuid.New(), Generation: 2, Attempts: testCase.attempts, StartedAt: now}
|
|
outcome, err := executor.Execute(t.Context(), job)
|
|
if err != nil || outcome != testCase.wantOutcome {
|
|
t.Fatalf("Execute() = %q, error %v; want %q", outcome, err, testCase.wantOutcome)
|
|
}
|
|
if store.completed {
|
|
t.Fatal("an infrastructure failure stored a result")
|
|
}
|
|
if testCase.wantFailed != "" && store.failedWith != testCase.wantFailed {
|
|
t.Fatalf("failed with %q, want %q", store.failedWith, testCase.wantFailed)
|
|
}
|
|
if testCase.wantOutcome == OutcomeRetry {
|
|
if !store.requeue.Counted || store.requeue.Generation != testCase.wantGeneration ||
|
|
!store.requeue.AvailableAt.Equal(now.Add(time.Second)) {
|
|
t.Fatalf("requeue = %+v, want a counted try at generation %d after the first backoff", store.requeue, testCase.wantGeneration)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExecutorHandlesPollFailuresAndShutdown(t *testing.T) {
|
|
now := time.Date(2026, 10, 11, 12, 0, 0, 0, time.UTC)
|
|
store := &fakeJobStore{}
|
|
runner := &fakeRunner{pollErr: outbound.ErrCodeRunNotFound, submits: []outbound.CodeRunState{outbound.CodeRunRunning, outbound.CodeRunFinished}}
|
|
executor := testExecutor(t, runner, store, now)
|
|
job := outbound.ClaimedPracticeJob{SubmissionID: uuid.New(), StartedAt: now}
|
|
if outcome, err := executor.Execute(t.Context(), job); err != nil || outcome != OutcomeFinished || !store.completed {
|
|
t.Fatalf("Execute() = %q, error %v, completed %v; want the resubmitted run finished", outcome, err, store.completed)
|
|
}
|
|
if runner.submitCalls != 2 || runner.pollCalls != 1 {
|
|
t.Fatalf("submits = %d, polls = %d; want a resubmit after the lost run", runner.submitCalls, runner.pollCalls)
|
|
}
|
|
|
|
store = &fakeJobStore{}
|
|
runner = &fakeRunner{pollErr: outbound.ErrCodeRunnerInfrastructureFailure, submits: []outbound.CodeRunState{outbound.CodeRunRunning}}
|
|
executor = testExecutor(t, runner, store, now)
|
|
if outcome, err := executor.Execute(t.Context(), job); err != nil || outcome != OutcomeRetry || store.requeue.Generation != job.Generation+1 {
|
|
t.Fatalf("Execute() after a failed poll = %q, error %v, requeue %+v; want a retry on a new generation", outcome, err, store.requeue)
|
|
}
|
|
|
|
store = &fakeJobStore{}
|
|
runner = &fakeRunner{submits: []outbound.CodeRunState{outbound.CodeRunRunning}}
|
|
executor = testExecutor(t, runner, store, now)
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
cancel()
|
|
if outcome, err := executor.Execute(ctx, job); err != nil || outcome != OutcomeReleased {
|
|
t.Fatalf("Execute() at shutdown = %q, error %v; want released", outcome, err)
|
|
}
|
|
if store.requeue.Counted || store.requeue.Generation != job.Generation {
|
|
t.Fatalf("release = %+v, want an uncounted requeue on the same key", store.requeue)
|
|
}
|
|
}
|
|
|
|
func testExecutor(t *testing.T, runner outbound.CodeRunner, store outbound.PracticeJobStore, now time.Time) *Executor {
|
|
t.Helper()
|
|
executor, err := NewExecutor(runner, store, func() time.Time { return now }, ExecutionSettings{
|
|
PollInterval: time.Millisecond, Lease: time.Minute, RunTimeout: time.Hour, MaxAttempts: 3,
|
|
BackoffInitial: time.Second, BackoffMax: 4 * time.Second, StoreTimeout: time.Second,
|
|
}, slog.New(slog.DiscardHandler))
|
|
if err != nil {
|
|
t.Fatalf("NewExecutor() error = %v", err)
|
|
}
|
|
return executor
|
|
}
|
|
|
|
type fakeRunner struct {
|
|
submitErr error
|
|
pollErr error
|
|
submits []outbound.CodeRunState
|
|
submitCalls int
|
|
pollCalls int
|
|
}
|
|
|
|
func (runner *fakeRunner) Submit(_ context.Context, request outbound.CodeRunRequest) (outbound.CodeRunStatus, error) {
|
|
runner.submitCalls++
|
|
if runner.submitErr != nil {
|
|
return outbound.CodeRunStatus{}, runner.submitErr
|
|
}
|
|
state := runner.submits[min(runner.submitCalls, len(runner.submits))-1]
|
|
return fakeStatus(state, request), nil
|
|
}
|
|
|
|
func (runner *fakeRunner) Poll(_ context.Context, request outbound.CodeRunRequest) (outbound.CodeRunStatus, error) {
|
|
runner.pollCalls++
|
|
if runner.pollErr != nil {
|
|
return outbound.CodeRunStatus{}, runner.pollErr
|
|
}
|
|
return fakeStatus(outbound.CodeRunFinished, request), nil
|
|
}
|
|
|
|
func fakeStatus(state outbound.CodeRunState, request outbound.CodeRunRequest) outbound.CodeRunStatus {
|
|
if state != outbound.CodeRunFinished {
|
|
return outbound.CodeRunStatus{State: state}
|
|
}
|
|
result := outbound.CodeRunResult{Outcome: outbound.CodeOutcomeAccepted}
|
|
for _, group := range request.Groups {
|
|
result.Groups = append(result.Groups, outbound.CodeGroupResult{
|
|
ID: group.ID, Visibility: group.Visibility, Outcome: outbound.CodeOutcomeAccepted,
|
|
PassedTests: len(group.Tests), TotalTests: len(group.Tests),
|
|
})
|
|
}
|
|
return outbound.CodeRunStatus{State: state, Result: &result}
|
|
}
|
|
|
|
type fakeJobStore struct {
|
|
requeue outbound.PracticeRequeue
|
|
failedWith outbound.PracticeJobErrorClass
|
|
completed bool
|
|
}
|
|
|
|
func (store *fakeJobStore) Claim(context.Context, outbound.PracticeJobClaim) (outbound.ClaimedPracticeJob, bool, error) {
|
|
return outbound.ClaimedPracticeJob{}, false, errors.New("not used")
|
|
}
|
|
|
|
func (store *fakeJobStore) Work(_ context.Context, job outbound.ClaimedPracticeJob) (outbound.PracticeJobWork, error) {
|
|
return outbound.PracticeJobWork{Task: testTask(), Submission: domain.PracticeSubmission{
|
|
ID: job.SubmissionID, PracticeID: uuid.New(), UserID: uuid.New(), Mode: domain.PracticeModeFinal,
|
|
Language: domain.TaskLanguagePython, Source: "print(1)\n", CreatedAt: time.Now(),
|
|
}}, nil
|
|
}
|
|
|
|
func (store *fakeJobStore) Extend(_ context.Context, job outbound.ClaimedPracticeJob, expiresAt time.Time) (outbound.ClaimedPracticeJob, bool, error) {
|
|
job.LeaseExpiresAt = expiresAt
|
|
return job, true, nil
|
|
}
|
|
|
|
func (store *fakeJobStore) Requeue(_ context.Context, _ outbound.ClaimedPracticeJob, requeue outbound.PracticeRequeue) (bool, error) {
|
|
store.requeue = requeue
|
|
return true, nil
|
|
}
|
|
|
|
func (store *fakeJobStore) Complete(context.Context, outbound.ClaimedPracticeJob, domain.PracticeRunResult) (bool, error) {
|
|
store.completed = true
|
|
return true, nil
|
|
}
|
|
|
|
func (store *fakeJobStore) Fail(_ context.Context, _ outbound.ClaimedPracticeJob, class outbound.PracticeJobErrorClass) (bool, error) {
|
|
store.failedWith = class
|
|
return true, nil
|
|
}
|