Skip to content

Commit

Permalink
change logging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
siller174 committed Oct 21, 2024
1 parent e3ea687 commit 70697da
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func Test_Single_RepeatPolitic_Broken_Failed_Test(t *testing.T) {
Title("Test_Single_RepeatPolitic_Broken_Failed_Test").
Create().
RequestRetry(2).
RequestRetryOptional(true).
RequestRetryOptional(false).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
Expand Down
88 changes: 88 additions & 0 deletions examples/table_test/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,94 @@ func Test_One_Execute(t *testing.T) {
test.Execute(context.Background(), t)
}

func Test_Array_Retry_OptionalFirstTries(t *testing.T) {
tests := []*cute.Test{
{
Name: "test_1",

Retry: &cute.Retry{
MaxAttempts: 10,
Delay: 1 * time.Second,
},
Middleware: nil,
Request: &cute.Request{
Builders: []cute.RequestBuilder{
cute.WithURI("https://httpstat.us/Random/201,202"),
cute.WithMethod(http.MethodGet),
},
},
Expect: &cute.Expect{
Code: 201,
},
},
{
Name: "test_2",
Retry: &cute.Retry{
MaxAttempts: 10,
Delay: 1 * time.Second,
},
Middleware: nil,
Request: &cute.Request{
Builders: []cute.RequestBuilder{
cute.WithURI("https://httpstat.us/Random/403,404"),
cute.WithMethod(http.MethodGet),
},
},
Expect: &cute.Expect{
Code: 404,
},
},
}

for _, test := range tests {
test.Execute(context.Background(), t)
}
}

func Test_Array_Retry_OptionalFirstTries_UltimatelyFailing(t *testing.T) {
tests := []*cute.Test{
{
Name: "test_1",

Retry: &cute.Retry{
MaxAttempts: 4,
Delay: 1 * time.Second,
},
Middleware: nil,
Request: &cute.Request{
Builders: []cute.RequestBuilder{
cute.WithURI("https://httpstat.us/Random/202,200"),
cute.WithMethod(http.MethodGet),
},
},
Expect: &cute.Expect{
Code: 201,
},
},
{
Name: "test_2",
Retry: &cute.Retry{
MaxAttempts: 3,
Delay: 1 * time.Second,
},
Middleware: nil,
Request: &cute.Request{
Builders: []cute.RequestBuilder{
cute.WithURI("https://httpstat.us/Random/403,401"),
cute.WithMethod(http.MethodGet),
},
},
Expect: &cute.Expect{
Code: 404,
},
},
}

for _, test := range tests {
test.Execute(context.Background(), t)
}
}

func Test_Array_TimeoutRetry(t *testing.T) {
var executeTimeout = 3000

Expand Down
4 changes: 2 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (it *Test) errorf(t tlogger, level, format string, args ...interface{}) {
}
// If we are in a retry context, add some indication in the logs about the current attempt
if it.Retry.MaxAttempts != 1 {
t.Errorf("[%s][%s](Attempt #%d) %v\n", name, level, it.Retry.currentCount, fmt.Sprintf(format, args...))
t.Logf("[%s][%s](Attempt #%d) %v\n", name, level, it.Retry.currentCount, fmt.Sprintf(format, args...))
} else {
t.Errorf("[%s][%s] %v\n", name, level, fmt.Sprintf(format, args...))
t.Logf("[%s][%s] %v\n", name, level, fmt.Sprintf(format, args...))
}
}
2 changes: 1 addition & 1 deletion roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (it *Test) doRequest(t T, baseReq *http.Request) (*http.Response, error) {
}

if validErr := it.validateResponseCode(resp); validErr != nil {
return nil, validErr
return resp, validErr
}

return resp, nil
Expand Down

0 comments on commit 70697da

Please sign in to comment.