Fix data race in bulk import tests (#24897)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
1a12faaaae
Коммит
4408ece955
@@ -268,7 +268,7 @@ func (a *App) bulkImport(c request.CTX, jsonlReader io.Reader, attachmentsReader
|
|||||||
linesChan = make(chan imports.LineImportWorkerData, workers)
|
linesChan = make(chan imports.LineImportWorkerData, workers)
|
||||||
for i := 0; i < workers; i++ {
|
for i := 0; i < workers; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go a.bulkImportWorker(c, dryRun, &wg, linesChan, errorsChan)
|
go a.bulkImportWorker(c.Clone(), dryRun, &wg, linesChan, errorsChan)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mattermost/mattermost/server/public/shared/request"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,3 +17,48 @@ func TestContextMaster(t *testing.T) {
|
|||||||
m := WithMaster(ctx)
|
m := WithMaster(ctx)
|
||||||
assert.True(t, HasMaster(m))
|
assert.True(t, HasMaster(m))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRequestContextWithMaster(t *testing.T) {
|
||||||
|
t.Run("set and get", func(t *testing.T) {
|
||||||
|
var rctx request.CTX = request.TestContext(t)
|
||||||
|
|
||||||
|
rctx = RequestContextWithMaster(rctx)
|
||||||
|
assert.True(t, HasMaster(rctx.Context()))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("directly assigning does cause the child to alter the parent", func(t *testing.T) {
|
||||||
|
var rctx request.CTX = request.TestContext(t)
|
||||||
|
rctxClone := rctx
|
||||||
|
rctxClone = RequestContextWithMaster(rctxClone)
|
||||||
|
|
||||||
|
assert.True(t, HasMaster(rctx.Context()))
|
||||||
|
assert.True(t, HasMaster(rctxClone.Context()))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("values get copied from parent", func(t *testing.T) {
|
||||||
|
var rctx request.CTX = request.TestContext(t)
|
||||||
|
rctx = RequestContextWithMaster(rctx)
|
||||||
|
rctxClone := rctx.Clone()
|
||||||
|
|
||||||
|
assert.True(t, HasMaster(rctx.Context()))
|
||||||
|
assert.True(t, HasMaster(rctxClone.Context()))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("changing the child does not alter the parent", func(t *testing.T) {
|
||||||
|
var rctx request.CTX = request.TestContext(t)
|
||||||
|
rctxClone := rctx.Clone()
|
||||||
|
rctxClone = RequestContextWithMaster(rctxClone)
|
||||||
|
|
||||||
|
assert.False(t, HasMaster(rctx.Context()))
|
||||||
|
assert.True(t, HasMaster(rctxClone.Context()))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("changing the parent does not alter the child", func(t *testing.T) {
|
||||||
|
var rctx request.CTX = request.TestContext(t)
|
||||||
|
rctxClone := rctx.Clone()
|
||||||
|
rctx = RequestContextWithMaster(rctx)
|
||||||
|
|
||||||
|
assert.True(t, HasMaster(rctx.Context()))
|
||||||
|
assert.False(t, HasMaster(rctxClone.Context()))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -54,6 +54,12 @@ func TestContext(t testing.TB) *Context {
|
|||||||
return EmptyContext(logger)
|
return EmptyContext(logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone creates a shallow copy of Context, allowing clones to apply per-request changes.
|
||||||
|
func (c *Context) Clone() CTX {
|
||||||
|
cCopy := *c
|
||||||
|
return &cCopy
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Context) T(translationID string, args ...any) string {
|
func (c *Context) T(translationID string, args ...any) string {
|
||||||
return c.t(translationID, args...)
|
return c.t(translationID, args...)
|
||||||
}
|
}
|
||||||
@@ -125,6 +131,7 @@ func (c *Context) Logger() mlog.LoggerIFace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CTX interface {
|
type CTX interface {
|
||||||
|
Clone() CTX
|
||||||
T(string, ...interface{}) string
|
T(string, ...interface{}) string
|
||||||
Session() *model.Session
|
Session() *model.Session
|
||||||
RequestId() string
|
RequestId() string
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user