Skip to content

Commit 19d3dcb

Browse files
committed
TestScheduleConcurrency
1 parent 3f839ff commit 19d3dcb

File tree

2 files changed

+74
-16
lines changed

2 files changed

+74
-16
lines changed

Diff for: tests/integration/actions_concurrency_test.go

+72-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
"code.gitea.io/gitea/models/unittest"
1818
user_model "code.gitea.io/gitea/models/user"
1919
api "code.gitea.io/gitea/modules/structs"
20+
"code.gitea.io/gitea/modules/timeutil"
2021
"code.gitea.io/gitea/modules/util"
22+
webhook_module "code.gitea.io/gitea/modules/webhook"
2123
actions_service "code.gitea.io/gitea/services/actions"
2224

2325
"github.com/stretchr/testify/assert"
@@ -470,7 +472,7 @@ jobs:
470472
"appVersion": "v1.22",
471473
})
472474
session.MakeRequest(t, req, http.StatusSeeOther)
473-
runner.fetchNoTask(t) // cannot fetch task since task2 is not completed
475+
runner.fetchNoTask(t) // cannot fetch task because task2 is not completed
474476

475477
// run the workflow with appVersion=v1.22 and cancel=true
476478
req = NewRequestWithValues(t, "POST", urlStr, map[string]string{
@@ -499,39 +501,95 @@ func TestScheduleConcurrency(t *testing.T) {
499501

500502
apiRepo := createActionsTestRepo(t, token, "actions-concurrency", false)
501503
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: apiRepo.ID})
504+
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
505+
defer doAPIDeleteRepository(httpContext)(t)
502506
runner := newMockRunner()
503507
runner.registerAsRepoRunner(t, user2.Name, repo.Name, "mock-runner", []string{"ubuntu-latest"})
504508

505-
// add a variable for test
506-
req := NewRequestWithJSON(t, "POST",
507-
fmt.Sprintf("/api/v1/repos/%s/%s/actions/variables/cancel_schedule", user2.Name, repo.Name), &api.CreateVariableOption{
508-
Value: "false",
509-
}).
510-
AddTokenAuth(token)
511-
MakeRequest(t, req, http.StatusNoContent)
512-
513509
wf1TreePath := ".gitea/workflows/schedule-concurrency.yml"
514510
wf1FileContent := `name: schedule-concurrency
515511
on:
512+
push:
516513
schedule:
517-
- cron: '0/5 * * * *'
514+
- cron: '@every 1m'
518515
concurrency:
519516
group: schedule-concurrency
520-
cancel-in-progress: ${{ vars.cancel_schedule == 'false' }}
517+
cancel-in-progress: ${{ gitea.event_name == 'push' }}
521518
jobs:
522519
job:
523520
runs-on: ubuntu-latest
524521
steps:
525-
- run: echo 'workflow dispatch job'
522+
- run: echo 'schedule workflow'
526523
`
527524

528525
opts1 := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", wf1TreePath), wf1FileContent)
529526
createWorkflowFile(t, token, user2.Name, repo.Name, wf1TreePath, opts1)
530527

528+
// fetch the task triggered by push
529+
task1 := runner.fetchTask(t)
530+
_, _, run1 := getTaskAndJobAndRunByTaskID(t, task1.Id)
531+
assert.Equal(t, "schedule-concurrency", run1.ConcurrencyGroup)
532+
assert.True(t, run1.ConcurrencyCancel)
533+
assert.Equal(t, string(webhook_module.HookEventPush), run1.TriggerEvent)
534+
assert.Equal(t, actions_model.StatusRunning, run1.Status)
535+
536+
// trigger the task by schedule
537+
spec := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionScheduleSpec{RepoID: repo.ID})
538+
spec.Next = timeutil.TimeStampNow() // manually update "Next"
539+
assert.NoError(t, actions_model.UpdateScheduleSpec(context.Background(), spec, "next"))
540+
assert.NoError(t, actions_service.StartScheduleTasks(context.Background()))
541+
runner.fetchNoTask(t) // cannot fetch because task1 is not completed
542+
runner.execTask(t, task1, &mockTaskOutcome{
543+
result: runnerv1.Result_RESULT_SUCCESS,
544+
})
545+
_, _, run1 = getTaskAndJobAndRunByTaskID(t, task1.Id)
546+
assert.Equal(t, actions_model.StatusSuccess, run1.Status)
547+
task2 := runner.fetchTask(t)
548+
_, _, run2 := getTaskAndJobAndRunByTaskID(t, task2.Id)
549+
assert.Equal(t, "schedule-concurrency", run2.ConcurrencyGroup)
550+
assert.False(t, run2.ConcurrencyCancel)
551+
assert.Equal(t, string(webhook_module.HookEventSchedule), run2.TriggerEvent)
552+
assert.Equal(t, actions_model.StatusRunning, run2.Status)
553+
554+
// trigger the task by schedule again
555+
spec = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionScheduleSpec{RepoID: repo.ID})
556+
spec.Next = timeutil.TimeStampNow() // manually update "Next"
557+
assert.NoError(t, actions_model.UpdateScheduleSpec(context.Background(), spec, "next"))
531558
assert.NoError(t, actions_service.StartScheduleTasks(context.Background()))
559+
runner.fetchNoTask(t) // cannot fetch because task2 is not completed
560+
run3 := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: repo.ID, Status: actions_model.StatusBlocked})
561+
assert.Equal(t, "schedule-concurrency", run3.ConcurrencyGroup)
562+
assert.False(t, run3.ConcurrencyCancel)
563+
assert.Equal(t, string(webhook_module.HookEventSchedule), run3.TriggerEvent)
564+
565+
// trigger the task by push
566+
doAPICreateFile(httpContext, "doc.txt", &api.CreateFileOptions{
567+
FileOptions: api.FileOptions{
568+
NewBranchName: "main",
569+
Message: "create doc.txt",
570+
Author: api.Identity{
571+
Name: user2.Name,
572+
Email: user2.Email,
573+
},
574+
Committer: api.Identity{
575+
Name: user2.Name,
576+
Email: user2.Email,
577+
},
578+
Dates: api.CommitDateOptions{
579+
Author: time.Now(),
580+
Committer: time.Now(),
581+
},
582+
},
583+
ContentBase64: base64.StdEncoding.EncodeToString([]byte("doc")),
584+
})(t)
532585

533-
httpContext := NewAPITestContext(t, user2.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository)
534-
doAPIDeleteRepository(httpContext)(t)
586+
task4 := runner.fetchTask(t)
587+
_, _, run4 := getTaskAndJobAndRunByTaskID(t, task4.Id)
588+
assert.Equal(t, "schedule-concurrency", run4.ConcurrencyGroup)
589+
assert.True(t, run4.ConcurrencyCancel)
590+
assert.Equal(t, string(webhook_module.HookEventPush), run4.TriggerEvent)
591+
run3 = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: run3.ID})
592+
assert.Equal(t, actions_model.StatusCancelled, run3.Status)
535593
})
536594
}
537595

Diff for: tests/integration/actions_runner_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (r *mockRunner) fetchNoTask(t *testing.T, timeout ...time.Duration) {
102102
assert.Nil(t, task, "a task is fetched")
103103
}
104104

105-
const defaultFetchTaskTimeout = 5 * time.Second
105+
const defaultFetchTaskTimeout = 1 * time.Second
106106

107107
func (r *mockRunner) tryFetchTask(t *testing.T, timeout ...time.Duration) *runnerv1.Task {
108108
fetchTimeout := defaultFetchTaskTimeout
@@ -120,7 +120,7 @@ func (r *mockRunner) tryFetchTask(t *testing.T, timeout ...time.Duration) *runne
120120
task = resp.Msg.Task
121121
break
122122
}
123-
time.Sleep(time.Second)
123+
time.Sleep(200 * time.Millisecond)
124124
}
125125

126126
return task

0 commit comments

Comments
 (0)