1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-28 09:21:13 -05:00

Merge pull request '[TEST] cancel all processes on PrepareTestEnv' (#3126) from oliverpool/forgejo:test_cancel_processes_on_prepare_test_env into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3126
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
This commit is contained in:
Earl Warren 2024-04-09 21:54:57 +00:00
commit fd0c8f5059
2 changed files with 39 additions and 0 deletions

View file

@ -1076,6 +1076,7 @@ func TestDataAsync_Issue29101(t *testing.T) {
gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, repo) gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, repo)
assert.NoError(t, err) assert.NoError(t, err)
defer gitRepo.Close()
commit, err := gitRepo.GetCommit(sha) commit, err := gitRepo.GetCommit(sha)
assert.NoError(t, err) assert.NoError(t, err)

View file

@ -11,7 +11,9 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"runtime"
"testing" "testing"
"time"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
packages_model "code.gitea.io/gitea/models/packages" packages_model "code.gitea.io/gitea/models/packages"
@ -20,6 +22,7 @@ import (
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
repo_module "code.gitea.io/gitea/modules/repository" repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage" "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() { func PrepareTestEnv(t testing.TB, skip ...int) func() {
t.Helper() t.Helper()
ourSkip := 1 ourSkip := 1
@ -189,6 +222,11 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() {
} }
deferFn := PrintCurrentTest(t, ourSkip) 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 // load database fixtures
assert.NoError(t, unittest.LoadFixtures()) assert.NoError(t, unittest.LoadFixtures())