Skip to content

Commit ad71599

Browse files
committed
improve TestJobConcurrency
1 parent 06b5215 commit ad71599

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

models/actions/run_job.go

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ func CancelPreviousJobsByConcurrency(ctx context.Context, job *ActionRunJob) err
237237
if err != nil {
238238
return fmt.Errorf("find previous jobs: %w", err)
239239
}
240+
previousJobs = slices.DeleteFunc(previousJobs, func(j *ActionRunJob) bool { return j.ID == job.ID })
240241
if err := CancelJobs(ctx, previousJobs); err != nil {
241242
return fmt.Errorf("cancel previous jobs: %w", err)
242243
}

services/actions/job_emitter.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func jobEmitterQueueHandler(items ...*jobUpdate) []*jobUpdate {
4040
var ret []*jobUpdate
4141
for _, update := range items {
4242
if err := checkJobsByRunID(ctx, update.RunID); err != nil {
43+
log.Error("check run %d: %v", update.RunID, err)
4344
ret = append(ret, update)
4445
}
4546
}

tests/integration/actions_concurrency_test.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,26 @@ jobs:
333333
steps:
334334
- run: echo 'wf2-job2'
335335
`
336+
wf3TreePath := ".gitea/workflows/concurrent-workflow-3.yml"
337+
wf3FileContent := `name: concurrent-workflow-3
338+
on:
339+
push:
340+
paths:
341+
- '.gitea/workflows/concurrent-workflow-3.yml'
342+
jobs:
343+
wf3-job1:
344+
runs-on: runner1
345+
concurrency:
346+
group: job-main-${{ vars.version_var }}
347+
cancel-in-progress: ${{ vars.version_var == 'v1.23.0' }}
348+
steps:
349+
- run: echo 'wf3-job1'
350+
`
336351

337352
opts1 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", wf1TreePath), wf1FileContent)
338353
createWorkflowFile(t, token, user2.Name, repo.Name, wf1TreePath, opts1)
339354
opts2 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", wf2TreePath), wf2FileContent)
340355
createWorkflowFile(t, token, user2.Name, repo.Name, wf2TreePath, opts2)
341-
// opts3 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", wf3TreePath), wf3FileContent)
342-
// createWorkflowFile(t, token, user2.Name, repo.Name, wf3TreePath, opts3)
343356

344357
// fetch wf1-job1
345358
wf1Job1Task := runner1.fetchTask(t)
@@ -361,10 +374,21 @@ jobs:
361374
result: runnerv1.Result_RESULT_SUCCESS,
362375
})
363376
// fetch wf2-job2
364-
wf2Job2Task := runner2.fetchTask(t)
377+
wf2Job2Task := runner1.fetchTask(t)
365378
_, wf2Job2ActionJob, _ := getTaskAndJobAndRunByTaskID(t, wf2Job2Task.Id)
366379
assert.Equal(t, "job-main-v1.23.0", wf2Job2ActionJob.ConcurrencyGroup)
367-
assert.True(t, wf1Job1ActionJob.Status.IsRunning())
380+
assert.True(t, wf2Job2ActionJob.Status.IsRunning())
381+
// push workflow3 to trigger wf3-job1
382+
opts3 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", wf3TreePath), wf3FileContent)
383+
createWorkflowFile(t, token, user2.Name, repo.Name, wf3TreePath, opts3)
384+
// fetch wf3-job1
385+
wf3Job1Task := runner1.fetchTask(t)
386+
_, wf3Job1ActionJob, _ := getTaskAndJobAndRunByTaskID(t, wf3Job1Task.Id)
387+
assert.Equal(t, "job-main-v1.23.0", wf3Job1ActionJob.ConcurrencyGroup)
388+
assert.True(t, wf3Job1ActionJob.Status.IsRunning())
389+
// wf2-job2 has been cancelled
390+
_, wf2Job2ActionJob, _ = getTaskAndJobAndRunByTaskID(t, wf2Job2Task.Id)
391+
assert.True(t, wf2Job2ActionJob.Status.IsCancelled())
368392

369393
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
370394
doAPIDeleteRepository(httpContext)(t)

0 commit comments

Comments
 (0)