Welcome to mirror list, hosted at ThFree Co, Russian Federation.

manager_test.go « transaction « gitaly « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e4117ed0b5ffd0dfcd16a2dc9aff6a60f5eb080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package transaction

import (
	"context"
	"errors"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/internal/backchannel"
	"gitlab.com/gitlab-org/gitaly/internal/gitaly/client"
	"gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
	"gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag"
	"gitlab.com/gitlab-org/gitaly/internal/praefect/metadata"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg"
	"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type testTransactionServer struct {
	vote func(*gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error)
	stop func(*gitalypb.StopTransactionRequest) (*gitalypb.StopTransactionResponse, error)
}

func (s *testTransactionServer) VoteTransaction(ctx context.Context, in *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) {
	if s.vote != nil {
		return s.vote(in)
	}
	return nil, nil
}

func (s *testTransactionServer) StopTransaction(ctx context.Context, in *gitalypb.StopTransactionRequest) (*gitalypb.StopTransactionResponse, error) {
	if s.stop != nil {
		return s.stop(in)
	}
	return nil, nil
}

func TestPoolManager_Vote(t *testing.T) {
	testhelper.NewFeatureSets([]featureflag.FeatureFlag{
		featureflag.BackchannelVoting,
	}).Run(t, func(t *testing.T, ctx context.Context) {
		cfg := testcfg.Build(t)

		transactionServer, praefect, stop := runTransactionServer(t, cfg)
		defer stop()

		registry := backchannel.NewRegistry()
		if featureflag.IsEnabled(ctx, featureflag.BackchannelVoting) {
			backchannelConn, err := client.Dial(ctx, praefect.ListenAddr, nil, nil)
			require.NoError(t, err)
			defer backchannelConn.Close()
			praefect = metadata.PraefectServer{BackchannelID: registry.RegisterBackchannel(backchannelConn)}
		}

		manager := NewManager(cfg, registry)

		for _, tc := range []struct {
			desc        string
			transaction metadata.Transaction
			vote        Vote
			voteFn      func(*testing.T, *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error)
			expectedErr error
		}{
			{
				desc: "successful vote",
				transaction: metadata.Transaction{
					ID:   1,
					Node: "node",
				},
				vote: VoteFromData([]byte("foobar")),
				voteFn: func(t *testing.T, request *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) {
					require.Equal(t, uint64(1), request.TransactionId)
					require.Equal(t, "node", request.Node)
					require.Equal(t, request.ReferenceUpdatesHash, VoteFromData([]byte("foobar")).Bytes())

					return &gitalypb.VoteTransactionResponse{
						State: gitalypb.VoteTransactionResponse_COMMIT,
					}, nil
				},
			},
			{
				desc: "aborted vote",
				voteFn: func(t *testing.T, request *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) {
					return &gitalypb.VoteTransactionResponse{
						State: gitalypb.VoteTransactionResponse_ABORT,
					}, nil
				},
				expectedErr: errors.New("transaction was aborted"),
			},
			{
				desc: "stopped vote",
				voteFn: func(t *testing.T, request *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) {
					return &gitalypb.VoteTransactionResponse{
						State: gitalypb.VoteTransactionResponse_STOP,
					}, nil
				},
				expectedErr: errors.New("transaction was stopped"),
			},
			{
				desc: "erroneous vote",
				voteFn: func(t *testing.T, request *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) {
					return nil, status.Error(codes.Internal, "foobar")
				},
				expectedErr: status.Error(codes.Internal, "foobar"),
			},
		} {
			t.Run(tc.desc, func(t *testing.T) {
				transactionServer.vote = func(request *gitalypb.VoteTransactionRequest) (*gitalypb.VoteTransactionResponse, error) {
					return tc.voteFn(t, request)
				}

				err := manager.Vote(ctx, tc.transaction, praefect, tc.vote)
				require.Equal(t, tc.expectedErr, err)
			})
		}
	})
}

func TestPoolManager_Stop(t *testing.T) {
	testhelper.NewFeatureSets([]featureflag.FeatureFlag{
		featureflag.BackchannelVoting,
	}).Run(t, func(t *testing.T, ctx context.Context) {
		cfg := testcfg.Build(t)

		transactionServer, praefect, stop := runTransactionServer(t, cfg)
		defer stop()

		registry := backchannel.NewRegistry()
		if featureflag.IsEnabled(ctx, featureflag.BackchannelVoting) {
			backchannelConn, err := client.Dial(ctx, praefect.ListenAddr, nil, nil)
			require.NoError(t, err)
			defer backchannelConn.Close()
			praefect = metadata.PraefectServer{BackchannelID: registry.RegisterBackchannel(backchannelConn)}
		}

		manager := NewManager(cfg, registry)

		for _, tc := range []struct {
			desc        string
			transaction metadata.Transaction
			stopFn      func(*testing.T, *gitalypb.StopTransactionRequest) (*gitalypb.StopTransactionResponse, error)
			expectedErr error
		}{
			{
				desc: "successful stop",
				transaction: metadata.Transaction{
					ID:   1,
					Node: "node",
				},
				stopFn: func(t *testing.T, request *gitalypb.StopTransactionRequest) (*gitalypb.StopTransactionResponse, error) {
					require.Equal(t, uint64(1), request.TransactionId)
					return &gitalypb.StopTransactionResponse{}, nil
				},
			},
			{
				desc: "erroneous stop",
				stopFn: func(t *testing.T, request *gitalypb.StopTransactionRequest) (*gitalypb.StopTransactionResponse, error) {
					return nil, status.Error(codes.Internal, "foobar")
				},
				expectedErr: status.Error(codes.Internal, "foobar"),
			},
		} {
			t.Run(tc.desc, func(t *testing.T) {
				transactionServer.stop = func(request *gitalypb.StopTransactionRequest) (*gitalypb.StopTransactionResponse, error) {
					return tc.stopFn(t, request)
				}

				err := manager.Stop(ctx, tc.transaction, praefect)
				require.Equal(t, tc.expectedErr, err)
			})
		}
	})
}

func runTransactionServer(t *testing.T, cfg config.Cfg) (*testTransactionServer, metadata.PraefectServer, func()) {
	transactionServer := &testTransactionServer{}

	server := testhelper.NewServerWithAuth(t, nil, nil, cfg.Auth.Token, backchannel.NewRegistry(), testhelper.WithInternalSocket(cfg))
	gitalypb.RegisterRefTransactionServer(server.GrpcServer(), transactionServer)
	server.Start(t)

	listener, address := testhelper.GetLocalhostListener(t)
	go func() { require.NoError(t, server.GrpcServer().Serve(listener)) }()

	praefect := metadata.PraefectServer{
		ListenAddr: "tcp://" + address,
		Token:      cfg.Auth.Token,
	}

	return transactionServer, praefect, server.Stop
}