|
| 1 | +package coordination |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb" |
| 8 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Coordination" |
| 9 | + "go.uber.org/mock/gomock" |
| 10 | + grpcCodes "google.golang.org/grpc/codes" |
| 11 | + grpcStatus "google.golang.org/grpc/status" |
| 12 | + |
| 13 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" |
| 14 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest" |
| 15 | + "github.com/ydb-platform/ydb-go-sdk/v3/trace" |
| 16 | +) |
| 17 | + |
| 18 | +func TestNewSessionStream(t *testing.T) { |
| 19 | + t.Run("HappyWay", func(t *testing.T) { |
| 20 | + ctx := xtest.Context(t) |
| 21 | + ctrl := gomock.NewController(t) |
| 22 | + client := NewMockCoordinationServiceClient(ctrl) |
| 23 | + sessionStream := NewMockCoordinationService_SessionClient(ctrl) |
| 24 | + sessionStream.EXPECT().Recv().Return(&Ydb_Coordination.SessionResponse{ |
| 25 | + Response: &Ydb_Coordination.SessionResponse_SessionStarted_{ |
| 26 | + SessionStarted: &Ydb_Coordination.SessionResponse_SessionStarted{ |
| 27 | + SessionId: 123456789, |
| 28 | + TimeoutMillis: 987654321, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, nil) |
| 32 | + client.EXPECT().Session(gomock.Any()).Return(sessionStream, nil) |
| 33 | + s, err := newSessionStream(ctx, client, &trace.Coordination{}) |
| 34 | + require.NoError(t, err) |
| 35 | + require.NotNil(t, s) |
| 36 | + require.EqualValues(t, 123456789, s.sessionID) |
| 37 | + }) |
| 38 | + t.Run("TransportError", func(t *testing.T) { |
| 39 | + t.Run("On", func(t *testing.T) { |
| 40 | + t.Run("NewStream", func(t *testing.T) { |
| 41 | + ctx := xtest.Context(t) |
| 42 | + ctrl := gomock.NewController(t) |
| 43 | + client := NewMockCoordinationServiceClient(ctrl) |
| 44 | + sessionStream := NewMockCoordinationService_SessionClient(ctrl) |
| 45 | + sessionStream.EXPECT().Recv().Return(nil, |
| 46 | + xerrors.Transport(grpcStatus.Error(grpcCodes.Unavailable, "")), |
| 47 | + ) |
| 48 | + client.EXPECT().Session(gomock.Any()).Return(sessionStream, nil) |
| 49 | + s, err := newSessionStream(ctx, client, &trace.Coordination{}) |
| 50 | + require.Error(t, err) |
| 51 | + require.True(t, xerrors.IsTransportError(err, grpcCodes.Unavailable)) |
| 52 | + require.Nil(t, s) |
| 53 | + }) |
| 54 | + t.Run("Recv", func(t *testing.T) { |
| 55 | + ctx := xtest.Context(t) |
| 56 | + ctrl := gomock.NewController(t) |
| 57 | + client := NewMockCoordinationServiceClient(ctrl) |
| 58 | + client.EXPECT().Session(gomock.Any()).Return(nil, |
| 59 | + xerrors.Transport(grpcStatus.Error(grpcCodes.ResourceExhausted, "")), |
| 60 | + ) |
| 61 | + s, err := newSessionStream(ctx, client, &trace.Coordination{}) |
| 62 | + require.True(t, xerrors.IsTransportError(err, grpcCodes.ResourceExhausted)) |
| 63 | + require.Nil(t, s) |
| 64 | + }) |
| 65 | + }) |
| 66 | + }) |
| 67 | + t.Run("OperationError", func(t *testing.T) { |
| 68 | + t.Run("On", func(t *testing.T) { |
| 69 | + t.Run("NewStream", func(t *testing.T) { |
| 70 | + ctx := xtest.Context(t) |
| 71 | + ctrl := gomock.NewController(t) |
| 72 | + client := NewMockCoordinationServiceClient(ctrl) |
| 73 | + client.EXPECT().Session(gomock.Any()).Return( |
| 74 | + nil, xerrors.Operation(xerrors.WithStatusCode(Ydb.StatusIds_ABORTED)), |
| 75 | + ) |
| 76 | + s, err := newSessionStream(ctx, client, &trace.Coordination{}) |
| 77 | + require.True(t, xerrors.IsOperationError(err, Ydb.StatusIds_ABORTED)) |
| 78 | + require.Nil(t, s) |
| 79 | + }) |
| 80 | + t.Run("Recv", func(t *testing.T) { |
| 81 | + ctx := xtest.Context(t) |
| 82 | + ctrl := gomock.NewController(t) |
| 83 | + client := NewMockCoordinationServiceClient(ctrl) |
| 84 | + sessionStream := NewMockCoordinationService_SessionClient(ctrl) |
| 85 | + sessionStream.EXPECT().Recv().Return(&Ydb_Coordination.SessionResponse{ |
| 86 | + Response: &Ydb_Coordination.SessionResponse_Failure_{ |
| 87 | + Failure: &Ydb_Coordination.SessionResponse_Failure{ |
| 88 | + Status: Ydb.StatusIds_ABORTED, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, nil) |
| 92 | + client.EXPECT().Session(gomock.Any()).Return(sessionStream, nil) |
| 93 | + s, err := newSessionStream(ctx, client, &trace.Coordination{}) |
| 94 | + require.True(t, xerrors.IsOperationError(err, Ydb.StatusIds_ABORTED)) |
| 95 | + require.Nil(t, s) |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | +} |
0 commit comments