[#121] pool: Add wait params validation for containerPut method

* Add WaitParams.CheckValidity() check because SetWaitParams is deprecated,
  but parameters were checked within this setter with checkForPositive()

Signed-off-by: Airat Arifullin a.arifullin@yadro.com
This commit is contained in:
Airat Arifullin 2023-08-03 12:14:00 +03:00 committed by Evgenii Stratonikov
parent be28b89312
commit 936e6d230b
3 changed files with 33 additions and 25 deletions

View file

@ -30,8 +30,8 @@ type PrmContainerDelete struct {
// Required parameter. // Required parameter.
// //
// Deprecated: Use PrmContainerDelete.Container instead. // Deprecated: Use PrmContainerDelete.Container instead.
func (x *PrmContainerDelete) SetContainer(id cid.ID) { func (prm *PrmContainerDelete) SetContainer(id cid.ID) {
x.ContainerID = &id prm.ContainerID = &id
} }
func (prm *PrmContainerDelete) buildRequest(c *Client) (*v2container.DeleteRequest, error) { func (prm *PrmContainerDelete) buildRequest(c *Client) (*v2container.DeleteRequest, error) {
@ -88,8 +88,8 @@ func (prm *PrmContainerDelete) buildRequest(c *Client) (*v2container.DeleteReque
// Must be signed. // Must be signed.
// //
// Deprecated: Use PrmContainerDelete.Session instead. // Deprecated: Use PrmContainerDelete.Session instead.
func (x *PrmContainerDelete) WithinSession(tok session.Container) { func (prm *PrmContainerDelete) WithinSession(tok session.Container) {
x.Session = &tok prm.Session = &tok
} }
// ResContainerDelete groups resulting values of ContainerDelete operation. // ResContainerDelete groups resulting values of ContainerDelete operation.

View file

@ -420,6 +420,9 @@ func (c *clientWrapper) containerPut(ctx context.Context, prm PrmContainerPut) (
if prm.WaitParams == nil { if prm.WaitParams == nil {
prm.WaitParams = defaultWaitParams() prm.WaitParams = defaultWaitParams()
} }
if err = prm.WaitParams.CheckValidity(); err != nil {
return cid.ID{}, fmt.Errorf("invalid wait parameters: %w", err)
}
idCnr := res.ID() idCnr := res.ID()
@ -1205,45 +1208,50 @@ func (x *NodeParam) Weight() float64 {
// WaitParams contains parameters used in polling is a something applied on FrostFS network. // WaitParams contains parameters used in polling is a something applied on FrostFS network.
type WaitParams struct { type WaitParams struct {
timeout time.Duration Timeout time.Duration
pollInterval time.Duration PollInterval time.Duration
} }
// SetTimeout specifies the time to wait for the operation to complete. // SetTimeout specifies the time to wait for the operation to complete.
//
// Deprecated: Use WaitParams.Timeout instead.
func (x *WaitParams) SetTimeout(timeout time.Duration) { func (x *WaitParams) SetTimeout(timeout time.Duration) {
x.timeout = timeout x.Timeout = timeout
} }
// SetPollInterval specifies the interval, once it will check the completion of the operation. // SetPollInterval specifies the interval, once it will check the completion of the operation.
//
// Deprecated: Use WaitParams.PollInterval instead.
func (x *WaitParams) SetPollInterval(tick time.Duration) { func (x *WaitParams) SetPollInterval(tick time.Duration) {
x.pollInterval = tick x.PollInterval = tick
} }
// Deprecated: Use defaultWaitParams() instead.
func (x *WaitParams) setDefaults() { func (x *WaitParams) setDefaults() {
x.timeout = 120 * time.Second x.Timeout = 120 * time.Second
x.pollInterval = 5 * time.Second x.PollInterval = 5 * time.Second
} }
func defaultWaitParams() *WaitParams { func defaultWaitParams() *WaitParams {
return &WaitParams{ return &WaitParams{
timeout: 120 * time.Second, Timeout: 120 * time.Second,
pollInterval: 5 * time.Second, PollInterval: 5 * time.Second,
} }
} }
// checkForPositive panics if any of the wait params isn't positive. // checkForPositive panics if any of the wait params isn't positive.
func (x *WaitParams) checkForPositive() { func (x *WaitParams) checkForPositive() {
if x.timeout <= 0 || x.pollInterval <= 0 { if x.Timeout <= 0 || x.PollInterval <= 0 {
panic("all wait params must be positive") panic("all wait params must be positive")
} }
} }
// CheckForValid checks if all wait params are non-negative. // CheckForValid checks if all wait params are non-negative.
func (waitPrm *WaitParams) CheckValidity() error { func (x *WaitParams) CheckValidity() error {
if waitPrm.timeout <= 0 { if x.Timeout <= 0 {
return errors.New("timeout cannot be negative") return errors.New("timeout cannot be negative")
} }
if waitPrm.pollInterval <= 0 { if x.PollInterval <= 0 {
return errors.New("poll interval cannot be negative") return errors.New("poll interval cannot be negative")
} }
return nil return nil
@ -2533,9 +2541,9 @@ func waitForContainerRemoved(ctx context.Context, cli client, cnrID *cid.ID, wai
// waitFor await that given condition will be met in waitParams time. // waitFor await that given condition will be met in waitParams time.
func waitFor(ctx context.Context, params *WaitParams, condition func(context.Context) bool) error { func waitFor(ctx context.Context, params *WaitParams, condition func(context.Context) bool) error {
wctx, cancel := context.WithTimeout(ctx, params.timeout) wctx, cancel := context.WithTimeout(ctx, params.Timeout)
defer cancel() defer cancel()
ticker := time.NewTimer(params.pollInterval) ticker := time.NewTimer(params.PollInterval)
defer ticker.Stop() defer ticker.Stop()
wdone := wctx.Done() wdone := wctx.Done()
done := ctx.Done() done := ctx.Done()
@ -2549,7 +2557,7 @@ func waitFor(ctx context.Context, params *WaitParams, condition func(context.Con
if condition(ctx) { if condition(ctx) {
return nil return nil
} }
ticker.Reset(params.pollInterval) ticker.Reset(params.PollInterval)
} }
} }
} }

View file

@ -483,8 +483,8 @@ func TestWaitPresence(t *testing.T) {
var idCnr cid.ID var idCnr cid.ID
err := waitForContainerPresence(ctx, mockCli, idCnr, &WaitParams{ err := waitForContainerPresence(ctx, mockCli, idCnr, &WaitParams{
timeout: 120 * time.Second, Timeout: 120 * time.Second,
pollInterval: 5 * time.Second, PollInterval: 5 * time.Second,
}) })
require.Error(t, err) require.Error(t, err)
require.Contains(t, err.Error(), "context canceled") require.Contains(t, err.Error(), "context canceled")
@ -494,8 +494,8 @@ func TestWaitPresence(t *testing.T) {
ctx := context.Background() ctx := context.Background()
var idCnr cid.ID var idCnr cid.ID
err := waitForContainerPresence(ctx, mockCli, idCnr, &WaitParams{ err := waitForContainerPresence(ctx, mockCli, idCnr, &WaitParams{
timeout: 500 * time.Millisecond, Timeout: 500 * time.Millisecond,
pollInterval: 5 * time.Second, PollInterval: 5 * time.Second,
}) })
require.Error(t, err) require.Error(t, err)
require.Contains(t, err.Error(), "context deadline exceeded") require.Contains(t, err.Error(), "context deadline exceeded")
@ -505,8 +505,8 @@ func TestWaitPresence(t *testing.T) {
ctx := context.Background() ctx := context.Background()
var idCnr cid.ID var idCnr cid.ID
err := waitForContainerPresence(ctx, mockCli, idCnr, &WaitParams{ err := waitForContainerPresence(ctx, mockCli, idCnr, &WaitParams{
timeout: 10 * time.Second, Timeout: 10 * time.Second,
pollInterval: 500 * time.Millisecond, PollInterval: 500 * time.Millisecond,
}) })
require.NoError(t, err) require.NoError(t, err)
}) })