From aba99ab8fc3b8bc8f55fa0b7d6261eea7b28d7c0 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Tue, 9 Apr 2024 15:08:01 +0200 Subject: [PATCH 1/2] [TEST] cancel all processes on PrepareTestEnv --- tests/test_utils.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/test_utils.go b/tests/test_utils.go index af83f9027d..f52a060adf 100644 --- a/tests/test_utils.go +++ b/tests/test_utils.go @@ -11,7 +11,9 @@ import ( "os" "path" "path/filepath" + "runtime" "testing" + "time" "code.gitea.io/gitea/models/db" packages_model "code.gitea.io/gitea/models/packages" @@ -20,6 +22,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "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/storage" @@ -181,6 +184,36 @@ func PrepareAttachmentsStorage(t testing.TB) { })) } +// cancelProcesses cancels all processes of the [process.Manager]. +// Returns immediately if delay is 0, otherwise wait until all processes are done +// and fails the test if it takes longer that the given delay. +func cancelProcesses(t testing.TB, delay time.Duration) { + processManager := process.GetManager() + processes, _ := processManager.Processes(true, true) + for _, p := range processes { + processManager.Cancel(p.PID) + t.Logf("PrepareTestEnv:Process %q cancelled", p.Description) + } + if delay == 0 || len(processes) == 0 { + return + } + + start := time.Now() + processes, _ = processManager.Processes(true, true) + for len(processes) > 0 { + if time.Since(start) > delay { + t.Errorf("ERROR PrepareTestEnv: could not cancel all processes within %s", delay) + for _, p := range processes { + t.Logf("PrepareTestEnv:Remaining Process: %q", p.Description) + } + return + } + runtime.Gosched() // let the context cancellation propagate + processes, _ = processManager.Processes(true, true) + } + t.Logf("PrepareTestEnv: all processes cancelled within %s", time.Since(start)) +} + func PrepareTestEnv(t testing.TB, skip ...int) func() { t.Helper() ourSkip := 1 @@ -189,6 +222,11 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() { } deferFn := PrintCurrentTest(t, ourSkip) + // kill all background processes to prevent them from interfering with the fixture loading + // see https://codeberg.org/forgejo/forgejo/issues/2962 + cancelProcesses(t, time.Second) + t.Cleanup(func() { cancelProcesses(t, 0) }) // cancel remaining processes in a non-blocking way + // load database fixtures assert.NoError(t, unittest.LoadFixtures()) From 8ffaa08b04f507c5133cef2b585777a469127563 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Tue, 9 Apr 2024 21:14:51 +0200 Subject: [PATCH 2/2] add missing defer --- tests/integration/git_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/git_test.go b/tests/integration/git_test.go index 708f9a820d..f833366568 100644 --- a/tests/integration/git_test.go +++ b/tests/integration/git_test.go @@ -1076,6 +1076,7 @@ func TestDataAsync_Issue29101(t *testing.T) { gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, repo) assert.NoError(t, err) + defer gitRepo.Close() commit, err := gitRepo.GetCommit(sha) assert.NoError(t, err)