From 9fd527bfe7e35d78bffac2d1d9219e9e0f079095 Mon Sep 17 00:00:00 2001 From: Nedyalko Andreev Date: Wed, 25 Jan 2023 18:57:09 +0200 Subject: [PATCH] Process thresholds only if some were defined --- cmd/run.go | 5 ++++- cmd/tests/cmd_run_test.go | 4 ++++ js/runner_test.go | 3 +-- metrics/engine/engine.go | 4 ++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index b1f5ab24073e..d1338ba1d942 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 36955ee5af7b..8f42ff1d8a87 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 f734c47e1246..ffb5960fe03d 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 336d8a507850..8733b1f38bed 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{})