diff --git a/cmd/run.go b/cmd/run.go index b1f5ab24073..d1338ba1d94 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -177,9 +177,12 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { stopOutputs(err) }() - if !testRunState.RuntimeOptions.NoThresholds.Bool { + if !testRunState.RuntimeOptions.NoThresholds.Bool { //nolint:nestif finalizeThresholds := metricsEngine.StartThresholdCalculations(runAbort) defer func() { + if finalizeThresholds == nil { + return + } // This gets called after the Samples channel has been closed and // the OutputManager has flushed all of the cached samples to // outputs (including MetricsEngine's ingester). So we are sure diff --git a/cmd/tests/cmd_run_test.go b/cmd/tests/cmd_run_test.go index 36955ee5af7..8f42ff1d8a8 100644 --- a/cmd/tests/cmd_run_test.go +++ b/cmd/tests/cmd_run_test.go @@ -533,6 +533,8 @@ func TestSetupTeardownThresholds(t *testing.T) { assert.Failf(t, "unexpected log message", "level %s, msg '%s'", msg.Level, msg.Message) } } + assert.True(t, testutils.LogContains(logMsgs, logrus.DebugLevel, "Running thresholds on 3 metrics...")) + assert.True(t, testutils.LogContains(logMsgs, logrus.DebugLevel, "Finalizing thresholds...")) assert.True(t, testutils.LogContains(logMsgs, logrus.DebugLevel, "Metrics emission of VUs and VUsMax metrics stopped")) assert.True(t, testutils.LogContains(logMsgs, logrus.DebugLevel, "Metrics processing finished!")) } @@ -799,6 +801,8 @@ func TestAbortedByUserWithRestAPI(t *testing.T) { assert.Contains(t, stdout, `level=debug msg="Metrics emission of VUs and VUsMax metrics stopped"`) assert.Contains(t, stdout, `level=debug msg="Metrics processing finished!"`) assert.Contains(t, stdout, `level=debug msg="Sending test finished" output=cloud ref=111 run_status=5 tainted=false`) + assert.NotContains(t, stdout, `Running thresholds`) + assert.NotContains(t, stdout, `Finalizing thresholds`) } func TestAbortedByScriptSetupErrorWithDependency(t *testing.T) { diff --git a/js/runner_test.go b/js/runner_test.go index f734c47e124..ffb5960fe03 100644 --- a/js/runner_test.go +++ b/js/runner_test.go @@ -402,6 +402,7 @@ func TestDataIsolation(t *testing.T) { defer stopOutputs(nil) finalizeThresholds := metricsEngine.StartThresholdCalculations(runAbort) + require.Nil(t, finalizeThresholds) require.Empty(t, runner.defaultGroup.Groups) @@ -416,8 +417,6 @@ func TestDataIsolation(t *testing.T) { close(samples) require.NoError(t, err) waitForMetricsFlushed() - breached := finalizeThresholds() - require.Empty(t, breached) } require.Contains(t, runner.defaultGroup.Groups, "setup") require.Contains(t, runner.defaultGroup.Groups, "teardown") diff --git a/metrics/engine/engine.go b/metrics/engine/engine.go index dc08acd71fe..3fa561409d1 100644 --- a/metrics/engine/engine.go +++ b/metrics/engine/engine.go @@ -169,6 +169,10 @@ func (me *MetricsEngine) initSubMetricsAndThresholds() error { func (me *MetricsEngine) StartThresholdCalculations(abortRun func(error)) ( finalize func() (breached []string), ) { + if len(me.metricsWithThresholds) == 0 { + return nil // no thresholds were defined + } + stop := make(chan struct{}) done := make(chan struct{})