From 51d8501642523767f398bae38aa268f8e039eaee Mon Sep 17 00:00:00 2001 From: nejtr0n Date: Mon, 24 Jul 2023 12:09:22 +0300 Subject: [PATCH 1/2] Fix data race Test results should be protected by mutex to prevent datarace in parallel tests --- pkg/framework/runner/tests.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/framework/runner/tests.go b/pkg/framework/runner/tests.go index 6445f2c..4a88460 100644 --- a/pkg/framework/runner/tests.go +++ b/pkg/framework/runner/tests.go @@ -12,6 +12,7 @@ import ( type suiteResult struct { Container *allure.Container `json:"container,omitempty"` TestResults []TestResult `json:"test_results,omitempty"` + mu sync.Mutex } // NewSuiteResult Returns new SuiteResult @@ -21,7 +22,9 @@ func NewSuiteResult(container *allure.Container) SuiteResult { // NewResult appends test result to suite result func (sr *suiteResult) NewResult(result TestResult) { + sr.mu.Lock() sr.TestResults = append(sr.TestResults, result) + sr.mu.Unlock() } // GetContainer returns parent Container From eba053b5a1772bffedd490da5abd466212569398 Mon Sep 17 00:00:00 2001 From: Alexey Shishkin Date: Mon, 24 Jul 2023 18:24:43 +0300 Subject: [PATCH 2/2] fix defer --- pkg/framework/runner/tests.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/framework/runner/tests.go b/pkg/framework/runner/tests.go index 4a88460..d150213 100644 --- a/pkg/framework/runner/tests.go +++ b/pkg/framework/runner/tests.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "reflect" + "sync" "github.com/ozontech/allure-go/pkg/allure" "github.com/ozontech/allure-go/pkg/framework/provider" @@ -12,7 +13,7 @@ import ( type suiteResult struct { Container *allure.Container `json:"container,omitempty"` TestResults []TestResult `json:"test_results,omitempty"` - mu sync.Mutex + mu sync.Mutex } // NewSuiteResult Returns new SuiteResult @@ -23,8 +24,9 @@ func NewSuiteResult(container *allure.Container) SuiteResult { // NewResult appends test result to suite result func (sr *suiteResult) NewResult(result TestResult) { sr.mu.Lock() + defer sr.mu.Unlock() + sr.TestResults = append(sr.TestResults, result) - sr.mu.Unlock() } // GetContainer returns parent Container