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

helper_test.go « proxy « grpc-proxy « praefect « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 615d5a3fd32765a03cbf092f1446e35fc175b5ad (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
package proxy_test

import (
	"net"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/internal/praefect/grpc-proxy/proxy"
	"golang.org/x/net/context"
	"google.golang.org/grpc"

	testservice "gitlab.com/gitlab-org/gitaly/internal/praefect/grpc-proxy/testdata"
)

func newListener(tb testing.TB) net.Listener {
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	require.NoError(tb, err, "must be able to allocate a port for listener")

	return listener
}

func newBackendPinger(tb testing.TB, ctx context.Context) (*grpc.ClientConn, *interceptPinger, func()) {
	ip := &interceptPinger{}

	done := make(chan struct{})
	srvr := grpc.NewServer()
	listener := newListener(tb)

	testservice.RegisterTestServiceServer(srvr, ip)

	go func() {
		defer close(done)
		srvr.Serve(listener)
	}()

	cc, err := grpc.DialContext(
		ctx,
		listener.Addr().String(),
		grpc.WithInsecure(),
		grpc.WithBlock(),
		grpc.WithDefaultCallOptions(
			grpc.CallCustomCodec(proxy.Codec()),
		),
	)
	require.NoError(tb, err)

	cleanup := func() {
		srvr.GracefulStop()
		require.NoError(tb, cc.Close())
		<-done
	}

	return cc, ip, cleanup
}

func newProxy(tb testing.TB, ctx context.Context, director proxy.StreamDirector, svc, method string) (*grpc.ClientConn, func()) {
	proxySrvr := grpc.NewServer(
		grpc.CustomCodec(proxy.Codec()),
		grpc.UnknownServiceHandler(proxy.TransparentHandler(director)),
	)

	proxy.RegisterService(proxySrvr, director, svc, method)

	done := make(chan struct{})
	listener := newListener(tb)

	go func() {
		defer close(done)
		proxySrvr.Serve(listener)
	}()

	proxyCC, err := grpc.DialContext(
		ctx,
		listener.Addr().String(),
		grpc.WithInsecure(),
		grpc.WithBlock(),
	)
	require.NoError(tb, err)

	cleanup := func() {
		proxySrvr.GracefulStop()
		require.NoError(tb, proxyCC.Close())
		<-done
	}

	return proxyCC, cleanup
}

// interceptPinger allows an RPC to be intercepted with a custom
// function defined in each unit test
type interceptPinger struct {
	pingStream func(testservice.TestService_PingStreamServer) error
	pingEmpty  func(context.Context, *testservice.Empty) (*testservice.PingResponse, error)
	ping       func(context.Context, *testservice.PingRequest) (*testservice.PingResponse, error)
	pingError  func(context.Context, *testservice.PingRequest) (*testservice.Empty, error)
	pingList   func(*testservice.PingRequest, testservice.TestService_PingListServer) error
}

func (ip *interceptPinger) PingStream(stream testservice.TestService_PingStreamServer) error {
	return ip.pingStream(stream)
}

func (ip *interceptPinger) PingEmpty(ctx context.Context, req *testservice.Empty) (*testservice.PingResponse, error) {
	return ip.pingEmpty(ctx, req)
}
func (ip *interceptPinger) Ping(ctx context.Context, req *testservice.PingRequest) (*testservice.PingResponse, error) {
	return ip.ping(ctx, req)
}
func (ip *interceptPinger) PingError(ctx context.Context, req *testservice.PingRequest) (*testservice.Empty, error) {
	return ip.pingError(ctx, req)
}
func (ip *interceptPinger) PingList(req *testservice.PingRequest, stream testservice.TestService_PingListServer) error {
	return ip.pingList(req, stream)
}