forked from mwitkow/grpc-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_test.go
211 lines (183 loc) · 5.36 KB
/
proxy_test.go
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package proxy_test
import (
"context"
"flag"
"fmt"
"net"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/test/bufconn"
"github.com/mwitkow/grpc-proxy/proxy"
"github.com/mwitkow/grpc-proxy/testservice"
)
var testBackend = flag.String("test-backend", "", "Service providing TestServiceServer")
// TestIntegrationV1 is a regression test of the proxy.
func TestLegacyBehaviour(t *testing.T) {
// These bufconns are test listeners used to make connections between our
// services. This test actually starts two fully functional grpc services.
proxyBc := bufconn.Listen(10)
// Setup is a little thorough, but here's the gist of it:
// 1. Create the test backend using testservice.DefaultTestServiceServer
// 2. Create the proxy backend using this package
// 3. Make calls to 1 via 2.
// 1.
//lint:ignore SA1019 regression test
testCC, err := backendDialer(t, grpc.WithCodec(proxy.Codec()))
if err != nil {
t.Fatal(err)
}
// 2.
go func() {
// Second, we need to implement the SteamDirector.
directorFn := func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
md, _ := metadata.FromIncomingContext(ctx)
outCtx := metadata.NewOutgoingContext(ctx, md.Copy())
return outCtx, testCC, nil
}
// Set up the proxy server and then serve from it like in step one.
proxySrv := grpc.NewServer(
//lint:ignore SA1019 regression test
grpc.CustomCodec(proxy.Codec()), // was previously needed for proxy to function.
grpc.UnknownServiceHandler(proxy.TransparentHandler(directorFn)),
)
// run the proxy backend
go func() {
t.Log("Running proxySrv")
if err := proxySrv.Serve(proxyBc); err != nil {
if err == grpc.ErrServerStopped {
return
}
t.Logf("running proxy server: %v", err)
}
}()
t.Cleanup(func() {
t.Log("Gracefully stopping proxySrv")
proxySrv.GracefulStop()
})
}()
// 3.
// Connect to the proxy. We should not need to do anything special here -
// users do not need to know they're talking to a proxy.
proxyCC, err := grpc.Dial(
"bufnet",
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
return proxyBc.Dial()
}),
)
if err != nil {
t.Fatalf("dialing proxy: %v", err)
}
proxyClient := testservice.NewTestServiceClient(proxyCC)
// 4. Run the tests!
testservice.TestTestServiceServerImpl(t, proxyClient)
}
func TestNewProxy(t *testing.T) {
proxyBc := bufconn.Listen(10)
// Setup is a little thorough, but here's the gist of it:
// 1. Create the test backend using testservice.DefaultTestServiceServer
// 2. Create the proxy backend using this package
// 3. Make calls to 1 via 2.
// 1.
// First, we need to create a client connection to this backend.
testCC, err := backendDialer(t)
if err != nil {
t.Fatal(err)
}
// 2.
go func() {
t.Helper()
// First, we need to create a client connection to this backend.
proxySrv := proxy.NewProxy(testCC)
// run the proxy backend
go func() {
t.Log("Running proxySrv")
if err := proxySrv.Serve(proxyBc); err != nil {
if err == grpc.ErrServerStopped {
return
}
t.Logf("running proxy server: %v", err)
}
}()
t.Cleanup(func() {
t.Log("Gracefully stopping proxySrv")
proxySrv.GracefulStop()
})
}()
// 3.
// Connect to the proxy. We should not need to do anything special here -
// users do not need to know they're talking to a proxy.
t.Logf("dialing %s", proxyBc.Addr())
proxyCC, err := grpc.Dial(
proxyBc.Addr().String(),
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
return proxyBc.Dial()
}),
)
if err != nil {
t.Fatalf("dialing proxy: %v", err)
}
proxyClient := testservice.NewTestServiceClient(proxyCC)
// 4. Run the tests!
testservice.TestTestServiceServerImpl(t, proxyClient)
}
// backendDialer dials the testservice.TestServiceServer either by connecting
// to the user-supplied server, or by creating a mock server using bufconn.
func backendDialer(t *testing.T, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
t.Helper()
if *testBackend != "" {
return backendSvcDialer(t, *testBackend, opts...)
}
backendBc := bufconn.Listen(10)
// set up the backend using a "real" server over a bufconn
testSrv := grpc.NewServer()
testservice.RegisterTestServiceServer(testSrv, testservice.DefaultTestServiceServer)
// run the test backend
go func() {
t.Log("Running testSrv")
if err := testSrv.Serve(backendBc); err != nil {
if err == grpc.ErrServerStopped {
return
}
t.Logf("running test server: %v", err)
}
}()
t.Cleanup(func() {
t.Log("Gracefully stopping testSrv")
testSrv.GracefulStop()
})
opts = append(opts,
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
return backendBc.Dial()
}),
)
backendCC, err := grpc.Dial(
"bufnet",
opts...,
)
if err != nil {
return nil, fmt.Errorf("dialing backend: %v", err)
}
return backendCC, nil
}
func backendSvcDialer(t *testing.T, addr string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
opts = append(opts,
grpc.WithInsecure(),
grpc.WithBlock(),
)
t.Logf("connecting to %s", addr)
cc, err := grpc.Dial(
addr,
opts...,
)
if err != nil {
return nil, fmt.Errorf("dialing backend: %v", err)
}
return cc, nil
}