From 8f06a99c2ce1add8e31082e1a7792b3aa15dfefe Mon Sep 17 00:00:00 2001
From: oliverpool <git@olivier.pfad.fr>
Date: Wed, 10 Apr 2024 14:26:47 +0200
Subject: [PATCH] [TEST] make the indexer and pull tasks cancellable (without
 shutdown)

See
https://codeberg.org/forgejo/forgejo/pulls/3130#issuecomment-1763440
for the conflict resolution.

(cherry picked from commit d79690ce1890e692add310532858a77076792965)
---
 modules/indexer/code/indexer.go | 5 ++++-
 services/pull/pull.go           | 9 +++++++--
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/modules/indexer/code/indexer.go b/modules/indexer/code/indexer.go
index c1ab26569c..31adce10e0 100644
--- a/modules/indexer/code/indexer.go
+++ b/modules/indexer/code/indexer.go
@@ -120,9 +120,12 @@ func Init() {
 	case "bleve", "elasticsearch":
 		handler := func(items ...*internal.IndexerData) (unhandled []*internal.IndexerData) {
 			indexer := *globalIndexer.Load()
+			// make it a process to allow for cancellation (especially during integration tests where no global shutdown happens)
+			batchCtx, _, finished := process.GetManager().AddContext(ctx, "CodeIndexer batch")
+			defer finished()
 			for _, indexerData := range items {
 				log.Trace("IndexerData Process Repo: %d", indexerData.RepoID)
-				if err := index(ctx, indexer, indexerData.RepoID); err != nil {
+				if err := index(batchCtx, indexer, indexerData.RepoID); err != nil {
 					unhandled = append(unhandled, indexerData)
 					if !setting.IsInTesting {
 						log.Error("Codes indexer handler: index error for repo %v: %v", indexerData.RepoID, err)
diff --git a/services/pull/pull.go b/services/pull/pull.go
index ae70a14a1e..720efdb0cb 100644
--- a/services/pull/pull.go
+++ b/services/pull/pull.go
@@ -26,6 +26,7 @@ import (
 	"code.gitea.io/gitea/modules/graceful"
 	"code.gitea.io/gitea/modules/json"
 	"code.gitea.io/gitea/modules/log"
+	"code.gitea.io/gitea/modules/process"
 	repo_module "code.gitea.io/gitea/modules/repository"
 	"code.gitea.io/gitea/modules/setting"
 	"code.gitea.io/gitea/modules/sync"
@@ -296,8 +297,12 @@ func checkForInvalidation(ctx context.Context, requests issues_model.PullRequest
 // AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
 // and generate new patch for testing as needed.
 func AddTestPullRequestTask(ctx context.Context, doer *user_model.User, repoID int64, branch string, isSync bool, oldCommitID, newCommitID string, timeNano int64) {
-	log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: only pull requests created before nano time %d will be considered", repoID, branch, timeNano)
-	go graceful.GetManager().RunWithShutdownContext(func(ctx context.Context) {
+	description := fmt.Sprintf("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: only pull requests created before nano time %d will be considered", repoID, branch, timeNano)
+	log.Trace(description)
+	go graceful.GetManager().RunWithShutdownContext(func(shutdownCtx context.Context) {
+		// make it a process to allow for cancellation (especially during integration tests where no global shutdown happens)
+		ctx, _, finished := process.GetManager().AddContext(shutdownCtx, description)
+		defer finished()
 		// There is no sensible way to shut this down ":-("
 		// If you don't let it run all the way then you will lose data
 		// TODO: graceful: TestPullRequest needs to become a queue!