From 2040b96c65aea8cbc53df132ad6f82caaf1abdc5 Mon Sep 17 00:00:00 2001 From: Norbert Schneider Date: Fri, 27 Dec 2024 17:00:32 +0100 Subject: [PATCH] feat: add hint to fixed amount check and lower default duration --- CHANGELOG.md | 4 +++ exthttpcheck/fixAmount.go | 19 ++++++++++-- exthttpcheck/fixAmount_test.go | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 023885c..aa9f9af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v1.0.25 (next) + +- Add hint describing the behavior of the fixed amount check and lower the default duration to 2 seconds + ## v1.0.24 - Update dependencies diff --git a/exthttpcheck/fixAmount.go b/exthttpcheck/fixAmount.go index 8a95e76..b6fa8df 100644 --- a/exthttpcheck/fixAmount.go +++ b/exthttpcheck/fixAmount.go @@ -64,6 +64,13 @@ func (l *httpCheckActionFixedAmount) Describe() action_kit_api.ActionDescription // Instantaneous: The action is done immediately. Use this for actions that happen immediately, e.g. a reboot. TimeControl: action_kit_api.TimeControlInternal, + Hint: &action_kit_api.ActionHint{ + Content: "Please note that the given number of requests is uniformly distributed over the given duration. For example, 10 requests in 10 seconds " + + "will result in 1 request per second, whereas the first request is executed immediately. " + + "The requests are handled by the given number of parallel processes, adhering to the overall request count.", + Type: action_kit_api.HintInfo, + }, + // The parameters for the action Parameters: []action_kit_api.ActionParameter{ //------------------------ @@ -76,7 +83,7 @@ func (l *httpCheckActionFixedAmount) Describe() action_kit_api.ActionDescription headers, separator(5), //------------------------ - // Repitions + // Repetitions //------------------------ repetitionControl, { @@ -88,7 +95,15 @@ func (l *httpCheckActionFixedAmount) Describe() action_kit_api.ActionDescription DefaultValue: extutil.Ptr("1"), Order: extutil.Ptr(7), }, - duration, + action_kit_api.ActionParameter{ + Name: "duration", + Label: "Duration", + Description: extutil.Ptr("In which timeframe should the specified requests be executed?"), + Type: action_kit_api.ActionParameterTypeDuration, + DefaultValue: extutil.Ptr("2s"), + Required: extutil.Ptr(true), + Order: extutil.Ptr(8), + }, separator(9), //------------------------ // Result Verification diff --git a/exthttpcheck/fixAmount_test.go b/exthttpcheck/fixAmount_test.go index be2773d..3a6da2d 100644 --- a/exthttpcheck/fixAmount_test.go +++ b/exthttpcheck/fixAmount_test.go @@ -269,3 +269,56 @@ func TestNewHTTPCheckActionFixedAmount_All_Failure(t *testing.T) { assert.Equal(t, stopResult.Error.Title, "Success Rate (0.00%) was below 100%") assert.Equal(t, executionRunData.requestSuccessCounter.Load(), uint64(0)) } + +func TestNewHTTPCheckActionFixedAmount_start_directly(t *testing.T) { + // write receive timestamps to the channel to check the delay between requests + var receivedRequests = make(chan time.Time, 10) + + // generate a test server so we can capture and inspect the request + testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + receivedRequests <- time.Now() + res.WriteHeader(200) + })) + defer func() { testServer.Close() }() + + //prepare the action + action := httpCheckActionFixedAmount{} + state := action.NewEmptyState() + prepareActionRequestBody := extutil.JsonMangle(action_kit_api.PrepareActionRequestBody{ + Config: map[string]interface{}{ + "action": "prepare", + "duration": 2000, + "statusCode": "200-209", + "responsesContains": "test", + "successRate": 100, + "maxConcurrent": 1, + "numberOfRequests": 2, + "readTimeout": 5000, + "body": "test", + "url": testServer.URL, + "method": "GET", + "connectTimeout": 5000, + "followRedirects": true, + "headers": []interface{}{map[string]interface{}{"key": "test", "value": "test"}}, + }, + ExecutionId: uuid.New(), + }) + + // prepare + _, err := action.Prepare(context.Background(), &state, prepareActionRequestBody) + assert.NoError(t, err) + + now := time.Now() + println(now.String()) + _, _ = action.Start(context.Background(), &state) + + // first request is executed immediately, check with quite big tolerance to avoid flakiness + firstRequest := <-receivedRequests + println(firstRequest.String()) + assert.WithinDuration(t, now, firstRequest, 400*time.Millisecond) + + // second request is executed after 1 second + secondRequest := <-receivedRequests + println(secondRequest.String()) + assert.WithinDuration(t, now.Add(1*time.Second), secondRequest, 400*time.Millisecond) +}