Fix data race in bulk import tests (#24897)

This commit is contained in:
Ben Schumacher 2023-10-16 22:40:19 +02:00 committed by GitHub
parent 1a12faaaae
commit 4408ece955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 1 deletions

View File

@ -268,7 +268,7 @@ func (a *App) bulkImport(c request.CTX, jsonlReader io.Reader, attachmentsReader
linesChan = make(chan imports.LineImportWorkerData, workers)
for i := 0; i < workers; i++ {
wg.Add(1)
go a.bulkImportWorker(c, dryRun, &wg, linesChan, errorsChan)
go a.bulkImportWorker(c.Clone(), dryRun, &wg, linesChan, errorsChan)
}
}

View File

@ -7,6 +7,7 @@ import (
"context"
"testing"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/stretchr/testify/assert"
)
@ -16,3 +17,48 @@ func TestContextMaster(t *testing.T) {
m := WithMaster(ctx)
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()))
})
}

View File

@ -54,6 +54,12 @@ func TestContext(t testing.TB) *Context {
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 {
return c.t(translationID, args...)
}
@ -125,6 +131,7 @@ func (c *Context) Logger() mlog.LoggerIFace {
}
type CTX interface {
Clone() CTX
T(string, ...interface{}) string
Session() *model.Session
RequestId() string