From fc60a1e8dd9d1a225687aead0684035ee63d7562 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 2 Dec 2020 14:22:41 +0530 Subject: [PATCH] MM-30988 - Fix racy test ServerSystemdNotification (#16431) * MM-30988 - Fix racy test ServerSystemdNotification The translateFunc is a global variable which was unguarded. So we convert that into an atomic variable to prevent against races. https://mattermost.atlassian.net/browse/MM-30988 ```release-note NONE ``` * fix tests * move call upwards * use race * after translations init * guard with a sync.Once * missed import * revert race * update timeout --- app/server.go | 3 +-- model/utils.go | 8 ++++++-- scripts/test.sh | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/server.go b/app/server.go index 71850c34ae..505ac49689 100644 --- a/app/server.go +++ b/app/server.go @@ -269,6 +269,7 @@ func NewServer(options ...Option) (*Server, error) { if err := utils.TranslationsPreInit(); err != nil { return nil, errors.Wrapf(err, "unable to load Mattermost translation files") } + model.AppErrorInit(utils.T) searchEngine := searchengine.NewBroker(s.Config(), s.Jobs) bleveEngine := bleveengine.NewBleveEngine(s.Config(), s.Jobs) @@ -450,8 +451,6 @@ func NewServer(options ...Option) (*Server, error) { mlog.Error("Problem with file storage settings", mlog.Err(appErr)) } - model.AppErrorInit(utils.T) - s.timezones = timezones.New() // Start email batching because it's not like the other jobs s.AddConfigListener(func(_, _ *model.Config) { diff --git a/model/utils.go b/model/utils.go index 3aed19da15..172e8de19e 100644 --- a/model/utils.go +++ b/model/utils.go @@ -18,6 +18,7 @@ import ( "regexp" "strconv" "strings" + "sync" "time" "unicode" @@ -72,10 +73,13 @@ func (sa StringArray) Equals(input StringArray) bool { return true } -var translateFunc goi18n.TranslateFunc = nil +var translateFunc goi18n.TranslateFunc +var translateFuncOnce sync.Once func AppErrorInit(t goi18n.TranslateFunc) { - translateFunc = t + translateFuncOnce.Do(func() { + translateFunc = t + }) } type AppError struct { diff --git a/scripts/test.sh b/scripts/test.sh index 74afbf5390..32251e9005 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -16,7 +16,7 @@ echo "Packages to test: $PACKAGES" find . -name 'cprofile*.out' -exec sh -c 'rm "{}"' \; find . -type d -name data -not -path './vendor/*' -not -path './data' | xargs rm -rf -$GO test $GOFLAGS -run=$TESTS $TESTFLAGS -v -timeout=2000s -covermode=count -coverpkg=$PACKAGES_COMMA -exec $DIR/test-xprog.sh $PACKAGES 2>&1 > >( tee output ) +$GO test $GOFLAGS -run=$TESTS $TESTFLAGS -v -timeout=20m -covermode=count -coverpkg=$PACKAGES_COMMA -exec $DIR/test-xprog.sh $PACKAGES 2>&1 > >( tee output ) EXIT_STATUS=$? cat output | $GOBIN/go-junit-report > report.xml