Skip to content

Commit

Permalink
Make the REST API tests less flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Mar 29, 2022
1 parent d7bc04b commit 58fca82
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
10 changes: 5 additions & 5 deletions api/v1/setup_teardown_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
42 changes: 26 additions & 16 deletions api/v1/status_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit 58fca82

Please sign in to comment.