Skip to content

Commit cc1fdc8

Browse files
TheFox0x7GiteaBotwxiaoguang
authored
Use test context in tests and new loop system in benchmarks (#33648)
Replace all contexts in tests with go1.24 t.Context() --------- Co-authored-by: Giteabot <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 3bbc482 commit cc1fdc8

File tree

108 files changed

+712
-794
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+712
-794
lines changed

cmd/hook_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cmd
66
import (
77
"bufio"
88
"bytes"
9-
"context"
109
"strings"
1110
"testing"
1211

@@ -15,7 +14,7 @@ import (
1514

1615
func TestPktLine(t *testing.T) {
1716
// test read
18-
ctx := context.Background()
17+
ctx := t.Context()
1918
s := strings.NewReader("0000")
2019
r := bufio.NewReader(s)
2120
result, err := readPktLine(ctx, r, pktLineTypeFlush)

cmd/migrate_storage_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package cmd
55

66
import (
7-
"context"
87
"os"
98
"strings"
109
"testing"
@@ -53,7 +52,7 @@ func TestMigratePackages(t *testing.T) {
5352
assert.NotNil(t, v)
5453
assert.NotNil(t, f)
5554

56-
ctx := context.Background()
55+
ctx := t.Context()
5756

5857
p := t.TempDir()
5958

models/auth/oauth2_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestOAuth2Application_GenerateClientSecret(t *testing.T) {
2525
func BenchmarkOAuth2Application_GenerateClientSecret(b *testing.B) {
2626
assert.NoError(b, unittest.PrepareTestDatabase())
2727
app := unittest.AssertExistsAndLoadBean(b, &auth_model.OAuth2Application{ID: 1})
28-
for i := 0; i < b.N; i++ {
28+
for b.Loop() {
2929
_, _ = app.GenerateClientSecret(db.DefaultContext)
3030
}
3131
}

models/issues/issue_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package issues_test
55

66
import (
7-
"context"
87
"fmt"
98
"sort"
109
"sync"
@@ -326,7 +325,7 @@ func TestCorrectIssueStats(t *testing.T) {
326325
wg.Wait()
327326

328327
// Now we will get all issueID's that match the "Bugs are nasty" query.
329-
issues, err := issues_model.Issues(context.TODO(), &issues_model.IssuesOptions{
328+
issues, err := issues_model.Issues(t.Context(), &issues_model.IssuesOptions{
330329
Paginator: &db.ListOptions{
331330
PageSize: issueAmount,
332331
},

models/renderhelper/repo_comment_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package renderhelper
55

66
import (
7-
"context"
87
"testing"
98

109
repo_model "code.gitea.io/gitea/models/repo"
@@ -21,7 +20,7 @@ func TestRepoComment(t *testing.T) {
2120
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
2221

2322
t.Run("AutoLink", func(t *testing.T) {
24-
rctx := NewRenderContextRepoComment(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
23+
rctx := NewRenderContextRepoComment(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
2524
rendered, err := markup.RenderString(rctx, `
2625
65f1bf27bc3bf70f64657658635e66094edbcb4d
2726
#1
@@ -36,7 +35,7 @@ func TestRepoComment(t *testing.T) {
3635
})
3736

3837
t.Run("AbsoluteAndRelative", func(t *testing.T) {
39-
rctx := NewRenderContextRepoComment(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
38+
rctx := NewRenderContextRepoComment(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
4039

4140
// It is Gitea's old behavior, the relative path is resolved to the repo path
4241
// It is different from GitHub, GitHub resolves relative links to current page's path
@@ -56,7 +55,7 @@ func TestRepoComment(t *testing.T) {
5655
})
5756

5857
t.Run("WithCurrentRefPath", func(t *testing.T) {
59-
rctx := NewRenderContextRepoComment(context.Background(), repo1, RepoCommentOptions{CurrentRefPath: "/commit/1234"}).
58+
rctx := NewRenderContextRepoComment(t.Context(), repo1, RepoCommentOptions{CurrentRefPath: "/commit/1234"}).
6059
WithMarkupType(markdown.MarkupName)
6160

6261
// the ref path is only used to render commit message: a commit message is rendered at the commit page with its commit ID path

models/renderhelper/repo_file_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package renderhelper
55

66
import (
7-
"context"
87
"testing"
98

109
repo_model "code.gitea.io/gitea/models/repo"
@@ -22,7 +21,7 @@ func TestRepoFile(t *testing.T) {
2221
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
2322

2423
t.Run("AutoLink", func(t *testing.T) {
25-
rctx := NewRenderContextRepoFile(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
24+
rctx := NewRenderContextRepoFile(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
2625
rendered, err := markup.RenderString(rctx, `
2726
65f1bf27bc3bf70f64657658635e66094edbcb4d
2827
#1
@@ -37,7 +36,7 @@ func TestRepoFile(t *testing.T) {
3736
})
3837

3938
t.Run("AbsoluteAndRelative", func(t *testing.T) {
40-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{CurrentRefPath: "branch/main"}).
39+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{CurrentRefPath: "branch/main"}).
4140
WithMarkupType(markdown.MarkupName)
4241
rendered, err := markup.RenderString(rctx, `
4342
[/test](/test)
@@ -55,7 +54,7 @@ func TestRepoFile(t *testing.T) {
5554
})
5655

5756
t.Run("WithCurrentRefPath", func(t *testing.T) {
58-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{CurrentRefPath: "/commit/1234"}).
57+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{CurrentRefPath: "/commit/1234"}).
5958
WithMarkupType(markdown.MarkupName)
6059
rendered, err := markup.RenderString(rctx, `
6160
[/test](/test)
@@ -68,7 +67,7 @@ func TestRepoFile(t *testing.T) {
6867
})
6968

7069
t.Run("WithCurrentRefPathByTag", func(t *testing.T) {
71-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{
70+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{
7271
CurrentRefPath: "/commit/1234",
7372
CurrentTreePath: "my-dir",
7473
}).
@@ -89,7 +88,7 @@ func TestRepoFileOrgMode(t *testing.T) {
8988
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
9089

9190
t.Run("Links", func(t *testing.T) {
92-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{
91+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{
9392
CurrentRefPath: "/commit/1234",
9493
CurrentTreePath: "my-dir",
9594
}).WithRelativePath("my-dir/a.org")
@@ -106,7 +105,7 @@ func TestRepoFileOrgMode(t *testing.T) {
106105
})
107106

108107
t.Run("CodeHighlight", func(t *testing.T) {
109-
rctx := NewRenderContextRepoFile(context.Background(), repo1, RepoFileOptions{}).WithRelativePath("my-dir/a.org")
108+
rctx := NewRenderContextRepoFile(t.Context(), repo1, RepoFileOptions{}).WithRelativePath("my-dir/a.org")
110109

111110
rendered, err := markup.RenderString(rctx, `
112111
#+begin_src c

models/renderhelper/repo_wiki_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package renderhelper
55

66
import (
7-
"context"
87
"testing"
98

109
repo_model "code.gitea.io/gitea/models/repo"
@@ -20,7 +19,7 @@ func TestRepoWiki(t *testing.T) {
2019
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
2120

2221
t.Run("AutoLink", func(t *testing.T) {
23-
rctx := NewRenderContextRepoWiki(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
22+
rctx := NewRenderContextRepoWiki(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
2423
rendered, err := markup.RenderString(rctx, `
2524
65f1bf27bc3bf70f64657658635e66094edbcb4d
2625
#1
@@ -35,7 +34,7 @@ func TestRepoWiki(t *testing.T) {
3534
})
3635

3736
t.Run("AbsoluteAndRelative", func(t *testing.T) {
38-
rctx := NewRenderContextRepoWiki(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
37+
rctx := NewRenderContextRepoWiki(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
3938
rendered, err := markup.RenderString(rctx, `
4039
[/test](/test)
4140
[./test](./test)
@@ -52,7 +51,7 @@ func TestRepoWiki(t *testing.T) {
5251
})
5352

5453
t.Run("PathInTag", func(t *testing.T) {
55-
rctx := NewRenderContextRepoWiki(context.Background(), repo1).WithMarkupType(markdown.MarkupName)
54+
rctx := NewRenderContextRepoWiki(t.Context(), repo1).WithMarkupType(markdown.MarkupName)
5655
rendered, err := markup.RenderString(rctx, `
5756
<img src="LINK">
5857
<video src="LINK">

models/renderhelper/simple_document_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package renderhelper
55

66
import (
7-
"context"
87
"testing"
98

109
"code.gitea.io/gitea/models/unittest"
@@ -16,7 +15,7 @@ import (
1615

1716
func TestSimpleDocument(t *testing.T) {
1817
unittest.PrepareTestEnv(t)
19-
rctx := NewRenderContextSimpleDocument(context.Background(), "/base").WithMarkupType(markdown.MarkupName)
18+
rctx := NewRenderContextSimpleDocument(t.Context(), "/base").WithMarkupType(markdown.MarkupName)
2019
rendered, err := markup.RenderString(rctx, `
2120
65f1bf27bc3bf70f64657658635e66094edbcb4d
2221
#1

models/repo/wiki_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package repo_test
55

66
import (
7-
"context"
87
"path/filepath"
98
"testing"
109

@@ -19,7 +18,7 @@ func TestRepository_WikiCloneLink(t *testing.T) {
1918
assert.NoError(t, unittest.PrepareTestDatabase())
2019

2120
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
22-
cloneLink := repo.WikiCloneLink(context.Background(), nil)
21+
cloneLink := repo.WikiCloneLink(t.Context(), nil)
2322
assert.Equal(t, "ssh://[email protected]:3000/user2/repo1.wiki.git", cloneLink.SSH)
2423
assert.Equal(t, "https://try.gitea.io/user2/repo1.wiki.git", cloneLink.HTTPS)
2524
}

models/unittest/fixtures_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ func BenchmarkFixturesLoader(b *testing.B) {
9999
// BenchmarkFixturesLoader/Internal
100100
// BenchmarkFixturesLoader/Internal-12 1746 670457 ns/op
101101
b.Run("Internal", func(b *testing.B) {
102-
for i := 0; i < b.N; i++ {
102+
for b.Loop() {
103103
require.NoError(b, loaderInternal.Load())
104104
}
105105
})
106106
b.Run("Vendor", func(b *testing.B) {
107107
if loaderVendor == nil {
108108
b.Skip()
109109
}
110-
for i := 0; i < b.N; i++ {
110+
for b.Loop() {
111111
require.NoError(b, loaderVendor.Load())
112112
}
113113
})

models/user/avatar_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package user
55

66
import (
7-
"context"
87
"io"
98
"strings"
109
"testing"
@@ -37,7 +36,7 @@ func TestUserAvatarGenerate(t *testing.T) {
3736
assert.NoError(t, unittest.PrepareTestDatabase())
3837
var err error
3938
tmpDir := t.TempDir()
40-
storage.Avatars, err = storage.NewLocalStorage(context.Background(), &setting.Storage{Path: tmpDir})
39+
storage.Avatars, err = storage.NewLocalStorage(t.Context(), &setting.Storage{Path: tmpDir})
4140
require.NoError(t, err)
4241

4342
u := unittest.AssertExistsAndLoadBean(t, &User{ID: 2})

models/user/user_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package user_test
55

66
import (
7-
"context"
87
"crypto/rand"
98
"fmt"
109
"strings"
@@ -201,7 +200,7 @@ func BenchmarkHashPassword(b *testing.B) {
201200
pass := "password1337"
202201
u := &user_model.User{Passwd: pass}
203202
b.ResetTimer()
204-
for i := 0; i < b.N; i++ {
203+
for b.Loop() {
205204
u.SetPassword(pass)
206205
}
207206
}
@@ -279,7 +278,7 @@ func TestCreateUserCustomTimestamps(t *testing.T) {
279278
err := user_model.CreateUser(db.DefaultContext, user, &user_model.Meta{})
280279
assert.NoError(t, err)
281280

282-
fetched, err := user_model.GetUserByID(context.Background(), user.ID)
281+
fetched, err := user_model.GetUserByID(t.Context(), user.ID)
283282
assert.NoError(t, err)
284283
assert.Equal(t, creationTimestamp, fetched.CreatedUnix)
285284
assert.Equal(t, creationTimestamp, fetched.UpdatedUnix)
@@ -306,7 +305,7 @@ func TestCreateUserWithoutCustomTimestamps(t *testing.T) {
306305

307306
timestampEnd := time.Now().Unix()
308307

309-
fetched, err := user_model.GetUserByID(context.Background(), user.ID)
308+
fetched, err := user_model.GetUserByID(t.Context(), user.ID)
310309
assert.NoError(t, err)
311310

312311
assert.LessOrEqual(t, timestampStart, fetched.CreatedUnix)

models/webhook/webhook_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package webhook
55

66
import (
7-
"context"
87
"testing"
98
"time"
109

@@ -245,7 +244,7 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
245244
assert.NoError(t, err)
246245
unittest.AssertExistsAndLoadBean(t, hookTask)
247246

248-
assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0))
247+
assert.NoError(t, CleanupHookTaskTable(t.Context(), PerWebhook, 168*time.Hour, 0))
249248
unittest.AssertNotExistsBean(t, hookTask)
250249
}
251250

@@ -261,7 +260,7 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
261260
assert.NoError(t, err)
262261
unittest.AssertExistsAndLoadBean(t, hookTask)
263262

264-
assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0))
263+
assert.NoError(t, CleanupHookTaskTable(t.Context(), PerWebhook, 168*time.Hour, 0))
265264
unittest.AssertExistsAndLoadBean(t, hookTask)
266265
}
267266

@@ -278,7 +277,7 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
278277
assert.NoError(t, err)
279278
unittest.AssertExistsAndLoadBean(t, hookTask)
280279

281-
assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 1))
280+
assert.NoError(t, CleanupHookTaskTable(t.Context(), PerWebhook, 168*time.Hour, 1))
282281
unittest.AssertExistsAndLoadBean(t, hookTask)
283282
}
284283

@@ -295,7 +294,7 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
295294
assert.NoError(t, err)
296295
unittest.AssertExistsAndLoadBean(t, hookTask)
297296

298-
assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
297+
assert.NoError(t, CleanupHookTaskTable(t.Context(), OlderThan, 168*time.Hour, 0))
299298
unittest.AssertNotExistsBean(t, hookTask)
300299
}
301300

@@ -311,7 +310,7 @@ func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
311310
assert.NoError(t, err)
312311
unittest.AssertExistsAndLoadBean(t, hookTask)
313312

314-
assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
313+
assert.NoError(t, CleanupHookTaskTable(t.Context(), OlderThan, 168*time.Hour, 0))
315314
unittest.AssertExistsAndLoadBean(t, hookTask)
316315
}
317316

@@ -328,6 +327,6 @@ func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *test
328327
assert.NoError(t, err)
329328
unittest.AssertExistsAndLoadBean(t, hookTask)
330329

331-
assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
330+
assert.NoError(t, CleanupHookTaskTable(t.Context(), OlderThan, 168*time.Hour, 0))
332331
unittest.AssertExistsAndLoadBean(t, hookTask)
333332
}

modules/cache/context_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
package cache
55

66
import (
7-
"context"
87
"testing"
98
"time"
109

1110
"github.com/stretchr/testify/assert"
1211
)
1312

1413
func TestWithCacheContext(t *testing.T) {
15-
ctx := WithCacheContext(context.Background())
14+
ctx := WithCacheContext(t.Context())
1615

1716
v := GetContextData(ctx, "empty_field", "my_config1")
1817
assert.Nil(t, v)
@@ -52,7 +51,7 @@ func TestWithCacheContext(t *testing.T) {
5251
}
5352

5453
func TestWithNoCacheContext(t *testing.T) {
55-
ctx := context.Background()
54+
ctx := t.Context()
5655

5756
const field = "system_setting"
5857

modules/csv/csv_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package csv
55

66
import (
77
"bytes"
8-
"context"
98
"encoding/csv"
109
"io"
1110
"strconv"
@@ -231,7 +230,7 @@ John Doe [email protected] This,note,had,a,lot,of,commas,to,test,delimiters`,
231230
}
232231

233232
for n, c := range cases {
234-
delimiter := determineDelimiter(markup.NewRenderContext(context.Background()).WithRelativePath(c.filename), []byte(decodeSlashes(t, c.csv)))
233+
delimiter := determineDelimiter(markup.NewRenderContext(t.Context()).WithRelativePath(c.filename), []byte(decodeSlashes(t, c.csv)))
235234
assert.EqualValues(t, c.expectedDelimiter, delimiter, "case %d: delimiter should be equal, expected '%c' got '%c'", n, c.expectedDelimiter, delimiter)
236235
}
237236
}

0 commit comments

Comments
 (0)