Skip to content

Commit 0060093

Browse files
committed
test
1 parent d70af38 commit 0060093

File tree

7 files changed

+55
-52
lines changed

7 files changed

+55
-52
lines changed

models/db/context.go

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,31 @@ import (
1515
// will be overwritten by Init with HammerContext
1616
var DefaultContext context.Context
1717

18-
// contextKey is a value for use with context.WithValue.
19-
type contextKey struct {
20-
name string
21-
}
18+
type engineContextKeyType struct{}
2219

23-
// enginedContextKey is a context key. It is used with context.Value() to get the current Engined for the context
24-
var (
25-
enginedContextKey = &contextKey{"engined"}
26-
_ Engined = &Context{}
27-
)
20+
var engineContextKey = engineContextKeyType{}
2821

2922
// Context represents a db context
3023
type Context struct {
3124
context.Context
32-
e Engine
33-
transaction bool
34-
}
35-
36-
func newContext(ctx context.Context, e Engine, transaction bool) *Context {
37-
return &Context{
38-
Context: ctx,
39-
e: e,
40-
transaction: transaction,
41-
}
25+
engine Engine
4226
}
4327

44-
// InTransaction if context is in a transaction
45-
func (ctx *Context) InTransaction() bool {
46-
return ctx.transaction
47-
}
48-
49-
// Engine returns db engine
50-
func (ctx *Context) Engine() Engine {
51-
return ctx.e
28+
func newContext(ctx context.Context, e Engine) *Context {
29+
return &Context{Context: ctx, engine: e}
5230
}
5331

5432
// Value shadows Value for context.Context but allows us to get ourselves and an Engined object
5533
func (ctx *Context) Value(key any) any {
56-
if key == enginedContextKey {
34+
if key == engineContextKey {
5735
return ctx
5836
}
5937
return ctx.Context.Value(key)
6038
}
6139

6240
// WithContext returns this engine tied to this context
6341
func (ctx *Context) WithContext(other context.Context) *Context {
64-
return newContext(ctx, ctx.e.Context(other), ctx.transaction)
65-
}
66-
67-
// Engined structs provide an Engine
68-
type Engined interface {
69-
Engine() Engine
42+
return newContext(ctx, ctx.engine.Context(other))
7043
}
7144

7245
// GetEngine will get a db Engine from this context or return an Engine restricted to this context
@@ -79,12 +52,11 @@ func GetEngine(ctx context.Context) Engine {
7952

8053
// getEngine will get a db Engine from this context or return nil
8154
func getEngine(ctx context.Context) Engine {
82-
if engined, ok := ctx.(Engined); ok {
83-
return engined.Engine()
55+
if engined, ok := ctx.(*Context); ok {
56+
return engined.engine
8457
}
85-
enginedInterface := ctx.Value(enginedContextKey)
86-
if enginedInterface != nil {
87-
return enginedInterface.(Engined).Engine()
58+
if engined, ok := ctx.Value(engineContextKey).(*Context); ok {
59+
return engined.engine
8860
}
8961
return nil
9062
}
@@ -132,23 +104,23 @@ func (c *halfCommitter) Close() error {
132104
// d. It doesn't mean rollback is forbidden, but always do it only when there is an error, and you do want to rollback.
133105
func TxContext(parentCtx context.Context) (*Context, Committer, error) {
134106
if sess, ok := inTransaction(parentCtx); ok {
135-
return newContext(parentCtx, sess, true), &halfCommitter{committer: sess}, nil
107+
return newContext(parentCtx, sess), &halfCommitter{committer: sess}, nil
136108
}
137109

138110
sess := x.NewSession()
139111
if err := sess.Begin(); err != nil {
140-
sess.Close()
112+
_ = sess.Close()
141113
return nil, nil, err
142114
}
143115

144-
return newContext(DefaultContext, sess, true), sess, nil
116+
return newContext(DefaultContext, sess), sess, nil
145117
}
146118

147119
// WithTx represents executing database operations on a transaction, if the transaction exist,
148120
// this function will reuse it otherwise will create a new one and close it when finished.
149121
func WithTx(parentCtx context.Context, f func(ctx context.Context) error) error {
150122
if sess, ok := inTransaction(parentCtx); ok {
151-
err := f(newContext(parentCtx, sess, true))
123+
err := f(newContext(parentCtx, sess))
152124
if err != nil {
153125
// rollback immediately, in case the caller ignores returned error and tries to commit the transaction.
154126
_ = sess.Close()
@@ -165,7 +137,7 @@ func txWithNoCheck(parentCtx context.Context, f func(ctx context.Context) error)
165137
return err
166138
}
167139

168-
if err := f(newContext(parentCtx, sess, true)); err != nil {
140+
if err := f(newContext(parentCtx, sess)); err != nil {
169141
return err
170142
}
171143

models/db/engine.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,7 @@ func InitEngine(ctx context.Context) error {
161161
// SetDefaultEngine sets the default engine for db
162162
func SetDefaultEngine(ctx context.Context, eng *xorm.Engine) {
163163
x = eng
164-
DefaultContext = &Context{
165-
Context: ctx,
166-
e: x,
167-
}
164+
DefaultContext = &Context{Context: ctx, engine: x}
168165
}
169166

170167
// UnsetDefaultEngine closes and unsets the default engine

models/db/install/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func getXORMEngine() *xorm.Engine {
14-
return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine)
14+
return db.GetEngine(db.DefaultContext).(*xorm.Engine)
1515
}
1616

1717
// CheckDatabaseConnection checks the database connection

models/db/iterate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"xorm.io/builder"
1212
)
1313

14-
// Iterate iterate all the Bean object
14+
// Iterate iterates all the Bean object
1515
func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx context.Context, bean *Bean) error) error {
1616
var start int
1717
batchSize := setting.Database.IterateBufferSize

models/unittest/fixtures.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
2525
if len(engine) == 1 {
2626
return engine[0]
2727
}
28-
return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine)
28+
return db.GetEngine(db.DefaultContext).(*xorm.Engine)
2929
}
3030

3131
// InitFixtures initialize test fixtures for a test database

services/packages/cleanup/cleanup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
rpm_service "code.gitea.io/gitea/services/packages/rpm"
2323
)
2424

25-
// Task method to execute cleanup rules and cleanup expired package data
25+
// CleanupTask executes cleanup rules and cleanup expired package data
2626
func CleanupTask(ctx context.Context, olderThan time.Duration) error {
2727
if err := ExecuteCleanupRules(ctx); err != nil {
2828
return err

tests/integration/api_packages_debian_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"net/http"
13+
"strconv"
1314
"strings"
1415
"testing"
1516

@@ -19,6 +20,9 @@ import (
1920
user_model "code.gitea.io/gitea/models/user"
2021
"code.gitea.io/gitea/modules/base"
2122
debian_module "code.gitea.io/gitea/modules/packages/debian"
23+
"code.gitea.io/gitea/modules/setting"
24+
"code.gitea.io/gitea/modules/test"
25+
packages_cleanup_service "code.gitea.io/gitea/services/packages/cleanup"
2226
"code.gitea.io/gitea/tests"
2327

2428
"github.com/blakesmith/ar"
@@ -263,4 +267,34 @@ func TestPackageDebian(t *testing.T) {
263267
assert.Contains(t, body, "Components: "+strings.Join(components, " ")+"\n")
264268
assert.Contains(t, body, "Architectures: "+architectures[1]+"\n")
265269
})
270+
271+
t.Run("Cleanup", func(t *testing.T) {
272+
defer tests.PrintCurrentTest(t)()
273+
274+
defer test.MockVariableValue(&setting.Database.IterateBufferSize, 100)()
275+
rule := &packages.PackageCleanupRule{
276+
Enabled: true,
277+
RemovePattern: `.*`,
278+
MatchFullName: true,
279+
OwnerID: user.ID,
280+
Type: packages.TypeDebian,
281+
}
282+
283+
_, err := packages.InsertCleanupRule(db.DefaultContext, rule)
284+
assert.NoError(t, err)
285+
286+
for i := 0; i < setting.Database.IterateBufferSize*3/2; i++ {
287+
uploadURL := fmt.Sprintf("%s/pool/%s/%s/upload", rootURL, "test", "main")
288+
req := NewRequestWithBody(t, "PUT", uploadURL, createArchive(packageName, "1.0."+strconv.Itoa(i), "all")).AddBasicAuth(user.Name)
289+
MakeRequest(t, req, http.StatusCreated)
290+
}
291+
req := NewRequest(t, "GET", fmt.Sprintf("%s/dists/%s/Release", rootURL, "test"))
292+
MakeRequest(t, req, http.StatusOK)
293+
294+
err = packages_cleanup_service.CleanupTask(db.DefaultContext, 0)
295+
assert.NoError(t, err)
296+
297+
req = NewRequest(t, "GET", fmt.Sprintf("%s/dists/%s/Release", rootURL, "test"))
298+
MakeRequest(t, req, http.StatusNotFound)
299+
})
266300
}

0 commit comments

Comments
 (0)