mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Fix data race in bulk import tests (#24897)
This commit is contained in:
parent
1a12faaaae
commit
4408ece955
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()))
|
||||
})
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user