Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve test coverage #153

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions errors/rockset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors_test

import (
"errors"
"github.com/rockset/rockset-go-client/openapi"
"net/http"
"testing"

Expand All @@ -27,10 +28,39 @@ func TestError_IsNotFoundError(t *testing.T) {
}

func TestError_NilHTTPResponse(t *testing.T) {
err := rockerr.NewWithStatusCode(errors.New("test error"), nil)
e := errors.New("test error")
err := rockerr.NewWithStatusCode(e, nil)

var re rockerr.Error
require.True(t, errors.As(err, &re))
assert.False(t, re.IsNotFoundError())
assert.Equal(t, 0, re.StatusCode)
assert.False(t, re.RateLimited())
assert.False(t, re.Retryable())
assert.Equal(t, e, re.Unwrap())
assert.Equal(t, "test error", err.Error())
}

func TestError_HTTPResponse(t *testing.T) {
e := errors.New("test error")
err := rockerr.NewWithStatusCode(e, &http.Response{StatusCode: http.StatusTooManyRequests})

var re rockerr.Error
require.True(t, errors.As(err, &re))

re.ErrorModel = &openapi.ErrorModel{
Message: openapi.PtrString("api error"),
}

assert.False(t, re.IsNotFoundError())
assert.True(t, re.RateLimited())
assert.True(t, re.Retryable())
assert.Equal(t, e, re.Unwrap())
assert.Equal(t, "api error", re.Error())
assert.Equal(t, "test error", err.Error())
}

func TestError_Nil(t *testing.T) {
err := rockerr.NewWithStatusCode(nil, nil)

assert.Nil(t, err)
}
90 changes: 51 additions & 39 deletions option/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,36 @@ const (
ListRolesGlobal
GrantRevokeRoleGlobal
CreateVirtualInstanceGlobal
CreateQueryLogsCollectionGlobal
)

var globalActions = map[string]GlobalAction{
"ALL_GLOBAL_ACTIONS": AllGlobalActions,
"GET_ORG_GLOBAL": GetOrgGlobal,
"GET_CURRENT_USER_GLOBAL": GetCurrentUserGlobal,
"INVITE_USER_GLOBAL": InviteUserGlobal,
"DELETE_USER_GLOBAL": DeleteUserGlobal,
"LIST_USERS_GLOBAL": ListUsersGlobal,
"GET_BILLING_GLOBAL": GetBillingGlobal,
"UPDATE_BILLING_GLOBAL": UpdateBillingGlobal,
"UPDATE_SETTINGS_GLOBAL": UpdateSettingsGlobal,
"GET_METRICS_GLOBAL": GetMetricsGlobal,
"UPDATE_VI_GLOBAL": UpdateViGlobal,
"LIST_VI_GLOBAL": ListViGlobal,
"CREATE_WS_GLOBAL": CreateWsGlobal,
"LIST_WS_GLOBAL": ListWsGlobal,
"CREATE_INTEGRATION_GLOBAL": CreateIntegrationGlobal,
"DELETE_INTEGRATION_GLOBAL": DeleteIntegrationGlobal,
"LIST_INTEGRATIONS_GLOBAL": ListIntegrationsGlobal,
"UPDATE_RESOURCE_OWNER_GLOBAL": UpdateResourceOwnerGlobal,
"CREATE_API_KEY_GLOBAL": CreateAPIKeyGlobal,
"CREATE_ROLE_GLOBAL": CreateRoleGlobal,
"UPDATE_ROLE_GLOBAL": UpdateRoleGlobal,
"DELETE_ROLE_GLOBAL": DeleteRoleGlobal,
"LIST_ROLES_GLOBAL": ListRolesGlobal,
"GRANT_REVOKE_ROLE_GLOBAL": GrantRevokeRoleGlobal,
"CREATE_VI_GLOBAL": CreateVirtualInstanceGlobal,
"ALL_GLOBAL_ACTIONS": AllGlobalActions,
"GET_ORG_GLOBAL": GetOrgGlobal,
"GET_CURRENT_USER_GLOBAL": GetCurrentUserGlobal,
"INVITE_USER_GLOBAL": InviteUserGlobal,
"DELETE_USER_GLOBAL": DeleteUserGlobal,
"LIST_USERS_GLOBAL": ListUsersGlobal,
"GET_BILLING_GLOBAL": GetBillingGlobal,
"UPDATE_BILLING_GLOBAL": UpdateBillingGlobal,
"UPDATE_SETTINGS_GLOBAL": UpdateSettingsGlobal,
"GET_METRICS_GLOBAL": GetMetricsGlobal,
"UPDATE_VI_GLOBAL": UpdateViGlobal,
"LIST_VI_GLOBAL": ListViGlobal,
"CREATE_WS_GLOBAL": CreateWsGlobal,
"LIST_WS_GLOBAL": ListWsGlobal,
"CREATE_INTEGRATION_GLOBAL": CreateIntegrationGlobal,
"DELETE_INTEGRATION_GLOBAL": DeleteIntegrationGlobal,
"LIST_INTEGRATIONS_GLOBAL": ListIntegrationsGlobal,
"UPDATE_RESOURCE_OWNER_GLOBAL": UpdateResourceOwnerGlobal,
"CREATE_API_KEY_GLOBAL": CreateAPIKeyGlobal,
"CREATE_ROLE_GLOBAL": CreateRoleGlobal,
"UPDATE_ROLE_GLOBAL": UpdateRoleGlobal,
"DELETE_ROLE_GLOBAL": DeleteRoleGlobal,
"LIST_ROLES_GLOBAL": ListRolesGlobal,
"GRANT_REVOKE_ROLE_GLOBAL": GrantRevokeRoleGlobal,
"CREATE_VI_GLOBAL": CreateVirtualInstanceGlobal,
"CREATE_QUERY_LOGS_COLLECTION_GLOBAL": CreateQueryLogsCollectionGlobal,
}

// IntegrationAction is the type for actions that operate on integrations.
Expand Down Expand Up @@ -160,23 +162,33 @@ const (
ExecuteQueryLambdaWs
CreateViewWs
DeleteViewWs
CreateSnapshotWs
CreateScheduledLambdaWs
DeleteScheduledLambdaWs
CreateSimilarityIndexWs
DeleteSimilarityIndexWs
)

var wsActions = map[string]WorkspaceAction{
"ALL_WORKSPACE_ACTIONS": AllWorkspaceActions,
"DELETE_WS": DeleteWs,
"QUERY_DATA_WS": QueryDataWs,
"WRITE_DATA_WS": WriteDataWs,
"CREATE_COLLECTION_WS": CreateCollectionWs,
"DELETE_COLLECTION_WS": DeleteCollectionWs,
"CREATE_ALIAS_WS": CreateAliasWs,
"DELETE_ALIAS_WS": DeleteAliasWs,
"LIST_RESOURCES_WS": ListResourcesWs,
"CREATE_QUERY_LAMBDA_WS": CreateQueryLambdaWs,
"DELETE_QUERY_LAMBDA_WS": DeleteQueryLambdaWs,
"EXECUTE_QUERY_LAMBDA_WS": ExecuteQueryLambdaWs,
"CREATE_VIEW_WS": CreateViewWs,
"DELETE_VIEW_WS": DeleteViewWs,
"ALL_WORKSPACE_ACTIONS": AllWorkspaceActions,
"DELETE_WS": DeleteWs,
"QUERY_DATA_WS": QueryDataWs,
"WRITE_DATA_WS": WriteDataWs,
"CREATE_COLLECTION_WS": CreateCollectionWs,
"DELETE_COLLECTION_WS": DeleteCollectionWs,
"CREATE_ALIAS_WS": CreateAliasWs,
"DELETE_ALIAS_WS": DeleteAliasWs,
"LIST_RESOURCES_WS": ListResourcesWs,
"CREATE_QUERY_LAMBDA_WS": CreateQueryLambdaWs,
"DELETE_QUERY_LAMBDA_WS": DeleteQueryLambdaWs,
"EXECUTE_QUERY_LAMBDA_WS": ExecuteQueryLambdaWs,
"CREATE_VIEW_WS": CreateViewWs,
"DELETE_VIEW_WS": DeleteViewWs,
"CREATE_SNAPSHOT_WS": CreateSnapshotWs,
"CREATE_SCHEDULED_LAMBDA_WS": CreateScheduledLambdaWs,
"DELETE_SCHEDULED_LAMBDA_WS": DeleteScheduledLambdaWs,
"CREATE_SIMILARITY_INDEX_WS": CreateSimilarityIndexWs,
"DELETE_SIMILARITY_INDEX_WS": DeleteSimilarityIndexWs,
}

// VirtualInstanceAction is the type for actions that operate on virtual instances.
Expand Down
6 changes: 4 additions & 2 deletions retry/exponential.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func NewExponential() Exponential {
return Exponential{}
}

const DefaultJitterFraction = .05

// Retry retries retryFn until it returns an error. Uses exponential backoff.
// If the retryFn returns an error, it is wrapped in an Error, which implements RetryableError
// so the RetryableErrorCheck can determine if it should retry the operation.
Expand All @@ -41,7 +43,7 @@ func (r Exponential) Retry(ctx context.Context, retryFn Func) error {
if r.WaitInterval != 0 {
waitInterval = r.WaitInterval
}
jitterFraction := .05
jitterFraction := DefaultJitterFraction
if r.JitterFraction != 0 {
jitterFraction = r.JitterFraction
}
Expand Down Expand Up @@ -102,7 +104,7 @@ func (r Exponential) RetryWithCheck(ctx context.Context, checkFn CheckFn) error
if r.WaitInterval != 0 {
waitInterval = r.WaitInterval
}
jitterFraction := .05
jitterFraction := DefaultJitterFraction
if r.JitterFraction != 0 {
jitterFraction = r.JitterFraction
}
Expand Down
56 changes: 52 additions & 4 deletions retry/exponential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ func (s *ExponentialRetrySuite) TestDefaultRetry() {
ctx := context.TODO()
var count int

err := retry.Exponential{
MaxBackoff: time.Second,
WaitInterval: time.Millisecond,
}.Retry(ctx, func() error {
exp := retry.NewExponential()
exp.JitterFraction = 0.04
exp.MaxBackoff = time.Second
exp.WaitInterval = time.Millisecond

err := exp.Retry(ctx, func() error {
count++
if count > 2 {
return nil
Expand Down Expand Up @@ -112,6 +114,24 @@ func (s *ExponentialRetrySuite) TestExponentialRetry_RetryFn() {
s.Assert().NoError(err)
}

func (s *ExponentialRetrySuite) TestExponentialRetry_RetryFnCancelled() {
ctx, cancel := context.WithTimeout(context.TODO(), time.Millisecond)

err := retry.Exponential{
MaxBackoff: time.Second,
WaitInterval: time.Millisecond,
RetryableErrorCheck: func(_ error) bool {
// always retry, forever
return true
},
}.Retry(ctx, func() error {
cancel()
return errors.New("error")
})

s.Assert().ErrorIs(err, context.Canceled)
}

func (s *ExponentialRetrySuite) TestExponentialRetry_RetryWithCheck() {
ctx := context.TODO()

Expand All @@ -130,3 +150,31 @@ func (s *ExponentialRetrySuite) TestExponentialRetry_RetryWithCheck() {

s.Assert().NoError(err)
}

func (s *ExponentialRetrySuite) TestExponentialRetry_RetryWithCheckError() {
ctx := context.TODO()

err := retry.Exponential{
MaxBackoff: time.Second,
WaitInterval: time.Millisecond,
}.RetryWithCheck(ctx, func() (bool, error) {
return false, errors.New("error")
})

s.Assert().Error(err)
}

func (s *ExponentialRetrySuite) TestExponentialRetry_RetryWithCheckCancelled() {
ctx, cancel := context.WithTimeout(context.TODO(), time.Millisecond)

err := retry.Exponential{
MaxBackoff: time.Second,
WaitInterval: time.Millisecond,
}.RetryWithCheck(ctx, func() (bool, error) {
cancel()
time.Sleep(time.Millisecond)
return true, nil
})

s.Assert().ErrorIs(err, context.Canceled)
}