Skip to content

Commit a409f9b

Browse files
committed
fix more tests
1 parent 3da3827 commit a409f9b

4 files changed

Lines changed: 44 additions & 25 deletions

File tree

modules/git/repo_base_nogogit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (_ CatFileBatch, close
7575
if repo.catFileBatchCloser == nil {
7676
repo.catFileBatchCloser, err = NewBatch(ctx, repo.Path)
7777
if err != nil {
78+
repo.catFileBatchCloser = nil // otherwise it is "interface(nil)" and will cause wrong logic
7879
return nil, nil, err
7980
}
8081
}
@@ -100,6 +101,8 @@ func (repo *Repository) Close() error {
100101
if repo == nil {
101102
return nil
102103
}
104+
repo.mu.Lock()
105+
defer repo.mu.Unlock()
103106
if repo.catFileBatchCloser != nil {
104107
repo.catFileBatchCloser.Close()
105108
repo.catFileBatchCloser = nil

modules/git/repo_base_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2026 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package git
5+
6+
import (
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestRepoCatFileBatch(t *testing.T) {
14+
t.Run("MissingRepoAndClose", func(t *testing.T) {
15+
repo, err := OpenRepository(t.Context(), filepath.Join(testReposDir, "repo1_bare"))
16+
require.NoError(t, err)
17+
repo.Path = "/no-such" // when the repo is missing (it usually occurs during testing because the fixtures are synced frequently)
18+
_, _, err = repo.CatFileBatch(t.Context())
19+
require.Error(t, err)
20+
require.NoError(t, repo.Close()) // shouldn't panic
21+
})
22+
23+
// TODO: test more methods and concurrency queries
24+
}

modules/graceful/manager.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ func (g *Manager) RunWithCancel(rc RunCanceler) {
7474
g.RunAtShutdown(context.Background(), rc.Cancel)
7575
g.runningServerWaitGroup.Add(1)
7676
defer g.runningServerWaitGroup.Done()
77-
defer func() {
78-
if err := recover(); err != nil {
79-
log.Critical("PANIC during RunWithCancel: %v\nStacktrace: %s", err, log.Stack(2))
80-
g.doShutdown()
81-
}
82-
}()
8377
rc.Run()
8478
}
8579

@@ -89,12 +83,6 @@ func (g *Manager) RunWithCancel(rc RunCanceler) {
8983
func (g *Manager) RunWithShutdownContext(run func(context.Context)) {
9084
g.runningServerWaitGroup.Add(1)
9185
defer g.runningServerWaitGroup.Done()
92-
defer func() {
93-
if err := recover(); err != nil {
94-
log.Critical("PANIC during RunWithShutdownContext: %v\nStacktrace: %s", err, log.Stack(2))
95-
g.doShutdown()
96-
}
97-
}()
9886
ctx := g.ShutdownContext()
9987
pprof.SetGoroutineLabels(ctx) // We don't have a label to restore back to but I think this is fine
10088
run(ctx)

modules/testlogger/testlogger.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package testlogger
55

66
import (
7+
"context"
78
"fmt"
89
"os"
910
"runtime"
@@ -108,30 +109,33 @@ func PrintCurrentTest(t testing.TB, skip ...int) func() {
108109
actualSkip := util.OptionalArg(skip) + 1
109110
_, filename, line, _ := runtime.Caller(actualSkip)
110111

112+
getRuntimeStackAll := func() string {
113+
stack := make([]byte, 1024*1024)
114+
n := runtime.Stack(stack, true)
115+
return util.UnsafeBytesToString(stack[:n])
116+
}
117+
118+
deferHasRun := false
119+
t.Cleanup(func() {
120+
Printf("--- %s (%s:%d) Cleanup\n", log.NewColoredValue(t.Name()), strings.TrimPrefix(filename, prefix), line)
121+
if !deferHasRun {
122+
Printf("!!! defer function hasn't been run but Cleanup is called\n%s", getRuntimeStackAll())
123+
}
124+
})
111125
Printf("=== %s (%s:%d)\n", log.NewColoredValue(t.Name()), strings.TrimPrefix(filename, prefix), line)
112126

113127
WriterCloser.pushT(t)
114128
timeoutChecker := time.AfterFunc(TestTimeout, func() {
115-
l := 128 * 1024
116-
var stack []byte
117-
for {
118-
stack = make([]byte, l)
119-
n := runtime.Stack(stack, true)
120-
if n <= l {
121-
stack = stack[:n]
122-
break
123-
}
124-
l = n
125-
}
126-
Printf("!!! %s ... timeout: %v ... stacktrace:\n%s\n\n", log.NewColoredValue(t.Name(), log.Bold, log.FgRed), TestTimeout, string(stack))
129+
Printf("!!! %s ... timeout: %v ... stacktrace:\n%s\n\n", log.NewColoredValue(t.Name(), log.Bold, log.FgRed), TestTimeout, getRuntimeStackAll())
127130
})
128131
return func() {
132+
deferHasRun = true
129133
flushStart := time.Now()
130134
slowFlushChecker := time.AfterFunc(TestSlowFlush, func() {
131135
Printf("+++ %s ... still flushing after %v ...\n", log.NewColoredValue(t.Name(), log.Bold, log.FgRed), TestSlowFlush)
132136
})
133137
if err := queue.GetManager().FlushAll(t.Context(), -1); err != nil {
134-
t.Errorf("Flushing queues failed with error %v", err)
138+
t.Errorf("Flushing queues failed with error %q, cause %q", err, context.Cause(t.Context()))
135139
}
136140
slowFlushChecker.Stop()
137141
timeoutChecker.Stop()

0 commit comments

Comments
 (0)