Open platform to collect and prioritize feedback
bugfix: turn on race condition flag and add mutex everywhere (#427)
Guilherme Oenningf69cfaad0c25
diff --git a/Makefile b/Makefileindex 141e1cc87..dce9667cd 100644--- a/Makefile+++ b/Makefile@@ -18,12 +18,12 @@ test-ui:TZ='GMT' npx jest ./publictest-server:- godotenv -f .test.env go test ./...+ godotenv -f .test.env go test ./... -racetest : test-server test-uicoverage:- godotenv -f .test.env go test ./... -coverprofile=cover.out -coverpkg=all+ godotenv -f .test.env go test ./... -coverprofile=cover.out -coverpkg=all -racee2e-single:./scripts/e2e.sh singlediff --git a/app/pkg/assert/assert_test.go b/app/pkg/assert/assert_test.goindex 971fac2bd..850916e73 100644--- a/app/pkg/assert/assert_test.go+++ b/app/pkg/assert/assert_test.go@@ -1,6 +1,7 @@package assert_testimport (+ "sync""testing""time"@@ -115,14 +116,19 @@ func TestFuncPanics(t *testing.T) {func TestFuncEventuallyEquals(t *testing.T) {mockT := new(testing.T)RegisterT(mockT)+ mu := &sync.RWMutex{}completed := falsego func() {time.Sleep(500 * time.Millisecond)+ mu.Lock()+ defer mu.Unlock()completed = true}()if !Expect(func() bool {+ mu.RLock()+ defer mu.RUnlock()return completed}).EventuallyEquals(true) {t.Error("EventuallyEquals assertion failed")diff --git a/app/pkg/dbx/mapping.go b/app/pkg/dbx/mapping.goindex 351a216e3..acf6dc49d 100644--- a/app/pkg/dbx/mapping.go+++ b/app/pkg/dbx/mapping.go@@ -3,6 +3,7 @@ package dbximport ("fmt""reflect"+ "sync""github.com/lib/pq")@@ -10,6 +11,7 @@ import (//RowMapper is resposible for mapping a sql.Rows into a Struct (model)type RowMapper struct {cache map[reflect.Type]TypeMapper+ sync.RWMutex}//NewRowMapper creates a new instance of RowMapper@@ -31,9 +33,15 @@ func (m *RowMapper) Map(dest interface{}, columns []string, scanner func(dest ..ok bool)- if typeMapper, ok = m.cache[t]; !ok {+ m.RLock()+ typeMapper, ok = m.cache[t]+ m.RUnlock()++ if !ok {typeMapper = NewTypeMapper(t)+ m.Lock()m.cache[t] = typeMapper+ m.Unlock()}pointers := make([]interface{}, len(columns))diff --git a/app/pkg/dbx/mapping_test.go b/app/pkg/dbx/mapping_test.goindex a5f370d9a..33621c791 100644--- a/app/pkg/dbx/mapping_test.go+++ b/app/pkg/dbx/mapping_test.go@@ -2,6 +2,7 @@ package dbx_testimport ("reflect"+ "sync""testing". "github.com/getfider/fider/app/pkg/assert"@@ -73,6 +74,21 @@ func TestRowMapper_NonStruct(t *testing.T) {Expect(id).Equals(5)}+func TestRowMapper_Concurrent(t *testing.T) {+ RegisterT(t)+ var wg sync.WaitGroup+ mapper := dbx.NewRowMapper()+ for i := 0; i < 5; i++ {+ wg.Add(1)+ go func() {+ var id int+ mapper.Map(&id, []string{"id"}, createScanner(5))+ wg.Done()+ }()+ }+ wg.Wait()+}+func TestRowMapper_SimpleStruct(t *testing.T) {RegisterT(t)mapper := dbx.NewRowMapper()diff --git a/app/pkg/worker/worker.go b/app/pkg/worker/worker.goindex c2e8cdb1c..6fe1b3914 100644--- a/app/pkg/worker/worker.go+++ b/app/pkg/worker/worker.go@@ -3,7 +3,7 @@ package workerimport ("context""errors"- "sync/atomic"+ "sync""time""github.com/getfider/fider/app/pkg/log"@@ -37,6 +37,7 @@ type BackgroundWorker struct {queue chan Tasklen int64middleware MiddlewareFunc+ sync.RWMutex}var maxQueueSize = 100@@ -64,7 +65,9 @@ func (w *BackgroundWorker) Run(id string) {}w.middleware(task.Job)(c)- atomic.AddInt64(&w.len, -1)+ w.Lock()+ w.len = w.len - 1+ w.Unlock()}}@@ -91,7 +94,9 @@ func (w *BackgroundWorker) Shutdown(ctx context.Context) error {//Enqueue a task on current workerfunc (w *BackgroundWorker) Enqueue(task Task) {- atomic.AddInt64(&w.len, 1)+ w.Lock()+ w.len = w.len + 1+ w.Unlock()w.queue <- task}@@ -102,6 +107,8 @@ func (w *BackgroundWorker) Logger() log.Logger {//Length from current queue lengthfunc (w *BackgroundWorker) Length() int64 {+ w.RLock()+ defer w.RUnlock()return w.len}diff --git a/app/pkg/worker/worker_test.go b/app/pkg/worker/worker_test.goindex 26b284b60..fa2625fbd 100644--- a/app/pkg/worker/worker_test.go+++ b/app/pkg/worker/worker_test.go@@ -2,6 +2,7 @@ package worker_testimport ("context"+ "sync""testing""time"@@ -21,18 +22,24 @@ func TestBackgroundWorker(t *testing.T) {RegisterT(t)var finished bool+ mu := &sync.RWMutex{}w := worker.New()w.Enqueue(worker.Task{Name: "Do Something",Job: func(c *worker.Context) error {+ mu.Lock()+ defer mu.Unlock()finished = truereturn nil},})+Expect(w.Length()).Equals(int64(1))go w.Run("worker-1")Expect(func() bool {+ mu.RLock()+ defer mu.RUnlock()return finished}).EventuallyEquals(true)}