Skip to content

Commit 85d01db

Browse files
committed
fix
1 parent 4121f95 commit 85d01db

File tree

9 files changed

+106
-115
lines changed

9 files changed

+106
-115
lines changed

.github/workflows/pull-db-tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ jobs:
158158
env:
159159
MYSQL_ALLOW_EMPTY_PASSWORD: true
160160
MYSQL_DATABASE: testgitea
161+
MYSQL_EXTRA_FLAGS: >
162+
--innodb-doublewrite=off
163+
--innodb-flush-log-at-trx-commit=0
164+
--disable-log-bin
161165
ports:
162166
- "3306:3306"
163167
elasticsearch:

models/migrations/base/tests.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"code.gitea.io/gitea/models/unittest"
1616
"code.gitea.io/gitea/modules/base"
1717
"code.gitea.io/gitea/modules/git"
18-
"code.gitea.io/gitea/modules/log"
1918
"code.gitea.io/gitea/modules/setting"
2019
"code.gitea.io/gitea/modules/testlogger"
2120

@@ -91,7 +90,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
9190
}
9291

9392
func MainTest(m *testing.M) {
94-
log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter)
93+
testlogger.Init()
9594

9695
giteaRoot := base.SetupGiteaRoot()
9796
if giteaRoot == "" {

modules/log/color.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,19 @@ type ColoredValue struct {
8686
colors []ColorAttribute
8787
}
8888

89+
var _ fmt.Formatter = (*ColoredValue)(nil)
90+
8991
func (c *ColoredValue) Format(f fmt.State, verb rune) {
9092
_, _ = f.Write(ColorBytes(c.colors...))
9193
s := fmt.Sprintf(fmt.FormatString(f, verb), c.v)
9294
_, _ = f.Write([]byte(s))
9395
_, _ = f.Write(resetBytes)
9496
}
9597

98+
func (c *ColoredValue) Value() any {
99+
return c.v
100+
}
101+
96102
func NewColoredValue(v any, color ...ColorAttribute) *ColoredValue {
97103
return &ColoredValue{v: v, colors: color}
98104
}

modules/testlogger/testlogger.go

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import (
1919
)
2020

2121
var (
22-
prefix string
23-
SlowTest = 10 * time.Second
24-
SlowFlush = 5 * time.Second
22+
prefix string
23+
TestTimeout = 10 * time.Minute
24+
TestSlowRun = 10 * time.Second
25+
TestSlowFlush = 5 * time.Second
2526
)
2627

2728
var WriterCloser = &testLoggerWriterCloser{}
@@ -89,79 +90,92 @@ func (w *testLoggerWriterCloser) Reset() {
8990
w.Unlock()
9091
}
9192

93+
// Printf takes a format and args and prints the string to os.Stdout
94+
func Printf(format string, args ...any) {
95+
if !log.CanColorStdout {
96+
for i := 0; i < len(args); i++ {
97+
if c, ok := args[i].(*log.ColoredValue); ok {
98+
args[i] = c.Value()
99+
}
100+
}
101+
}
102+
_, _ = fmt.Fprintf(os.Stdout, "\t"+format, args...)
103+
}
104+
92105
// PrintCurrentTest prints the current test to os.Stdout
93106
func PrintCurrentTest(t testing.TB, skip ...int) func() {
94107
t.Helper()
95-
start := time.Now()
108+
runStart := time.Now()
96109
actualSkip := util.OptionalArg(skip) + 1
97110
_, filename, line, _ := runtime.Caller(actualSkip)
98111

99-
if log.CanColorStdout {
100-
_, _ = fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", fmt.Formatter(log.NewColoredValue(t.Name())), strings.TrimPrefix(filename, prefix), line)
101-
} else {
102-
_, _ = fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", t.Name(), strings.TrimPrefix(filename, prefix), line)
103-
}
112+
Printf("=== %s (%s:%d)\n", log.NewColoredValue(t.Name()), strings.TrimPrefix(filename, prefix), line)
113+
104114
WriterCloser.pushT(t)
105-
return func() {
106-
took := time.Since(start)
107-
if took > SlowTest {
108-
if log.CanColorStdout {
109-
_, _ = fmt.Fprintf(os.Stdout, "+++ %s is a slow test (took %v)\n", fmt.Formatter(log.NewColoredValue(t.Name(), log.Bold, log.FgYellow)), fmt.Formatter(log.NewColoredValue(took, log.Bold, log.FgYellow)))
110-
} else {
111-
_, _ = fmt.Fprintf(os.Stdout, "+++ %s is a slow test (took %v)\n", t.Name(), took)
115+
timeoutChecker := time.AfterFunc(TestTimeout, func() {
116+
l := 128 * 1024
117+
var stack []byte
118+
for {
119+
stack = make([]byte, l)
120+
n := runtime.Stack(stack, true)
121+
if n <= l {
122+
stack = stack[:n]
123+
break
112124
}
125+
l = n
113126
}
114-
timer := time.AfterFunc(SlowFlush, func() {
115-
if log.CanColorStdout {
116-
_, _ = fmt.Fprintf(os.Stdout, "+++ %s ... still flushing after %v ...\n", fmt.Formatter(log.NewColoredValue(t.Name(), log.Bold, log.FgRed)), SlowFlush)
117-
} else {
118-
_, _ = fmt.Fprintf(os.Stdout, "+++ %s ... still flushing after %v ...\n", t.Name(), SlowFlush)
119-
}
127+
Printf("!!! %s ... timeout: %v ... stacktrace:\n%s\n\n", log.NewColoredValue(t.Name(), log.Bold, log.FgRed), TestTimeout, string(stack))
128+
})
129+
return func() {
130+
flushStart := time.Now()
131+
slowFlushChecker := time.AfterFunc(TestSlowFlush, func() {
132+
Printf("+++ %s ... still flushing after %v ...\n", log.NewColoredValue(t.Name(), log.Bold, log.FgRed), TestSlowFlush)
120133
})
121134
if err := queue.GetManager().FlushAll(context.Background(), -1); err != nil {
122135
t.Errorf("Flushing queues failed with error %v", err)
123136
}
124-
timer.Stop()
125-
flushTook := time.Since(start) - took
126-
if flushTook > SlowFlush {
127-
if log.CanColorStdout {
128-
_, _ = fmt.Fprintf(os.Stdout, "+++ %s had a slow clean-up flush (took %v)\n", fmt.Formatter(log.NewColoredValue(t.Name(), log.Bold, log.FgRed)), fmt.Formatter(log.NewColoredValue(flushTook, log.Bold, log.FgRed)))
129-
} else {
130-
_, _ = fmt.Fprintf(os.Stdout, "+++ %s had a slow clean-up flush (took %v)\n", t.Name(), flushTook)
131-
}
132-
}
133-
WriterCloser.popT()
134-
}
135-
}
137+
slowFlushChecker.Stop()
138+
timeoutChecker.Stop()
136139

137-
// Printf takes a format and args and prints the string to os.Stdout
138-
func Printf(format string, args ...any) {
139-
if log.CanColorStdout {
140-
for i := 0; i < len(args); i++ {
141-
args[i] = log.NewColoredValue(args[i])
140+
runDuration := time.Since(runStart)
141+
flushDuration := time.Since(flushStart)
142+
if runDuration > TestSlowRun {
143+
Printf("+++ %s is a slow test (run: %v, flush: %v)\n", log.NewColoredValue(t.Name(), log.Bold, log.FgYellow), runDuration, flushDuration)
142144
}
145+
WriterCloser.popT()
143146
}
144-
_, _ = fmt.Fprintf(os.Stdout, "\t"+format, args...)
145147
}
146148

147149
// TestLogEventWriter is a logger which will write to the testing log
148150
type TestLogEventWriter struct {
149151
*log.EventWriterBaseImpl
150152
}
151153

152-
// NewTestLoggerWriter creates a TestLogEventWriter as a log.LoggerProvider
153-
func NewTestLoggerWriter(name string, mode log.WriterMode) log.EventWriter {
154+
// newTestLoggerWriter creates a TestLogEventWriter as a log.LoggerProvider
155+
func newTestLoggerWriter(name string, mode log.WriterMode) log.EventWriter {
154156
w := &TestLogEventWriter{}
155157
w.EventWriterBaseImpl = log.NewEventWriterBase(name, "test-log-writer", mode)
156158
w.OutputWriteCloser = WriterCloser
157159
return w
158160
}
159161

160-
func init() {
162+
func Init() {
161163
const relFilePath = "modules/testlogger/testlogger.go"
162164
_, filename, _, _ := runtime.Caller(0)
163165
if !strings.HasSuffix(filename, relFilePath) {
164166
panic("source code file path doesn't match expected: " + relFilePath)
165167
}
166168
prefix = strings.TrimSuffix(filename, relFilePath)
169+
170+
log.RegisterEventWriter("test", newTestLoggerWriter)
171+
172+
duration, err := time.ParseDuration(os.Getenv("GITEA_TEST_SLOW_RUN"))
173+
if err == nil && duration > 0 {
174+
TestSlowRun = duration
175+
}
176+
177+
duration, err = time.ParseDuration(os.Getenv("GITEA_TEST_SLOW_FLUSH"))
178+
if err == nil && duration > 0 {
179+
TestSlowFlush = duration
180+
}
167181
}

tests/integration/README.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,8 @@ We appreciate that some testing machines may not be very powerful and
9999
the default timeouts for declaring a slow test or a slow clean-up flush
100100
may not be appropriate.
101101

102-
You can either:
103-
104-
* Within the test ini file set the following section:
105-
106-
```ini
107-
[integration-tests]
108-
SLOW_TEST = 10s ; 10s is the default value
109-
SLOW_FLUSH = 5S ; 5s is the default value
110-
```
111-
112-
* Set the following environment variables:
102+
You can set the following environment variables:
113103

114104
```bash
115-
GITEA_SLOW_TEST_TIME="10s" GITEA_SLOW_FLUSH_TIME="5s" make test-sqlite
105+
GITEA_TEST_SLOW_RUN="10s" GITEA_TEST_SLOW_FLUSH="5s" make test-sqlite
116106
```

tests/integration/integration_test.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ import (
2020
"strings"
2121
"sync/atomic"
2222
"testing"
23-
"time"
2423

2524
"code.gitea.io/gitea/models/auth"
2625
"code.gitea.io/gitea/models/unittest"
2726
"code.gitea.io/gitea/modules/graceful"
2827
"code.gitea.io/gitea/modules/json"
2928
"code.gitea.io/gitea/modules/log"
3029
"code.gitea.io/gitea/modules/setting"
31-
"code.gitea.io/gitea/modules/testlogger"
3230
"code.gitea.io/gitea/modules/util"
3331
"code.gitea.io/gitea/modules/web"
3432
"code.gitea.io/gitea/routers"
@@ -90,27 +88,6 @@ func TestMain(m *testing.M) {
9088
tests.InitTest(true)
9189
testWebRoutes = routers.NormalRoutes()
9290

93-
// integration test settings...
94-
if setting.CfgProvider != nil {
95-
testingCfg := setting.CfgProvider.Section("integration-tests")
96-
testlogger.SlowTest = testingCfg.Key("SLOW_TEST").MustDuration(testlogger.SlowTest)
97-
testlogger.SlowFlush = testingCfg.Key("SLOW_FLUSH").MustDuration(testlogger.SlowFlush)
98-
}
99-
100-
if os.Getenv("GITEA_SLOW_TEST_TIME") != "" {
101-
duration, err := time.ParseDuration(os.Getenv("GITEA_SLOW_TEST_TIME"))
102-
if err == nil {
103-
testlogger.SlowTest = duration
104-
}
105-
}
106-
107-
if os.Getenv("GITEA_SLOW_FLUSH_TIME") != "" {
108-
duration, err := time.ParseDuration(os.Getenv("GITEA_SLOW_FLUSH_TIME"))
109-
if err == nil {
110-
testlogger.SlowFlush = duration
111-
}
112-
}
113-
11491
os.Unsetenv("GIT_AUTHOR_NAME")
11592
os.Unsetenv("GIT_AUTHOR_EMAIL")
11693
os.Unsetenv("GIT_AUTHOR_DATE")
@@ -132,8 +109,6 @@ func TestMain(m *testing.M) {
132109
// Instead, "No tests were found", last nonsense log is "According to the configuration, subsequent logs will not be printed to the console"
133110
exitCode := m.Run()
134111

135-
testlogger.WriterCloser.Reset()
136-
137112
if err = util.RemoveAll(setting.Indexer.IssuePath); err != nil {
138113
fmt.Printf("util.RemoveAll: %v\n", err)
139114
os.Exit(1)

tests/integration/linguist_test.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package integration
66
import (
77
"context"
88
"net/url"
9+
"strconv"
910
"strings"
1011
"testing"
1112
"time"
@@ -19,6 +20,7 @@ import (
1920
"code.gitea.io/gitea/modules/queue"
2021
repo_service "code.gitea.io/gitea/services/repository"
2122
files_service "code.gitea.io/gitea/services/repository/files"
23+
"code.gitea.io/gitea/tests"
2224

2325
"github.com/stretchr/testify/assert"
2426
)
@@ -218,42 +220,43 @@ func TestLinguist(t *testing.T) {
218220
}
219221

220222
for i, c := range cases {
221-
repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
222-
Name: "linguist-test",
223-
})
224-
assert.NoError(t, err)
225-
226-
files := []*files_service.ChangeRepoFile{
227-
{
228-
TreePath: ".gitattributes",
229-
ContentReader: strings.NewReader(c.GitAttributesContent),
230-
},
231-
}
232-
files = append(files, c.FilesToAdd...)
233-
for _, f := range files {
234-
f.Operation = "create"
235-
}
223+
t.Run("Case-"+strconv.Itoa(i), func(t *testing.T) {
224+
defer tests.PrintCurrentTest(t)()
225+
repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
226+
Name: "linguist-test-" + strconv.Itoa(i),
227+
})
228+
assert.NoError(t, err)
236229

237-
_, err = files_service.ChangeRepoFiles(git.DefaultContext, repo, user, &files_service.ChangeRepoFilesOptions{
238-
Files: files,
239-
OldBranch: repo.DefaultBranch,
240-
NewBranch: repo.DefaultBranch,
241-
})
242-
assert.NoError(t, err)
230+
files := []*files_service.ChangeRepoFile{
231+
{
232+
TreePath: ".gitattributes",
233+
ContentReader: strings.NewReader(c.GitAttributesContent),
234+
},
235+
}
236+
files = append(files, c.FilesToAdd...)
237+
for _, f := range files {
238+
f.Operation = "create"
239+
}
243240

244-
assert.NoError(t, stats.UpdateRepoIndexer(repo))
245-
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), 10*time.Second))
241+
_, err = files_service.ChangeRepoFiles(git.DefaultContext, repo, user, &files_service.ChangeRepoFilesOptions{
242+
Files: files,
243+
OldBranch: repo.DefaultBranch,
244+
NewBranch: repo.DefaultBranch,
245+
})
246+
assert.NoError(t, err)
246247

247-
stats, err := repo_model.GetTopLanguageStats(db.DefaultContext, repo, len(c.FilesToAdd))
248-
assert.NoError(t, err)
248+
assert.NoError(t, stats.UpdateRepoIndexer(repo))
249+
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), 10*time.Second))
249250

250-
languages := make([]string, 0, len(stats))
251-
for _, s := range stats {
252-
languages = append(languages, s.Language)
253-
}
254-
assert.Equal(t, c.ExpectedLanguageOrder, languages, "case %d: unexpected language stats", i)
251+
stats, err := repo_model.GetTopLanguageStats(db.DefaultContext, repo, len(c.FilesToAdd))
252+
assert.NoError(t, err)
255253

256-
assert.NoError(t, repo_service.DeleteRepository(db.DefaultContext, user, repo, false))
254+
languages := make([]string, 0, len(stats))
255+
for _, s := range stats {
256+
languages = append(languages, s.Language)
257+
}
258+
assert.Equal(t, c.ExpectedLanguageOrder, languages, "case %d: unexpected language stats", i)
259+
})
257260
}
258261
})
259262
}

tests/integration/migration-test/migration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
var currentEngine *xorm.Engine
3838

3939
func initMigrationTest(t *testing.T) func() {
40-
log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter)
40+
testlogger.Init()
4141

4242
deferFn := tests.PrintCurrentTest(t, 2)
4343
giteaRoot := base.SetupGiteaRoot()

tests/test_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func exitf(format string, args ...any) {
3535
}
3636

3737
func InitTest(requireGitea bool) {
38-
log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter)
38+
testlogger.Init()
3939

4040
giteaRoot := base.SetupGiteaRoot()
4141
if giteaRoot == "" {

0 commit comments

Comments
 (0)