Skip to content

Commit

Permalink
improve TestJobConcurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
Zettat123 committed Jan 15, 2025
1 parent 06b5215 commit ad71599
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions models/actions/run_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func CancelPreviousJobsByConcurrency(ctx context.Context, job *ActionRunJob) err
if err != nil {
return fmt.Errorf("find previous jobs: %w", err)
}
previousJobs = slices.DeleteFunc(previousJobs, func(j *ActionRunJob) bool { return j.ID == job.ID })
if err := CancelJobs(ctx, previousJobs); err != nil {
return fmt.Errorf("cancel previous jobs: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions services/actions/job_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func jobEmitterQueueHandler(items ...*jobUpdate) []*jobUpdate {
var ret []*jobUpdate
for _, update := range items {
if err := checkJobsByRunID(ctx, update.RunID); err != nil {
log.Error("check run %d: %v", update.RunID, err)
ret = append(ret, update)
}
}
Expand Down
32 changes: 28 additions & 4 deletions tests/integration/actions_concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,26 @@ jobs:
steps:
- run: echo 'wf2-job2'
`
wf3TreePath := ".gitea/workflows/concurrent-workflow-3.yml"
wf3FileContent := `name: concurrent-workflow-3
on:
push:
paths:
- '.gitea/workflows/concurrent-workflow-3.yml'
jobs:
wf3-job1:
runs-on: runner1
concurrency:
group: job-main-${{ vars.version_var }}
cancel-in-progress: ${{ vars.version_var == 'v1.23.0' }}
steps:
- run: echo 'wf3-job1'
`

opts1 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", wf1TreePath), wf1FileContent)
createWorkflowFile(t, token, user2.Name, repo.Name, wf1TreePath, opts1)
opts2 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", wf2TreePath), wf2FileContent)
createWorkflowFile(t, token, user2.Name, repo.Name, wf2TreePath, opts2)
// opts3 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", wf3TreePath), wf3FileContent)
// createWorkflowFile(t, token, user2.Name, repo.Name, wf3TreePath, opts3)

// fetch wf1-job1
wf1Job1Task := runner1.fetchTask(t)
Expand All @@ -361,10 +374,21 @@ jobs:
result: runnerv1.Result_RESULT_SUCCESS,
})
// fetch wf2-job2
wf2Job2Task := runner2.fetchTask(t)
wf2Job2Task := runner1.fetchTask(t)
_, wf2Job2ActionJob, _ := getTaskAndJobAndRunByTaskID(t, wf2Job2Task.Id)
assert.Equal(t, "job-main-v1.23.0", wf2Job2ActionJob.ConcurrencyGroup)
assert.True(t, wf1Job1ActionJob.Status.IsRunning())
assert.True(t, wf2Job2ActionJob.Status.IsRunning())
// push workflow3 to trigger wf3-job1
opts3 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", wf3TreePath), wf3FileContent)
createWorkflowFile(t, token, user2.Name, repo.Name, wf3TreePath, opts3)
// fetch wf3-job1
wf3Job1Task := runner1.fetchTask(t)
_, wf3Job1ActionJob, _ := getTaskAndJobAndRunByTaskID(t, wf3Job1Task.Id)
assert.Equal(t, "job-main-v1.23.0", wf3Job1ActionJob.ConcurrencyGroup)
assert.True(t, wf3Job1ActionJob.Status.IsRunning())
// wf2-job2 has been cancelled
_, wf2Job2ActionJob, _ = getTaskAndJobAndRunByTaskID(t, wf2Job2Task.Id)
assert.True(t, wf2Job2ActionJob.Status.IsCancelled())

httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
doAPIDeleteRepository(httpContext)(t)
Expand Down

0 comments on commit ad71599

Please sign in to comment.