[MM-60685] Fix racy TestSyncLdap (#28324)

Этот коммит содержится в:
Ben Schumacher
2024-10-14 13:04:48 +02:00
коммит произвёл GitHub
родитель 5f8bdba459
Коммит 7ff22436dc
4 изменённых файлов: 20 добавлений и 8 удалений

Просмотреть файл

@@ -352,7 +352,7 @@ type AppIface interface {
// SyncLdap starts an LDAP sync job. // SyncLdap starts an LDAP sync job.
// If includeRemovedMembers is true, then members who left or were removed from a team/channel will // If includeRemovedMembers is true, then members who left or were removed from a team/channel will
// be re-added; otherwise, they will not be re-added. // be re-added; otherwise, they will not be re-added.
SyncLdap(c request.CTX, includeRemovedMembers bool) SyncLdap(rctx request.CTX, includeRemovedMembers bool)
// SyncPlugins synchronizes the plugins installed locally // SyncPlugins synchronizes the plugins installed locally
// with the plugin bundles available in the file store. // with the plugin bundles available in the file store.
SyncPlugins() *model.AppError SyncPlugins() *model.AppError

Просмотреть файл

@@ -17,20 +17,21 @@ import (
// SyncLdap starts an LDAP sync job. // SyncLdap starts an LDAP sync job.
// If includeRemovedMembers is true, then members who left or were removed from a team/channel will // If includeRemovedMembers is true, then members who left or were removed from a team/channel will
// be re-added; otherwise, they will not be re-added. // be re-added; otherwise, they will not be re-added.
func (a *App) SyncLdap(c request.CTX, includeRemovedMembers bool) { func (a *App) SyncLdap(rctx request.CTX, includeRemovedMembers bool) {
rctx = rctx.Clone()
a.Srv().Go(func() { a.Srv().Go(func() {
if license := a.Srv().License(); license != nil && *license.Features.LDAP { if license := a.Srv().License(); license != nil && *license.Features.LDAP {
if !*a.Config().LdapSettings.EnableSync { if !*a.Config().LdapSettings.EnableSync {
c.Logger().Error("LdapSettings.EnableSync is set to false. Skipping LDAP sync.") rctx.Logger().Error("LdapSettings.EnableSync is set to false. Skipping LDAP sync.")
return return
} }
ldapI := a.Ldap() ldapI := a.Ldap()
if ldapI == nil { if ldapI == nil {
c.Logger().Error("Not executing ldap sync because ldap is not available") rctx.Logger().Error("Not executing ldap sync because ldap is not available")
return return
} }
ldapI.StartSynchronizeJob(c, false, includeRemovedMembers) ldapI.StartSynchronizeJob(rctx, false, includeRemovedMembers)
} }
}) })
} }

Просмотреть файл

@@ -17464,7 +17464,7 @@ func (a *OpenTracingAppLayer) SwitchOAuthToEmail(c request.CTX, email string, pa
return resultVar0, resultVar1 return resultVar0, resultVar1
} }
func (a *OpenTracingAppLayer) SyncLdap(c request.CTX, includeRemovedMembers bool) { func (a *OpenTracingAppLayer) SyncLdap(rctx request.CTX, includeRemovedMembers bool) {
origCtx := a.ctx origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SyncLdap") span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SyncLdap")
@@ -17476,7 +17476,7 @@ func (a *OpenTracingAppLayer) SyncLdap(c request.CTX, includeRemovedMembers bool
}() }()
defer span.Finish() defer span.Finish()
a.app.SyncLdap(c, includeRemovedMembers) a.app.SyncLdap(rctx, includeRemovedMembers)
} }
func (a *OpenTracingAppLayer) SyncPlugins() *model.AppError { func (a *OpenTracingAppLayer) SyncPlugins() *model.AppError {

Просмотреть файл

@@ -54,9 +54,19 @@ 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. // Clone creates a deep copy of [CTX].
// It should only be used to pass a [CTX] to a separate goroutine that
// has a longer lifespan than the main goroutine handling the request.
// It should be used sparsely as coping [CTX] is often unnecessary.
func (c *Context) Clone() CTX {
return c.clone()
}
// clone creates a deep copy of [Context], allowing clones to apply per-request changes.
// It unexported to prevent leaking the [Context] type from the [CTX] interface.
func (c *Context) clone() *Context { func (c *Context) clone() *Context {
cCopy := *c cCopy := *c
cCopy.session = *c.session.DeepCopy()
return &cCopy return &cCopy
} }
@@ -173,4 +183,5 @@ type CTX interface {
WithLogger(mlog.LoggerIFace) CTX WithLogger(mlog.LoggerIFace) CTX
WithContext(ctx context.Context) CTX WithContext(ctx context.Context) CTX
With(func(ctx CTX) CTX) CTX With(func(ctx CTX) CTX) CTX
Clone() CTX
} }