Skip to content

Commit

Permalink
reimplement repeat politic
Browse files Browse the repository at this point in the history
  • Loading branch information
siller174 committed Aug 3, 2024
1 parent 00256a2 commit 9766bde
Show file tree
Hide file tree
Showing 19 changed files with 323 additions and 203 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (i *ExampleSuite) BeforeAll(t provider.T) {
// Preparing host
host, err := url.Parse("https://jsonplaceholder.typicode.com/")
if err != nil {
t.Fatalf("could not parse url, error %v", err)
t.Fatalf("could not parse url, error %w", err)
}

i.host = host
Expand Down
6 changes: 3 additions & 3 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (it *Test) assertHeaders(t internalT, headers http.Header) []error {
return nil
}

return executeWithStep(it, t, "Assert headers", func(t T) []error {
return it.executeWithStep(t, "Assert headers", func(t T) []error {
errs := make([]error, 0)
// Execute assert only response
for _, f := range asserts {
Expand Down Expand Up @@ -85,7 +85,7 @@ func (it *Test) assertResponse(t internalT, resp *http.Response) []error {
return nil
}

return executeWithStep(it, t, "Assert response", func(t T) []error {
return it.executeWithStep(t, "Assert response", func(t T) []error {
errs := make([]error, 0)
// Execute assert only response
for _, f := range asserts {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (it *Test) assertBody(t internalT, body []byte) []error {
return nil
}

return executeWithStep(it, t, "Assert body", func(t T) []error {
return it.executeWithStep(t, "Assert body", func(t T) []error {
errs := make([]error, 0)
// Execute assert only response
for _, f := range asserts {
Expand Down
2 changes: 1 addition & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func createDefaultTest(m *HTTPTestMaker) *Test {
Middleware: createMiddlewareFromTemplate(m.middleware),
AllureStep: new(AllureStep),
Request: &Request{
Repeat: new(RequestRepeatPolitic),
Retry: new(RequestRetryPolitic),
},
Expect: &Expect{JSONSchema: new(ExpectJSONSchema)},
}
Expand Down
70 changes: 64 additions & 6 deletions builder_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import (
// RequestRepeat is a function for set options in request
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// Default delay is 1 second.
// Deprecated: use RequestRetry instead
func (qt *cute) RequestRepeat(count int) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Repeat.Count = count
qt.tests[qt.countTests].Request.Retry.Count = count

return qt
}

// RequestRepeatDelay set delay for request repeat.
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// Default delay is 1 second.
// Deprecated: use RequestRetryDelay instead
func (qt *cute) RequestRepeatDelay(delay time.Duration) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Repeat.Delay = delay
qt.tests[qt.countTests].Request.Retry.Delay = delay

return qt
}
Expand All @@ -27,28 +29,84 @@ func (qt *cute) RequestRepeatDelay(delay time.Duration) RequestHTTPBuilder {
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// if Optional is true and request is failed, than test step allure will be skipped, and t.Fail() will not execute.
// If Broken is true and request is failed, than test step allure will be broken, and t.Fail() will not execute.
// Deprecated: use RequestRetryPolitic instead
func (qt *cute) RequestRepeatPolitic(politic *RequestRepeatPolitic) RequestHTTPBuilder {
if politic == nil {
panic("politic is nil in RequestRepeatPolitic")
panic("politic is nil in RequestRetryPolitic")
}

qt.tests[qt.countTests].Request.Repeat = politic
qt.tests[qt.countTests].Request.Retry = &RequestRetryPolitic{
Count: politic.Count,
Delay: politic.Delay,
Optional: politic.Optional,
Broken: politic.Broken,
}

return qt
}

// RequestRepeatOptional set option politic for request repeat.
// if Optional is true and request is failed, than test step allure will be skipped, and t.Fail() will not execute.
// Deprecated: use RequestRetryOptional instead
func (qt *cute) RequestRepeatOptional(option bool) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Repeat.Optional = option
qt.tests[qt.countTests].Request.Retry.Optional = option

return qt
}

// RequestRepeatBroken set broken politic for request repeat.
// If Broken is true and request is failed, than test step allure will be broken, and t.Fail() will not execute.
// Deprecated: use RequestRetryBroken instead
func (qt *cute) RequestRepeatBroken(broken bool) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Repeat.Broken = broken
qt.tests[qt.countTests].Request.Retry.Broken = broken

return qt
}

// RequestRetry is a function for set options in request
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// Default delay is 1 second.
func (qt *cute) RequestRetry(count int) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Retry.Count = count

return qt
}

// RequestRetryDelay set delay for request repeat.
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// Default delay is 1 second.
func (qt *cute) RequestRetryDelay(delay time.Duration) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Retry.Delay = delay

return qt
}

// RequestRetryPolitic set politic for request repeat.
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// if Optional is true and request is failed, than test step allure will be skipped, and t.Fail() will not execute.
// If Broken is true and request is failed, than test step allure will be broken, and t.Fail() will not execute.
func (qt *cute) RequestRetryPolitic(politic *RequestRetryPolitic) RequestHTTPBuilder {
if politic == nil {
panic("politic is nil in RequestRetryPolitic")
}

qt.tests[qt.countTests].Request.Retry = politic

return qt
}

// RequestRetryOptional set option politic for request repeat.
// if Optional is true and request is failed, than test step allure will be skipped, and t.Fail() will not execute.
func (qt *cute) RequestRetryOptional(option bool) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Retry.Optional = option

return qt
}

// RequestRetryBroken set broken politic for request repeat.
// If Broken is true and request is failed, than test step allure will be broken, and t.Fail() will not execute.
func (qt *cute) RequestRetryBroken(broken bool) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Retry.Broken = broken

return qt
}
Expand Down
29 changes: 29 additions & 0 deletions builder_retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cute

import "time"

// Retry is a function for configure test repeat
// if response.Code != Expect.Code or any of asserts are failed/broken than test will repeat counts with delay.
// Default delay is 1 second.
func (qt *cute) Retry(count int) MiddlewareRequest {
if count < 1 {
panic("count must be greater than 0")
}

qt.tests[qt.countTests].Retry.MaxAttempts = count

return qt
}

// RetryDelay set delay for test repeat.
// if response.Code != Expect.Code or any of asserts are failed/broken than test will repeat counts with delay.
// Default delay is 1 second.
func (qt *cute) RetryDelay(delay time.Duration) MiddlewareRequest {
if delay < 0 {
panic("delay must be greater than or equal to 0")
}

qt.tests[qt.countTests].Retry.Delay = delay

return qt
}
10 changes: 5 additions & 5 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ func TestHTTPTestMaker(t *testing.T) {
Link(link).
Description(desc).
CreateStep(stepName).
RequestRepeat(repeatCount).
RequestRepeatDelay(repeatDelay).
RequestRetry(repeatCount).
RequestRetryDelay(repeatDelay).
Request(req).
ExpectExecuteTimeout(executeTime).
ExpectStatus(status).
Expand Down Expand Up @@ -330,8 +330,8 @@ func TestHTTPTestMaker(t *testing.T) {
require.Equal(t, setIssue, resHt.allureLinks.issue)
require.Equal(t, setTestCase, resHt.allureLinks.testCase)
require.Equal(t, link, resHt.allureLinks.link)
require.Equal(t, repeatCount, resTest.Request.Repeat.Count)
require.Equal(t, repeatDelay, resTest.Request.Repeat.Delay)
require.Equal(t, repeatCount, resTest.Request.Retry.Count)
require.Equal(t, repeatDelay, resTest.Request.Retry.Delay)

require.Equal(t, len(assertHeaders), len(resTest.Expect.AssertHeaders))
require.Equal(t, len(assertHeadersT), len(resTest.Expect.AssertHeadersT))
Expand Down Expand Up @@ -360,7 +360,7 @@ func TestCreateDefaultTest(t *testing.T) {
BeforeT: make([]BeforeExecuteT, 0),
},
Request: &Request{
Repeat: new(RequestRepeatPolitic),
Retry: new(RequestRetryPolitic),
},
Expect: &Expect{
JSONSchema: new(ExpectJSONSchema),
Expand Down
50 changes: 26 additions & 24 deletions cute.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,6 @@ func createAllureT(t *testing.T) *common.Common {
return newT
}

// executeTestsInsideStep is method for run group of tests inside provider.StepCtx
func (qt *cute) executeTestsInsideStep(ctx context.Context, stepCtx provider.StepCtx) []ResultsHTTPBuilder {
var (
res = make([]ResultsHTTPBuilder, 0)
)

// Cycle for change number of Test
for i := 0; i <= qt.countTests; i++ {
currentTest := qt.tests[i]

result := currentTest.executeInsideStep(ctx, stepCtx)

// Remove from base struct all asserts
currentTest.clearFields()

res = append(res, result)
}

return res
}

// executeTests is method for run tests
// It's could be table tests or usual tests
func (qt *cute) executeTests(ctx context.Context, allureProvider allureProvider) []ResultsHTTPBuilder {
Expand All @@ -153,26 +132,49 @@ func (qt *cute) executeTests(ctx context.Context, allureProvider allureProvider)
// Set current test name
inT.Title(tableTestName)

res = append(res, qt.executeSingleTest(ctx, inT, currentTest))
res = append(res, qt.executeInsideAllure(ctx, inT, currentTest))
})
} else {
currentTest.Name = allureProvider.Name()

// set labels
qt.setAllureInformation(allureProvider)

res = append(res, qt.executeSingleTest(ctx, allureProvider, currentTest))
res = append(res, qt.executeInsideAllure(ctx, allureProvider, currentTest))
}
}

return res
}

func (qt *cute) executeSingleTest(ctx context.Context, allureProvider allureProvider, currentTest *Test) ResultsHTTPBuilder {
// executeInsideAllure is method for run test inside allure
// It's could be table tests or usual tests
func (qt *cute) executeInsideAllure(ctx context.Context, allureProvider allureProvider, currentTest *Test) ResultsHTTPBuilder {
resT := currentTest.executeInsideAllure(ctx, allureProvider)

// Remove from base struct all asserts
currentTest.clearFields()

return resT
}

// executeTestsInsideStep is method for run group of tests inside provider.StepCtx
func (qt *cute) executeTestsInsideStep(ctx context.Context, stepCtx provider.StepCtx) []ResultsHTTPBuilder {
var (
res = make([]ResultsHTTPBuilder, 0)
)

// Cycle for change number of Test
for i := 0; i <= qt.countTests; i++ {
currentTest := qt.tests[i]

result := currentTest.executeInsideStep(ctx, stepCtx)

// Remove from base struct all asserts
currentTest.clearFields()

res = append(res, result)
}

return res
}
16 changes: 9 additions & 7 deletions examples/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Test_Single_1(t *testing.T) {
Description("some_description").
Parallel().
Create().
RequestRepeat(3).
RequestRetry(3).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
cute.WithMarshalBody(struct {
Expand Down Expand Up @@ -96,14 +96,16 @@ func Test_Single_Broken(t *testing.T) {
},
).
ExecuteTest(context.Background(), t)

t.Skip()
}

func Test_Single_RepeatPolitic_Optional_Success_Test(t *testing.T) {
cute.NewTestBuilder().
Title("Test_Single_RepeatPolitic_Optional_Success_Test").
Create().
RequestRepeat(2).
RequestRepeatOptional(true).
RequestRetry(2).
RequestRetryOptional(true).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
Expand All @@ -120,8 +122,8 @@ func Test_Single_RepeatPolitic_Broken_Failed_Test(t *testing.T) {
cute.NewTestBuilder().
Title("Test_Single_RepeatPolitic_Broken_Failed_Test").
Create().
RequestRepeat(2).
RequestRepeatOptional(true).
RequestRetry(2).
RequestRetryOptional(true).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
Expand Down Expand Up @@ -173,8 +175,8 @@ func Test_Single_2_AllureRunner(t *testing.T) {
Tag("single_test").
Description("some_description").
Create().
RequestRepeatDelay(3*time.Second). // delay before new try
RequestRepeat(3). // count attempts
RequestRetryDelay(3*time.Second). // delay before new try
RequestRetry(3). // count attempts
RequestBuilder(
cute.WithURL(u),
cute.WithMethod(http.MethodGet),
Expand Down
8 changes: 4 additions & 4 deletions examples/table_test/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Test_Table_Array(t *testing.T) {
},
},
Expect: &cute.Expect{
Code: 201,
Code: 200,
},
},
{
Expand Down Expand Up @@ -111,7 +111,7 @@ func Test_One_Execute(t *testing.T) {
test.Execute(context.Background(), t)
}

func Test_Array(t *testing.T) {
func Test_Array(t *testing.T) { // не полный отчет в аллюре
tests := []*cute.Test{
{
Name: "test_1",
Expand Down Expand Up @@ -292,7 +292,7 @@ func Test_Array_Retry(t *testing.T) {
{
Name: "test_1",
Parallel: true,
Retry: cute.Retry{
Retry: &cute.Retry{
MaxAttempts: 10,
Delay: 1,
},
Expand All @@ -310,7 +310,7 @@ func Test_Array_Retry(t *testing.T) {
{
Name: "test_2",
Parallel: true,
Retry: cute.Retry{
Retry: &cute.Retry{
MaxAttempts: 10,
Delay: 1,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/two_step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_TwoSteps_1(t *testing.T) {
Title("Test with two requests.").
Tags("two_steps").
Parallel().
CreateStep("Creat entry /posts/1").
CreateStep("Create entry /posts/1").

// CreateWithStep first step

Expand Down
Loading

0 comments on commit 9766bde

Please sign in to comment.