Skip to content

Commit 9c51325

Browse files
committed
[FAB-9104] Improve core/comm tests
There were a number of flaky tests in the core/comm package after upgrading the gRPC package. Many seemed to be due to running tests in parallel while others were just due to changes in library behavior. Fixes flaky TestNewConnection test as well as eliminating race conditions for the tests Change-Id: If2465ab3332408d7c7d7b87abe14ec4ac4393bc8 Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
1 parent ed6ae9e commit 9c51325

File tree

9 files changed

+46
-91
lines changed

9 files changed

+46
-91
lines changed

core/comm/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func NewGRPCClient(config ClientConfig) (*GRPCClient, error) {
6060
grpc.WithBlock())
6161
client.timeout = config.Timeout
6262
// set send/recv message size to package defaults
63-
client.maxRecvMsgSize = maxRecvMsgSize
64-
client.maxSendMsgSize = maxSendMsgSize
63+
client.maxRecvMsgSize = MaxRecvMsgSize
64+
client.maxSendMsgSize = MaxSendMsgSize
6565

6666
return client, nil
6767
}

core/comm/client_test.go

+30-45
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ import (
2121
testpb "github.com/hyperledger/fabric/core/comm/testdata/grpc"
2222
"github.com/pkg/errors"
2323
"github.com/stretchr/testify/assert"
24-
netctx "golang.org/x/net/context"
24+
"google.golang.org/grpc"
25+
"google.golang.org/grpc/credentials"
2526
)
2627

27-
var caPEM, certPEM, keyPEM, serverKey, serverPEM []byte
2828
var testClientCert, testServerCert tls.Certificate
2929
var testTimeout = 1 * time.Second // conservative
3030

3131
type echoServer struct{}
3232

33-
func (es *echoServer) EchoCall(ctx netctx.Context,
33+
func (es *echoServer) EchoCall(ctx context.Context,
3434
echo *testpb.Echo) (*testpb.Echo, error) {
3535
return echo, nil
3636
}
3737

3838
func TestNewGRPCClient_GoodConfig(t *testing.T) {
3939
t.Parallel()
40-
loadCerts(t)
40+
caPEM, certPEM, keyPEM, _, _ := loadCerts(t)
4141

4242
config := comm.ClientConfig{}
4343
client, err := comm.NewGRPCClient(config)
@@ -82,7 +82,7 @@ func TestNewGRPCClient_GoodConfig(t *testing.T) {
8282

8383
func TestNewGRPCClient_BadConfig(t *testing.T) {
8484
t.Parallel()
85-
loadCerts(t)
85+
_, certPEM, keyPEM, _, _ := loadCerts(t)
8686
// bad root cert
8787
config := comm.ClientConfig{
8888
SecOpts: &comm.SecureOptions{
@@ -143,7 +143,7 @@ func TestNewConnection_Timeout(t *testing.T) {
143143

144144
func TestNewConnection(t *testing.T) {
145145
t.Parallel()
146-
loadCerts(t)
146+
caPEM, certPEM, keyPEM, _, _ := loadCerts(t)
147147
certPool := x509.NewCertPool()
148148
ok := certPool.AppendCertsFromPEM(caPEM)
149149
if !ok {
@@ -269,24 +269,15 @@ func TestNewConnection(t *testing.T) {
269269
t.Fatalf("error creating server for test: %v", err)
270270
}
271271
defer lis.Close()
272+
serverOpts := []grpc.ServerOption{}
272273
if test.serverTLS != nil {
273-
t.Log("starting server with TLS")
274-
tlsLis := tls.NewListener(lis, test.serverTLS)
275-
defer tlsLis.Close()
276-
go func() {
277-
rawConn, err := tlsLis.Accept()
278-
defer rawConn.Close()
279-
if err != nil {
280-
t.Fatalf("error accepting TLS connection: %v", err)
281-
}
282-
t.Logf("server: accepted from %s", rawConn.RemoteAddr())
283-
sconn := tls.Server(rawConn, test.serverTLS)
284-
err = sconn.Handshake()
285-
if err != nil {
286-
t.Logf("tls handshake error: %v", err)
287-
}
288-
}()
274+
serverOpts = append(
275+
serverOpts,
276+
grpc.Creds(credentials.NewTLS(test.serverTLS)))
289277
}
278+
srv := grpc.NewServer(serverOpts...)
279+
defer srv.Stop()
280+
go srv.Serve(lis)
290281
client, err := comm.NewGRPCClient(test.config)
291282
if err != nil {
292283
t.Fatalf("error creating client for test: %v", err)
@@ -306,7 +297,7 @@ func TestNewConnection(t *testing.T) {
306297

307298
func TestSetServerRootCAs(t *testing.T) {
308299
t.Parallel()
309-
loadCerts(t)
300+
caPEM, _, _, _, _ := loadCerts(t)
310301

311302
config := comm.ClientConfig{
312303
SecOpts: &comm.SecureOptions{
@@ -325,26 +316,10 @@ func TestSetServerRootCAs(t *testing.T) {
325316
t.Fatalf("failed to create listener for test server: %v", err)
326317
}
327318
defer lis.Close()
328-
tlsConf := &tls.Config{
329-
Certificates: []tls.Certificate{testServerCert}}
330-
tlsLis := tls.NewListener(lis, tlsConf)
331-
defer tlsLis.Close()
332-
go func() {
333-
// 3 connection tests
334-
for i := 0; i < 3; i++ {
335-
rawConn, err := tlsLis.Accept()
336-
defer rawConn.Close()
337-
if err != nil {
338-
t.Fatalf("error accepting TLS connection: %v", err)
339-
}
340-
t.Logf("server: accepted from %s", rawConn.RemoteAddr())
341-
sconn := tls.Server(rawConn, tlsConf)
342-
err = sconn.Handshake()
343-
if err != nil {
344-
t.Logf("tls handshake error: %v", err)
345-
}
346-
}
347-
}()
319+
srv := grpc.NewServer(grpc.Creds(credentials.NewTLS(
320+
&tls.Config{Certificates: []tls.Certificate{testServerCert}})))
321+
defer srv.Stop()
322+
go srv.Serve(lis)
348323

349324
// initial config should work
350325
t.Log("running initial good config")
@@ -371,7 +346,9 @@ func TestSetServerRootCAs(t *testing.T) {
371346
conn, err = client.NewConnection(address, "")
372347
assert.NoError(t, err)
373348
assert.NotNil(t, conn)
374-
conn.Close()
349+
if conn != nil {
350+
conn.Close()
351+
}
375352

376353
// bad root cert
377354
t.Log("running bad root cert")
@@ -472,7 +449,13 @@ func TestSetMessageSize(t *testing.T) {
472449
}
473450
}
474451

475-
func loadCerts(t *testing.T) {
452+
func loadCerts(t *testing.T) (
453+
caPEM,
454+
certPEM,
455+
keyPEM,
456+
serverKey,
457+
serverPEM []byte) {
458+
476459
t.Helper()
477460
var err error
478461
caPEM, err = ioutil.ReadFile(filepath.Join("testdata", "certs",
@@ -497,4 +480,6 @@ func loadCerts(t *testing.T) {
497480
testServerCert, err = tls.LoadX509KeyPair(filepath.Join("testdata", "certs",
498481
"Org1-server1-cert.pem"), filepath.Join("testdata", "certs",
499482
"Org1-server1-key.pem"))
483+
484+
return caPEM, certPEM, keyPEM, serverKey, serverPEM
500485
}

core/comm/config.go

+2-26
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ var (
2121
// Is TLS enabled
2222
tlsEnabled bool
2323
// Max send and receive bytes for grpc clients and servers
24-
maxRecvMsgSize = 100 * 1024 * 1024
25-
maxSendMsgSize = 100 * 1024 * 1024
24+
MaxRecvMsgSize = 100 * 1024 * 1024
25+
MaxSendMsgSize = 100 * 1024 * 1024
2626
// Default peer keepalive options
2727
keepaliveOptions = &KeepaliveOptions{
2828
ClientInterval: time.Duration(1) * time.Minute, // 1 min
@@ -123,30 +123,6 @@ func TLSEnabled() bool {
123123
return tlsEnabled
124124
}
125125

126-
// MaxRecvMsgSize returns the maximum message size in bytes that gRPC clients
127-
// and servers can receive
128-
func MaxRecvMsgSize() int {
129-
return maxRecvMsgSize
130-
}
131-
132-
// SetMaxRecvMsgSize sets the maximum message size in bytes that gRPC clients
133-
// and servers can receive
134-
func SetMaxRecvMsgSize(size int) {
135-
maxRecvMsgSize = size
136-
}
137-
138-
// MaxSendMsgSize returns the maximum message size in bytes that gRPC clients
139-
// and servers can send
140-
func MaxSendMsgSize() int {
141-
return maxSendMsgSize
142-
}
143-
144-
// SetMaxSendMsgSize sets the maximum message size in bytes that gRPC clients
145-
// and servers can send
146-
func SetMaxSendMsgSize(size int) {
147-
maxSendMsgSize = size
148-
}
149-
150126
// DefaultKeepaliveOptions returns sane default keepalive settings for gRPC
151127
// servers and clients
152128
func DefaultKeepaliveOptions() *KeepaliveOptions {

core/comm/config_test.go

-9
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,10 @@ import (
1616
func TestConfig(t *testing.T) {
1717
t.Parallel()
1818
// check the defaults
19-
assert.EqualValues(t, maxRecvMsgSize, MaxRecvMsgSize())
20-
assert.EqualValues(t, maxSendMsgSize, MaxSendMsgSize())
2119
assert.EqualValues(t, keepaliveOptions, DefaultKeepaliveOptions())
2220
assert.EqualValues(t, false, TLSEnabled())
2321
assert.EqualValues(t, true, configurationCached)
2422

25-
// set send/recv msg sizes
26-
size := 10 * 1024 * 1024
27-
SetMaxRecvMsgSize(size)
28-
SetMaxSendMsgSize(size)
29-
assert.EqualValues(t, size, MaxRecvMsgSize())
30-
assert.EqualValues(t, size, MaxSendMsgSize())
31-
3223
// reset cache
3324
configurationCached = false
3425
viper.Set("peer.tls.enabled", true)

core/comm/connection.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ func NewClientConnectionWithAddress(peerAddress string, block bool, tslEnabled b
205205
if block {
206206
opts = append(opts, grpc.WithBlock())
207207
}
208-
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(MaxRecvMsgSize()),
209-
grpc.MaxCallSendMsgSize(MaxSendMsgSize())))
208+
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(MaxRecvMsgSize),
209+
grpc.MaxCallSendMsgSize(MaxSendMsgSize)))
210210
ctx := context.Background()
211211
ctx, _ = context.WithTimeout(ctx, defaultTimeout)
212212
conn, err := grpc.DialContext(ctx, peerAddress, opts...)

core/comm/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ func NewGRPCServerFromListener(listener net.Listener, serverConfig ServerConfig)
118118
}
119119
}
120120
// set max send and recv msg sizes
121-
serverOpts = append(serverOpts, grpc.MaxSendMsgSize(MaxSendMsgSize()))
122-
serverOpts = append(serverOpts, grpc.MaxRecvMsgSize(MaxRecvMsgSize()))
121+
serverOpts = append(serverOpts, grpc.MaxSendMsgSize(MaxSendMsgSize))
122+
serverOpts = append(serverOpts, grpc.MaxRecvMsgSize(MaxRecvMsgSize))
123123
// set the keepalive options
124124
serverOpts = append(serverOpts, ServerKeepaliveOptions(serverConfig.KaOpts)...)
125125
// set connection timeout

core/comm/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ func TestNewSecureGRPCServer(t *testing.T) {
637637
tlsVersion := tlsVersion
638638
t.Run(tlsVersions[counter], func(t *testing.T) {
639639
t.Parallel()
640-
_, err = invokeEmptyCall(testAddress,
640+
_, err := invokeEmptyCall(testAddress,
641641
[]grpc.DialOption{grpc.WithTransportCredentials(
642642
credentials.NewTLS(&tls.Config{
643643
RootCAs: certPool,

core/deliverservice/deliveryclient.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ func DefaultConnectionFactory(channelID string) func(endpoint string) (*grpc.Cli
225225
return func(endpoint string) (*grpc.ClientConn, error) {
226226
dialOpts := []grpc.DialOption{grpc.WithBlock()}
227227
// set max send/recv msg sizes
228-
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(comm.MaxRecvMsgSize()),
229-
grpc.MaxCallSendMsgSize(comm.MaxSendMsgSize())))
228+
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(comm.MaxRecvMsgSize),
229+
grpc.MaxCallSendMsgSize(comm.MaxSendMsgSize)))
230230
// set the keepalive options
231231
kaOpts := comm.DefaultKeepaliveOptions()
232232
if viper.IsSet("peer.keepalive.deliveryClient.interval") {

peer/node/start.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,11 @@ func serve(args []string) error {
249249
secureDialOpts := func() []grpc.DialOption {
250250
var dialOpts []grpc.DialOption
251251
// set max send/recv msg sizes
252-
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(comm.MaxRecvMsgSize()),
253-
grpc.MaxCallSendMsgSize(comm.MaxSendMsgSize())))
252+
dialOpts = append(
253+
dialOpts,
254+
grpc.WithDefaultCallOptions(
255+
grpc.MaxCallRecvMsgSize(comm.MaxRecvMsgSize),
256+
grpc.MaxCallSendMsgSize(comm.MaxSendMsgSize)))
254257
// set the keepalive options
255258
kaOpts := comm.DefaultKeepaliveOptions()
256259
if viper.IsSet("peer.keepalive.client.interval") {

0 commit comments

Comments
 (0)