mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-25 01:59:52 +08:00
feat: support timeout option for driver session
This commit is contained in:
@@ -1 +1 @@
|
|||||||
v5.0.0-250707
|
v5.0.0-250708
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v5/internal/json"
|
"github.com/httprunner/httprunner/v5/internal/json"
|
||||||
|
"github.com/httprunner/httprunner/v5/uixt/option"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Attachments map[string]interface{}
|
type Attachments map[string]interface{}
|
||||||
@@ -151,32 +152,32 @@ func (s *DriverSession) buildURL(urlStr string) (string, error) {
|
|||||||
return baseURL.ResolveReference(relativeURL).String(), nil
|
return baseURL.ResolveReference(relativeURL).String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DriverSession) GET(urlStr string) (rawResp DriverRawResponse, err error) {
|
func (s *DriverSession) GET(urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) {
|
||||||
return s.RequestWithRetry(http.MethodGet, urlStr, nil)
|
return s.RequestWithRetry(http.MethodGet, urlStr, nil, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DriverSession) POST(data interface{}, urlStr string) (rawResp DriverRawResponse, err error) {
|
func (s *DriverSession) POST(data interface{}, urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) {
|
||||||
var bsJSON []byte = nil
|
var bsJSON []byte = nil
|
||||||
if data != nil {
|
if data != nil {
|
||||||
if bsJSON, err = json.Marshal(data); err != nil {
|
if bsJSON, err = json.Marshal(data); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON)
|
return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DriverSession) DELETE(urlStr string) (rawResp DriverRawResponse, err error) {
|
func (s *DriverSession) DELETE(urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) {
|
||||||
return s.RequestWithRetry(http.MethodDelete, urlStr, nil)
|
return s.RequestWithRetry(http.MethodDelete, urlStr, nil, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody []byte) (
|
func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody []byte, opts ...option.ActionOption) (
|
||||||
rawResp DriverRawResponse, err error,
|
rawResp DriverRawResponse, err error,
|
||||||
) {
|
) {
|
||||||
var lastError error
|
var lastError error
|
||||||
|
|
||||||
for attempt := 1; attempt <= s.maxRetry; attempt++ {
|
for attempt := 1; attempt <= s.maxRetry; attempt++ {
|
||||||
// Execute the request
|
// Execute the request
|
||||||
rawResp, err = s.Request(method, urlStr, rawBody)
|
rawResp, err = s.Request(method, urlStr, rawBody, opts...)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if attempt > 1 {
|
if attempt > 1 {
|
||||||
log.Info().Msgf("request succeeded after %d attempts", attempt)
|
log.Info().Msgf("request succeeded after %d attempts", attempt)
|
||||||
@@ -210,9 +211,15 @@ func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody [
|
|||||||
return nil, lastError
|
return nil, lastError
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DriverSession) Request(method string, urlStr string, rawBody []byte) (
|
func (s *DriverSession) Request(method string, urlStr string, rawBody []byte, opts ...option.ActionOption) (
|
||||||
rawResp DriverRawResponse, err error,
|
rawResp DriverRawResponse, err error,
|
||||||
) {
|
) {
|
||||||
|
timeout := s.timeout
|
||||||
|
options := option.NewActionOptions(opts...)
|
||||||
|
if options.Timeout > 0 {
|
||||||
|
timeout = time.Duration(options.Timeout) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
// build final URL
|
// build final URL
|
||||||
rawURL, err := s.buildURL(urlStr)
|
rawURL, err := s.buildURL(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -252,7 +259,7 @@ func (s *DriverSession) Request(method string, urlStr string, rawBody []byte) (
|
|||||||
logger.Msg("request uixt driver")
|
logger.Msg("request uixt driver")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(s.ctx, s.timeout)
|
ctx, cancel := context.WithTimeout(s.ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, rawURL, bytes.NewBuffer(rawBody))
|
req, err := http.NewRequestWithContext(ctx, method, rawURL, bytes.NewBuffer(rawBody))
|
||||||
|
|||||||
Reference in New Issue
Block a user