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"
|
2023-05-19 13:32:17 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2023-03-14 14:31:15 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-05-19 13:32:17 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services/client"
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
grpcDialOpt := grpc.WithTransportCredentials(insecure.NewCredentials())
|
2023-04-04 14:47:16 +00:00
|
|
|
cl := &ServiceClientGRPC{
|
|
|
|
log: zaptest.NewLogger(t),
|
2023-05-19 13:32:17 +00:00
|
|
|
clients: []*client.TreeClient{
|
|
|
|
client.NewTreeClient("node0", grpcDialOpt),
|
|
|
|
client.NewTreeClient("node1", grpcDialOpt),
|
|
|
|
client.NewTreeClient("node2", grpcDialOpt),
|
|
|
|
client.NewTreeClient("node3", grpcDialOpt),
|
2023-04-04 14:47:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-19 13:32:17 +00:00
|
|
|
makeFn := func(shouldFail []string) func(grpcService.TreeServiceClient) error {
|
|
|
|
return func(client grpcService.TreeServiceClient) error {
|
|
|
|
|
|
|
|
//for _, item := range shouldFail {
|
|
|
|
//if item == client.address {
|
|
|
|
// return errors.New("not found")
|
|
|
|
//}
|
|
|
|
//}
|
2023-04-11 12:16:14 +00:00
|
|
|
return nil
|
2023-04-04 14:47:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("first ok", func(t *testing.T) {
|
2023-05-19 13:32:17 +00:00
|
|
|
err := cl.requestWithRetry(ctx, log, makeFn([]string{}))
|
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())
|
|
|
|
cl.setStartIndex(0)
|
2023-04-04 14:47:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("first failed", func(t *testing.T) {
|
2023-05-19 13:32:17 +00:00
|
|
|
err := cl.requestWithRetry(ctx, log, makeFn([]string{"node0"}))
|
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())
|
|
|
|
cl.setStartIndex(0)
|
2023-04-04 14:47:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("all failed", func(t *testing.T) {
|
2023-05-19 13:32:17 +00:00
|
|
|
err := cl.requestWithRetry(ctx, log, makeFn([]string{"node0", "node1", "node2", "node3"}))
|
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())
|
|
|
|
cl.setStartIndex(0)
|
2023-04-04 14:47:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("round", func(t *testing.T) {
|
2023-05-19 13:32:17 +00:00
|
|
|
err := cl.requestWithRetry(ctx, log, makeFn([]string{"node0", "node1"}))
|
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-04-04 14:47:16 +00:00
|
|
|
|
2023-05-19 13:32:17 +00:00
|
|
|
err = cl.requestWithRetry(ctx, log, makeFn([]string{"node2", "node3"}))
|
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())
|
|
|
|
cl.setStartIndex(0)
|
2023-04-04 14:47:16 +00:00
|
|
|
})
|
|
|
|
}
|