2023-04-03 11:04:35 +00:00
|
|
|
package services
|
2023-03-14 14:31:15 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-19 13:32:17 +00:00
|
|
|
"context"
|
2023-03-14 14:31:15 +00:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-05-19 13:32:17 +00:00
|
|
|
grpcService "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services/tree"
|
2023-03-14 14:31:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-04-04 14:47:16 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2023-03-14 14:31:15 +00:00
|
|
|
)
|
|
|
|
|
2023-05-19 14:08:59 +00:00
|
|
|
type treeClientMock struct {
|
|
|
|
address string
|
|
|
|
err bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *treeClientMock) TreeClient(context.Context) (grpcService.TreeServiceClient, error) {
|
|
|
|
if t.err {
|
|
|
|
return nil, errors.New("error")
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *treeClientMock) Address() string {
|
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func TestHandleError(t *testing.T) {
|
|
|
|
defaultError := errors.New("default error")
|
|
|
|
for _, tc := range []struct {
|
|
|
|
err error
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
err: defaultError,
|
|
|
|
expectedError: defaultError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
err: errors.New("something not found"),
|
|
|
|
expectedError: layer.ErrNodeNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
err: errors.New("something is denied by some acl rule"),
|
|
|
|
expectedError: layer.ErrNodeAccessDenied,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
err := handleError("err message", tc.err)
|
|
|
|
require.True(t, errors.Is(err, tc.expectedError))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 14:47:16 +00:00
|
|
|
|
|
|
|
func TestRetry(t *testing.T) {
|
2023-05-19 13:32:17 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
log := zaptest.NewLogger(t)
|
|
|
|
|
2023-04-04 14:47:16 +00:00
|
|
|
cl := &ServiceClientGRPC{
|
|
|
|
log: zaptest.NewLogger(t),
|
2023-05-19 14:08:59 +00:00
|
|
|
clients: []TreeClient{
|
|
|
|
&treeClientMock{address: "node0"},
|
|
|
|
&treeClientMock{address: "node1"},
|
|
|
|
&treeClientMock{address: "node2"},
|
|
|
|
&treeClientMock{address: "node3"},
|
2023-04-04 14:47:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-19 14:08:59 +00:00
|
|
|
makeFn := func(client grpcService.TreeServiceClient) error {
|
|
|
|
return nil
|
2023-04-04 14:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("first ok", func(t *testing.T) {
|
2023-05-19 14:08:59 +00:00
|
|
|
err := cl.requestWithRetry(ctx, log, makeFn)
|
2023-04-04 14:47:16 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-19 13:32:17 +00:00
|
|
|
require.Equal(t, 0, cl.getStartIndex())
|
2023-05-19 14:08:59 +00:00
|
|
|
resetClients(cl)
|
2023-04-04 14:47:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("first failed", func(t *testing.T) {
|
2023-05-19 14:08:59 +00:00
|
|
|
setErrors(cl.clients[:1])
|
|
|
|
err := cl.requestWithRetry(ctx, log, makeFn)
|
2023-04-04 14:47:16 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-19 13:32:17 +00:00
|
|
|
require.Equal(t, 1, cl.getStartIndex())
|
2023-05-19 14:08:59 +00:00
|
|
|
resetClients(cl)
|
2023-04-04 14:47:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("all failed", func(t *testing.T) {
|
2023-05-19 14:08:59 +00:00
|
|
|
setErrors(cl.clients)
|
|
|
|
err := cl.requestWithRetry(ctx, log, makeFn)
|
2023-04-04 14:47:16 +00:00
|
|
|
require.Error(t, err)
|
2023-05-19 13:32:17 +00:00
|
|
|
require.Equal(t, 0, cl.getStartIndex())
|
2023-05-19 14:08:59 +00:00
|
|
|
resetClients(cl)
|
2023-04-04 14:47:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("round", func(t *testing.T) {
|
2023-05-19 14:08:59 +00:00
|
|
|
setErrors(cl.clients[:2])
|
|
|
|
err := cl.requestWithRetry(ctx, log, makeFn)
|
2023-04-04 14:47:16 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-19 13:32:17 +00:00
|
|
|
require.Equal(t, 2, cl.getStartIndex())
|
2023-05-19 14:08:59 +00:00
|
|
|
resetClientsErrors(cl)
|
2023-04-04 14:47:16 +00:00
|
|
|
|
2023-05-19 14:08:59 +00:00
|
|
|
setErrors(cl.clients[2:])
|
|
|
|
err = cl.requestWithRetry(ctx, log, makeFn)
|
2023-04-04 14:47:16 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-19 13:32:17 +00:00
|
|
|
require.Equal(t, 0, cl.getStartIndex())
|
2023-05-19 14:08:59 +00:00
|
|
|
resetClients(cl)
|
2023-04-04 14:47:16 +00:00
|
|
|
})
|
|
|
|
}
|
2023-05-19 14:08:59 +00:00
|
|
|
|
|
|
|
func resetClients(cl *ServiceClientGRPC) {
|
|
|
|
resetClientsErrors(cl)
|
|
|
|
cl.setStartIndex(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetClientsErrors(cl *ServiceClientGRPC) {
|
|
|
|
for _, client := range cl.clients {
|
|
|
|
node := client.(*treeClientMock)
|
|
|
|
node.err = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setErrors(clients []TreeClient) {
|
|
|
|
for _, client := range clients {
|
|
|
|
node := client.(*treeClientMock)
|
|
|
|
node.err = true
|
|
|
|
}
|
|
|
|
}
|