From 58fca82c92c6c99e3611b962650a748e6c70981d Mon Sep 17 00:00:00 2001 From: Nedyalko Andreev Date: Tue, 29 Mar 2022 12:09:38 +0300 Subject: [PATCH] Make the REST API tests less flaky --- api/v1/setup_teardown_routes_test.go | 10 +++---- api/v1/status_routes_test.go | 42 +++++++++++++++++----------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/api/v1/setup_teardown_routes_test.go b/api/v1/setup_teardown_routes_test.go index 8e18bd1ce500..a274b4c91bb9 100644 --- a/api/v1/setup_teardown_routes_test.go +++ b/api/v1/setup_teardown_routes_test.go @@ -137,14 +137,14 @@ func TestSetupData(t *testing.T) { runTestCase := func(t *testing.T, tcid int) { testCase := testCases[tcid] - logger := logrus.New() - logger.SetOutput(testutils.NewTestOutput(t)) - registry := metrics.NewRegistry() - builtinMetrics := metrics.RegisterBuiltinMetrics(registry) - t.Run(testCase.name, func(t *testing.T) { t.Parallel() + logger := logrus.New() + logger.SetOutput(testutils.NewTestOutput(t)) + registry := metrics.NewRegistry() + builtinMetrics := metrics.RegisterBuiltinMetrics(registry) + runner, err := js.New( logger, &loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: testCase.script}, diff --git a/api/v1/status_routes_test.go b/api/v1/status_routes_test.go index 77118e58556d..70b530646c82 100644 --- a/api/v1/status_routes_test.go +++ b/api/v1/status_routes_test.go @@ -124,34 +124,44 @@ func TestPatchStatus(t *testing.T) { Payload: []byte(`{"data":{"type":"status","id":"default","attributes":{"status":0,"paused":null,"vus":10,"vus-max":10,"stopped":false,"running":false,"tainted":false}}}`), }, } - logger := logrus.New() - logger.SetOutput(testutils.NewTestOutput(t)) - - scenarios := lib.ScenarioConfigs{} - err := json.Unmarshal([]byte(` - {"external": {"executor": "externally-controlled", - "vus": 0, "maxVUs": 10, "duration": "1s"}}`), &scenarios) - require.NoError(t, err) - options := lib.Options{Scenarios: scenarios} - registry := metrics.NewRegistry() - builtinMetrics := metrics.RegisterBuiltinMetrics(registry) for name, testCase := range testData { t.Run(name, func(t *testing.T) { t.Parallel() + logger := logrus.New() + logger.SetOutput(testutils.NewTestOutput(t)) + scenarios := lib.ScenarioConfigs{} + err := json.Unmarshal([]byte(` + {"external": {"executor": "externally-controlled", + "vus": 0, "maxVUs": 10, "duration": "0"}}`), &scenarios) + require.NoError(t, err) + options := lib.Options{Scenarios: scenarios} + + registry := metrics.NewRegistry() + builtinMetrics := metrics.RegisterBuiltinMetrics(registry) execScheduler, err := local.NewExecutionScheduler(&minirunner.MiniRunner{Options: options}, builtinMetrics, logger) require.NoError(t, err) engine, err := core.NewEngine(execScheduler, options, lib.RuntimeOptions{}, nil, logger, registry) require.NoError(t, err) - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() - run, _, err := engine.Init(ctx, ctx) + + require.NoError(t, engine.OutputManager.StartOutputs()) + defer engine.OutputManager.StopOutputs() + + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + run, wait, err := engine.Init(ctx, ctx) require.NoError(t, err) - go func() { _ = run() }() + defer func() { + cancel() + wait() + }() + + go func() { + assert.NoError(t, run()) + }() // wait for the executor to initialize to avoid a potential data race below - time.Sleep(100 * time.Millisecond) + time.Sleep(200 * time.Millisecond) rw := httptest.NewRecorder() NewHandler().ServeHTTP(rw, newRequestWithEngine(engine, "PATCH", "/v1/status", bytes.NewReader(testCase.Payload)))