Address data race in pkg/locale

This commit is contained in:
Denis Arh
2022-04-04 16:26:26 +02:00
parent 0792c0a17d
commit 3450509909
2 changed files with 31 additions and 0 deletions
+3
View File
@@ -421,6 +421,9 @@ func (svc *service) TResourceFor(tag language.Tag, ns, key string, rr ...string)
func (svc *service) ResourceTranslations(tag language.Tag, resource string) ResourceTranslationIndex {
out := make(ResourceTranslationIndex)
svc.l.Lock()
defer svc.l.Unlock()
if svc != nil && svc.set != nil {
if l, has := svc.set[tag]; has {
return l.resourceTranslations(resource)
+28
View File
@@ -0,0 +1,28 @@
package locale
import (
"testing"
"github.com/cortezaproject/corteza-server/pkg/options"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"golang.org/x/text/language"
)
// tested with
// go test -count 10 -race -run TestServiceReloadAndTranslate ./pkg/locale/...
func TestServiceReloadAndTranslate(t *testing.T) {
var (
req = require.New(t)
svc, err = Service(zap.NewNop(), options.LocaleOpt{
Languages: "en",
})
tag = language.English
)
req.NoError(err)
req.NotNil(svc)
go svc.ResourceTranslations(tag, "resource")
go svc.ReloadStatic()
}