test: wait for indexer startup before loading fixtures (#40467)

Integration tests load fixtures after the app starts, while the indexers
still initialize and populate in the background. A slow start indexed
every fixture repo or dropped early index updates, making search tests
flaky.

The indexer startup goroutines now join a WaitGroup that only the test
harness waits on. This replaces the issue indexer's `syncReindex` flag.
Also fixes a flaky `SyncDirs` test that relied on directory listing
order.

Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
silverwind authored and GitHub committed 2026-11-21 02:43:03 +00:00
1 parent 4a9a53d79a
commit c9273a5a59
8 files changed
+35 -28

No files matched your search

+1 -1
View File
@@ -29,7 +29,7 @@ func TestSyncDirsRestoresDestination(t *testing.T) {
entries, err := util.ListDirRecursively(destPath, &util.ListDirOptions{IncludeDir: true})
require.NoError(t, err)
assert.Equal(t, []string{"dir/", "dir/file", "dir/sub/"}, entries)
assert.ElementsMatch(t, []string{"dir/", "dir/file", "dir/sub/"}, entries)
content, err := os.ReadFile(filepath.Join(destPath, "dir/file"))
require.NoError(t, err)
assert.Equal(t, "content", string(content))
+5 -4
View File
@@ -8,6 +8,7 @@ import (
"os"
"runtime/pprof"
"slices"
"sync"
"sync/atomic"
"time"
@@ -98,7 +99,7 @@ func index(ctx context.Context, indexer internal.Indexer, repoID int64) error {
}
// Init initialize the repo indexer
func Init() {
func Init(startup *sync.WaitGroup) {
if !setting.Indexer.RepoIndexerEnabled {
(*globalIndexer.Load()).Close()
return
@@ -145,7 +146,7 @@ func Init() {
log.Fatal("Unknown codes indexer type; %s", setting.Indexer.RepoType)
}
go func() {
startup.Go(func() {
pprof.SetGoroutineLabels(ctx)
start := time.Now()
var (
@@ -201,7 +202,7 @@ func Init() {
go graceful.GetManager().RunWithCancel(indexerQueue)
if !existed { // populate the index because it's created for the first time
go graceful.GetManager().RunWithShutdownContext(populateRepoIndexer)
startup.Go(func() { graceful.GetManager().RunWithShutdownContext(populateRepoIndexer) })
}
select {
case waitChannel <- time.Since(start):
@@ -209,7 +210,7 @@ func Init() {
}
close(waitChannel)
}()
})
if setting.Indexer.StartupTimeout > 0 {
go func() {
+7 -16
View File
@@ -8,6 +8,7 @@ import (
"fmt"
"os"
"runtime/pprof"
"sync"
"sync/atomic"
"time"
@@ -59,9 +60,8 @@ func init() {
globalIndexer.Store(dummyIndexer)
}
// InitIssueIndexer initialize issue indexer, syncReindex is true then reindex until
// all issue index done.
func InitIssueIndexer(syncReindex bool) {
// InitIssueIndexer initialize issue indexer
func InitIssueIndexer(startup *sync.WaitGroup) {
ctx, _, finished := process.GetManager().AddTypedContext(context.Background(), "Service: IssueIndexer", process.SystemProcessType, false)
indexerInitWaitChannel := make(chan time.Duration, 1)
@@ -72,7 +72,7 @@ func InitIssueIndexer(syncReindex bool) {
graceful.GetManager().RunAtTerminate(finished)
// Create the Indexer
go func() {
startup.Go(func() {
pprof.SetGoroutineLabels(ctx)
start := time.Now()
log.Info("PID %d: Initializing Issue Indexer: %s", os.Getpid(), setting.Indexer.IssueType)
@@ -127,23 +127,14 @@ func InitIssueIndexer(syncReindex bool) {
// Populate the index
if !existed {
if syncReindex {
graceful.GetManager().RunWithShutdownContext(populateIssueIndexer)
} else {
go graceful.GetManager().RunWithShutdownContext(populateIssueIndexer)
}
startup.Go(func() { graceful.GetManager().RunWithShutdownContext(populateIssueIndexer) })
}
indexerInitWaitChannel <- time.Since(start)
close(indexerInitWaitChannel)
}()
})
if syncReindex {
select {
case <-indexerInitWaitChannel:
case <-graceful.GetManager().IsShutdown():
}
} else if setting.Indexer.StartupTimeout > 0 {
if setting.Indexer.StartupTimeout > 0 {
go func() {
pprof.SetGoroutineLabels(ctx)
timeout := setting.Indexer.StartupTimeout
+4 -1
View File
@@ -4,6 +4,7 @@
package issues
import (
"sync"
"testing"
"gitea.dev/models/db"
@@ -29,7 +30,9 @@ func TestDBSearchIssues(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
setting.Indexer.IssueType = "db"
InitIssueIndexer(true)
var startup sync.WaitGroup
InitIssueIndexer(&startup)
startup.Wait()
t.Run("search issues with keyword", searchIssueWithKeyword)
t.Run("search issues by index", searchIssueByIndex)
+3 -2
View File
@@ -5,6 +5,7 @@ package stats
import (
"context"
"sync"
"gitea.dev/models/db"
repo_model "gitea.dev/models/repo"
@@ -23,14 +24,14 @@ type Indexer interface {
var indexer Indexer
// Init initialize the repo indexer
func Init() error {
func Init(startup *sync.WaitGroup) error {
indexer = &DBIndexer{}
if err := initStatsQueue(); err != nil {
return err
}
go populateRepoIndexer(graceful.GetManager().ShutdownContext())
startup.Go(func() { populateRepoIndexer(graceful.GetManager().ShutdownContext()) })
return nil
}
+2 -1
View File
@@ -4,6 +4,7 @@
package stats
import (
"sync"
"testing"
"time"
@@ -29,7 +30,7 @@ func TestRepoStatsIndex(t *testing.T) {
setting.LoadQueueSettings()
err := Init()
err := Init(&sync.WaitGroup{})
assert.NoError(t, err)
repo, err := repo_model.GetRepositoryByID(t.Context(), 1)
+11 -3
View File
@@ -4,17 +4,25 @@
package indexer
import (
"sync"
code_indexer "gitea.dev/modules/indexer/code"
issue_indexer "gitea.dev/modules/indexer/issues"
stats_indexer "gitea.dev/modules/indexer/stats"
notify_service "gitea.dev/services/notify"
)
var startup sync.WaitGroup
// Init initialize the repo indexer
func Init() error {
notify_service.RegisterNotifier(NewNotifier())
issue_indexer.InitIssueIndexer(false)
code_indexer.Init()
return stats_indexer.Init()
issue_indexer.InitIssueIndexer(&startup)
code_indexer.Init(&startup)
return stats_indexer.Init(&startup)
}
func WaitForStartup() {
startup.Wait()
}
+2
View File
@@ -17,6 +17,7 @@ import (
"gitea.dev/modules/testlogger"
"gitea.dev/modules/util"
"gitea.dev/routers"
indexer_service "gitea.dev/services/indexer"
"github.com/stretchr/testify/assert"
)
@@ -49,6 +50,7 @@ func InitIntegrationTest() (workPath string, err error) {
}
routers.InitWebInstalled(graceful.GetManager().HammerContext())
indexer_service.WaitForStartup()
return workPath, nil
}