Skip to content

Commit

Permalink
fix(metrics): fix race condition in metrics.Init()
Browse files Browse the repository at this point in the history
IterationMetricsEnabled can be concurrently set

```
==================
WARNING: DATA RACE
Write at 0x00c000124098 by goroutine 12:
  github.com/form3tech-oss/f1/v2/internal/metrics.Init()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics.go:82 +0x7c
  github.com/form3tech-oss/f1/v2/internal/metrics_test.TestMetrics_Init_IsSafe.func1()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics_test.go:15 +0x20

Previous write at 0x00c000124098 by goroutine 14:
  github.com/form3tech-oss/f1/v2/internal/metrics.Init()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics.go:82 +0x7c
  github.com/form3tech-oss/f1/v2/internal/metrics_test.TestMetrics_Init_IsSafe.func1()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics_test.go:15 +0x20

Goroutine 12 (running) created at:
  github.com/form3tech-oss/f1/v2/internal/metrics_test.TestMetrics_Init_IsSafe()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics_test.go:14 +0xa4
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.23.1/libexec/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /opt/homebrew/Cellar/go/1.23.1/libexec/src/testing/testing.go:1743 +0x40

Goroutine 14 (finished) created at:
  github.com/form3tech-oss/f1/v2/internal/metrics_test.TestMetrics_Init_IsSafe()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics_test.go:14 +0xa4
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.23.1/libexec/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /opt/homebrew/Cellar/go/1.23.1/libexec/src/testing/testing.go:1743 +0x40
```
  • Loading branch information
nvloff-f3 committed Sep 11, 2024
1 parent f016ea2 commit c5571c9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 0 additions & 1 deletion internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func Init(iterationMetricsEnabled bool) {
}
m = NewInstance(defaultRegistry, iterationMetricsEnabled)
})
m.IterationMetricsEnabled = iterationMetricsEnabled
}

func Instance() *Metrics {
Expand Down
21 changes: 21 additions & 0 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package metrics_test

import (
"testing"

"github.com/form3tech-oss/f1/v2/internal/metrics"
"github.com/stretchr/testify/assert"
)

func TestMetrics_Init_IsSafe(t *testing.T) {

Check failure on line 10 in internal/metrics/metrics_test.go

View workflow job for this annotation

GitHub Actions / Test

Function TestMetrics_Init_IsSafe missing the call to method parallel (paralleltest)
metrics.Init(true)

// race detector assertion
for range 10 {
go func() {
metrics.Init(false)
}()
}

assert.True(t, metrics.Instance().IterationMetricsEnabled)
}
15 changes: 15 additions & 0 deletions pkg/f1/f1_scenarios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@ func TestCombineScenarios(t *testing.T) {
then.
each_scenarios_setup_and_iteration_functions_are_called()
}

func TestCombineScenarios_1(t *testing.T) {
t.Parallel()

given, when, then := newF1ScenarioStage(t)

given.
f1_is_configured_to_run_a_combined_scenario()

when.
the_f1_scenario_is_executed()

then.
each_scenarios_setup_and_iteration_functions_are_called()
}

0 comments on commit c5571c9

Please sign in to comment.