Skip to content

Commit

Permalink
[FAB-12084] address simple lostcancel vet issues
Browse files Browse the repository at this point in the history
Also enable lostcancel checks on vet for all but the two remaining
issues.

Change-Id: I9cd94c28638e7c7db826ac0aee6e13953bdd8be3
Signed-off-by: Matthew Sykes <[email protected]>
  • Loading branch information
sykesm committed Sep 22, 2018
1 parent a6a07ce commit 8a5961d
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 43 deletions.
4 changes: 2 additions & 2 deletions common/crypto/tlsgen/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func TestTLSCA(t *testing.T) {
}
tlsCfg.RootCAs.AppendCertsFromPEM(ca.CertBytes())
tlsOpts := grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg))
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, fmt.Sprintf("127.0.0.1:%d", randomPort), tlsOpts, grpc.WithBlock())
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions core/chaincode/accesscontrol/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func newClient(t *testing.T, port int, cert *tls.Certificate, peerCACert []byte)
tlsCfg.Certificates = []tls.Certificate{*cert}
}
tlsOpts := grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg))
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, fmt.Sprintf("localhost:%d", port), tlsOpts, grpc.WithBlock())
if err != nil {
return nil, err
Expand Down
10 changes: 6 additions & 4 deletions core/comm/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,12 @@ func NewClientConnectionWithAddress(peerAddress string, block bool, tslEnabled b
if block {
opts = append(opts, grpc.WithBlock())
}
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(MaxRecvMsgSize),
grpc.MaxCallSendMsgSize(MaxSendMsgSize)))
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, defaultTimeout)
opts = append(opts, grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(MaxRecvMsgSize),
grpc.MaxCallSendMsgSize(MaxSendMsgSize),
))
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
conn, err := grpc.DialContext(ctx, peerAddress, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions core/comm/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ func testInvoke(
creds, err := cs.GetDeliverServiceCredentials(channelID)
assert.NoError(t, err)
endpoint := fmt.Sprintf("localhost:%d", s.port)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, endpoint, grpc.WithTransportCredentials(creds), grpc.WithBlock())
if shouldSucceed {
assert.NoError(t, err)
Expand Down
28 changes: 10 additions & 18 deletions core/comm/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func (esss *emptyServiceServer) EmptyStream(stream testpb.EmptyService_EmptyStre

// invoke the EmptyCall RPC
func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Empty, error) {
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, timeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
//create GRPC client conn
clientConn, err := grpc.DialContext(ctx, address, dialOptions...)
if err != nil {
Expand All @@ -142,12 +142,8 @@ func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Emp
//create GRPC client
client := testpb.NewEmptyServiceClient(clientConn)

callCtx := context.Background()
callCtx, cancel := context.WithTimeout(callCtx, timeout)
defer cancel()

//invoke service
empty, err := client.EmptyCall(callCtx, new(testpb.Empty))
empty, err := client.EmptyCall(context.Background(), new(testpb.Empty))
if err != nil {
return nil, err
}
Expand All @@ -157,8 +153,8 @@ func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Emp

// invoke the EmptyStream RPC
func invokeEmptyStream(address string, dialOptions []grpc.DialOption) (*testpb.Empty, error) {
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, timeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
//create GRPC client conn
clientConn, err := grpc.DialContext(ctx, address, dialOptions...)
if err != nil {
Expand Down Expand Up @@ -632,8 +628,7 @@ func TestNewGRPCServerFromListener(t *testing.T) {
_, err = invokeEmptyCall(testAddress, dialOptions)

if err != nil {
t.Fatalf("GRPC client failed to invoke the EmptyCall service on %s: %v",
testAddress, err)
t.Fatalf("GRPC client failed to invoke the EmptyCall service on %s: %v", testAddress, err)
} else {
t.Log("GRPC client successfully invoked the EmptyCall service: " + testAddress)
}
Expand Down Expand Up @@ -1543,18 +1538,15 @@ func TestKeepaliveClientResponse(t *testing.T) {
defer srv.Stop()

// test that connection does not close with response to ping
connectCtx, cancel := context.WithDeadline(
context.Background(),
time.Now().Add(1*time.Second))
connectCtx, cancel := context.WithTimeout(context.Background(), time.Second)
clientTransport, err := transport.NewClientTransport(
connectCtx,
context.Background(),
transport.TargetInfo{Addr: testAddress},
transport.ConnectOptions{},
func() {})
if err != nil {
cancel()
}
func() {},
)
cancel()
assert.NoError(t, err, "Unexpected error creating client transport")
defer clientTransport.Close()
// sleep past keepalive timeout
Expand Down
1 change: 1 addition & 0 deletions core/deliverservice/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (bc *broadcastClient) connect() error {
if err != nil {
logger.Error("Connection to ", endpoint, "established but was unable to create gRPC stream:", err)
conn.Close()
cf()
return err
}
err = bc.afterConnect(conn, abc, cf, endpoint)
Expand Down
4 changes: 2 additions & 2 deletions core/deliverservice/deliveryclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ func DefaultConnectionFactory(channelID string) func(endpoint string) (*grpc.Cli
} else {
dialOpts = append(dialOpts, grpc.WithInsecure())
}
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, getConnectionTimeout())
ctx, cancel := context.WithTimeout(context.Background(), getConnectionTimeout())
defer cancel()
return grpc.DialContext(ctx, endpoint, dialOpts...)
}
}
Expand Down
10 changes: 3 additions & 7 deletions core/peer/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func createCertPool(rootCAs [][]byte) (*x509.CertPool, error) {
func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Empty, error) {
//add DialOptions
dialOptions = append(dialOptions, grpc.WithBlock())
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, timeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
//create GRPC client conn
clientConn, err := grpc.DialContext(ctx, address, dialOptions...)
if err != nil {
Expand All @@ -73,12 +73,8 @@ func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Emp
//create GRPC client
client := testpb.NewTestServiceClient(clientConn)

callCtx := context.Background()
callCtx, cancel := context.WithTimeout(callCtx, timeout)
defer cancel()

//invoke service
empty, err := client.EmptyCall(callCtx, new(testpb.Empty))
empty, err := client.EmptyCall(context.Background(), new(testpb.Empty))
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions gossip/comm/comm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func handshaker(endpoint string, comm Comm, t *testing.T, connMutator msgMutator
secureOpts = grpc.WithInsecure()
}
acceptChan := comm.Accept(acceptAll)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, "localhost:9611", secureOpts, grpc.WithBlock())
assert.NoError(t, err, "%v", err)
if err != nil {
Expand Down Expand Up @@ -530,8 +530,8 @@ func TestCloseConn(t *testing.T) {
}
ta := credentials.NewTLS(tlsCfg)

ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, "localhost:1611", grpc.WithTransportCredentials(ta), grpc.WithBlock())
assert.NoError(t, err, "%v", err)
cl := proto.NewGossipClient(conn)
Expand Down
4 changes: 2 additions & 2 deletions gossip/comm/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func TestCertificateExtraction(t *testing.T) {
Certificates: []tls.Certificate{clientCert},
InsecureSkipVerify: true,
})
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, "localhost:5611", grpc.WithTransportCredentials(ta), grpc.WithBlock())
assert.NoError(t, err, "%v", err)

Expand Down
10 changes: 10 additions & 0 deletions scripts/golinter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ if [ -n "$OUTPUT" ]; then
echo $OUTPUT
exit 1
fi
# This block should removed once the lostcancel issues have been corrected and
# the previous invocation of vet should remove the -lostcancel=false option.
# FAB-12082 - gossip
# FAB-12083 - orderer
OUTPUT="$(go vet -lostcancel=true ./... 2>&1 | grep -Ev '^#|(fabric/)?gossip/comm/comm_impl.go|(fabric)?orderer/common/cluster/comm.go' || true)"
if [ -n "$OUTPUT" ]; then
echo "The following files contain go vet errors"
echo $OUTPUT
exit 1
fi

0 comments on commit 8a5961d

Please sign in to comment.