From e32d8bb13d005733019e6b088fedf3bee34a90da Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 11 Sep 2024 10:23:08 +0200 Subject: [PATCH] internal/civisibility/integrations/gotesting: change ddTestItem.error and ddTestItem.skipped bool type to an atomic.Int32 to avoid any race condition. --- .../integrations/gotesting/instrumentation.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/internal/civisibility/integrations/gotesting/instrumentation.go b/internal/civisibility/integrations/gotesting/instrumentation.go index 8836df0d7e..6077c34e10 100644 --- a/internal/civisibility/integrations/gotesting/instrumentation.go +++ b/internal/civisibility/integrations/gotesting/instrumentation.go @@ -34,8 +34,8 @@ type ( ddTestItem struct { test integrations.DdTest - error bool - skipped bool + error atomic.Int32 + skipped atomic.Int32 } ) @@ -205,8 +205,7 @@ func instrumentTestingTFunc(f func(*testing.T)) func(*testing.T) { // instrumentSetErrorInfo helper function to set an error in the `testing.T or testing.B` CI Visibility span func instrumentSetErrorInfo(tb testing.TB, errType string, errMessage string, skip int) { ciTestItem := getCiVisibilityTest(tb) - if ciTestItem != nil && !ciTestItem.error && ciTestItem.test != nil { - ciTestItem.error = true + if ciTestItem != nil && ciTestItem.error.CompareAndSwap(0, 1) && ciTestItem.test != nil { ciTestItem.test.SetErrorInfo(errType, errMessage, utils.GetStacktrace(2+skip)) } } @@ -214,8 +213,7 @@ func instrumentSetErrorInfo(tb testing.TB, errType string, errMessage string, sk // instrumentCloseAndSkip helper function to close and skip with a reason a `testing.T or testing.B` CI Visibility span func instrumentCloseAndSkip(tb testing.TB, skipReason string) { ciTestItem := getCiVisibilityTest(tb) - if ciTestItem != nil && !ciTestItem.skipped && ciTestItem.test != nil { - ciTestItem.skipped = true + if ciTestItem != nil && ciTestItem.skipped.CompareAndSwap(0, 1) && ciTestItem.test != nil { ciTestItem.test.CloseWithFinishTimeAndSkipReason(integrations.ResultStatusSkip, time.Now(), skipReason) } } @@ -223,8 +221,7 @@ func instrumentCloseAndSkip(tb testing.TB, skipReason string) { // instrumentSkipNow helper function to close and skip a `testing.T or testing.B` CI Visibility span func instrumentSkipNow(tb testing.TB) { ciTestItem := getCiVisibilityTest(tb) - if ciTestItem != nil && !ciTestItem.skipped && ciTestItem.test != nil { - ciTestItem.skipped = true + if ciTestItem != nil && ciTestItem.skipped.CompareAndSwap(0, 1) && ciTestItem.test != nil { ciTestItem.test.Close(integrations.ResultStatusSkip) } }