Skip to content

Commit ca3ef64

Browse files
committed
[FAB-10247] Peer CLI clients use hard-coded timeouts
This CR removes the hard-coded CLI client timeout of 3 seconds and allows users (and tests) to set the value using new config options "peer.client.connTimeout" and "orderer.client.connTimeout". This greatly reduces the time for the peer CLI unit tests to run. Change-Id: I0342a900db3cb7b4fa28e074e03c0bc052b0ed04 Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
1 parent 57d7ac8 commit ca3ef64

17 files changed

+172
-98
lines changed

peer/chaincode/flags_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
)
1616

1717
func TestOrdererFlags(t *testing.T) {
18-
1918
var (
2019
ca = "root.crt"
2120
key = "client.key"

peer/chaincode/invoke_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"testing"
14+
"time"
1415

1516
"github.com/hyperledger/fabric/common/flogging"
1617
"github.com/hyperledger/fabric/common/util"
@@ -22,12 +23,16 @@ import (
2223
pb "github.com/hyperledger/fabric/protos/peer"
2324
"github.com/hyperledger/fabric/protos/utils"
2425
logging "github.com/op/go-logging"
26+
"github.com/spf13/viper"
2527
"github.com/stretchr/testify/assert"
2628
"golang.org/x/net/context"
2729
"google.golang.org/grpc"
2830
)
2931

3032
func TestInvokeCmd(t *testing.T) {
33+
defer viper.Reset()
34+
defer resetFlags()
35+
3136
InitMSP()
3237
mockCF, err := getMockChaincodeCmdFactory()
3338
assert.NoError(t, err, "Error getting mock chaincode command factory")
@@ -50,6 +55,9 @@ func TestInvokeCmd(t *testing.T) {
5055
err = cmd.Execute()
5156
assert.NoError(t, err, "Run chaincode invoke cmd error")
5257

58+
// set timeout for error cases
59+
viper.Set("peer.client.connTimeout", 10*time.Millisecond)
60+
5361
// Error case 1: no orderer endpoints
5462
t.Logf("Start error case 1: no orderer endpoints")
5563
getEndorserClient := common.GetEndorserClientFnc

peer/chaincode/upgrade_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import (
1111
"fmt"
1212
"sync"
1313
"testing"
14+
"time"
1415

1516
"github.com/hyperledger/fabric/msp/mgmt/testtools"
1617
"github.com/hyperledger/fabric/peer/common"
1718
pb "github.com/hyperledger/fabric/protos/peer"
19+
"github.com/spf13/viper"
1820
"github.com/stretchr/testify/assert"
1921
)
2022

@@ -146,10 +148,15 @@ func TestUpgradeCmdSendTXFail(t *testing.T) {
146148
}
147149

148150
func TestUpgradeCmdWithNilCF(t *testing.T) {
151+
defer viper.Reset()
152+
defer resetFlags()
153+
154+
// set timeout for failure cases
155+
viper.Set("peer.client.connTimeout", 10*time.Millisecond)
149156

150157
// trap possible SIGSEV panic
151158
defer func() {
152-
var err error = nil
159+
var err error
153160
if r := recover(); r != nil {
154161
err = fmt.Errorf("%v", r)
155162
}

peer/channel/create_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ import (
2727
"regexp"
2828
"sync"
2929
"testing"
30+
"time"
3031

3132
"github.com/golang/protobuf/proto"
3233
"github.com/hyperledger/fabric/core/config/configtest"
3334
"github.com/hyperledger/fabric/msp/mgmt/testtools"
3435
"github.com/hyperledger/fabric/peer/common"
3536
cb "github.com/hyperledger/fabric/protos/common"
3637
"github.com/hyperledger/fabric/protos/orderer"
38+
"github.com/spf13/viper"
3739
"github.com/stretchr/testify/assert"
3840
"google.golang.org/grpc"
3941
)
@@ -371,6 +373,8 @@ func TestCreateChainBCFail(t *testing.T) {
371373
}
372374

373375
func TestCreateChainDeliverFail(t *testing.T) {
376+
defer resetFlags()
377+
374378
InitMSP()
375379
cleanup := configtest.SetDevFabricConfigPath(t)
376380
defer cleanup()
@@ -438,6 +442,7 @@ func createTxFile(filename string, typ cb.HeaderType, channelID string) (*cb.Env
438442
}
439443

440444
func TestCreateChainFromTx(t *testing.T) {
445+
defer resetFlags()
441446
InitMSP()
442447
cleanup := configtest.SetDevFabricConfigPath(t)
443448
defer cleanup()
@@ -495,6 +500,9 @@ func TestCreateChainFromTx(t *testing.T) {
495500
assert.NoError(t, err, "Couldn't create tx file")
496501
err = cmd.Execute()
497502
assert.NoError(t, err)
503+
504+
// clean-up
505+
resetFlags()
498506
}
499507

500508
func TestCreateChainInvalidTx(t *testing.T) {
@@ -571,6 +579,9 @@ func TestCreateChainInvalidTx(t *testing.T) {
571579
}
572580

573581
func TestCreateChainNilCF(t *testing.T) {
582+
defer viper.Reset()
583+
defer resetFlags()
584+
574585
InitMSP()
575586
cleanup := configtest.SetDevFabricConfigPath(t)
576587
defer cleanup()
@@ -585,6 +596,7 @@ func TestCreateChainNilCF(t *testing.T) {
585596
file := filepath.Join(dir, mockchannel)
586597

587598
// Error case: grpc error
599+
viper.Set("orderer.client.connTimeout", 10*time.Millisecond)
588600
cmd := createCmd(nil)
589601
AddFlags(cmd)
590602
args := []string{"-c", mockchannel, "-f", file, "-o", "localhost:7050"}
@@ -612,6 +624,8 @@ func TestCreateChainNilCF(t *testing.T) {
612624
}
613625

614626
func TestSanityCheckAndSignChannelCreateTx(t *testing.T) {
627+
defer resetFlags()
628+
615629
// Error case 1
616630
env := &cb.Envelope{}
617631
env.Payload = make([]byte, 10)

peer/channel/join_test.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ import (
2121
"os"
2222
"path/filepath"
2323
"testing"
24+
"time"
2425

2526
"github.com/hyperledger/fabric/peer/common"
2627
pb "github.com/hyperledger/fabric/protos/peer"
28+
"github.com/spf13/viper"
2729
"github.com/stretchr/testify/assert"
2830
)
2931

3032
func TestMissingBlockFile(t *testing.T) {
33+
defer resetFlags()
34+
3135
resetFlags()
3236

3337
cmd := joinCmd(nil)
@@ -39,6 +43,8 @@ func TestMissingBlockFile(t *testing.T) {
3943
}
4044

4145
func TestJoin(t *testing.T) {
46+
defer resetFlags()
47+
4248
InitMSP()
4349
resetFlags()
4450

@@ -56,10 +62,10 @@ func TestJoin(t *testing.T) {
5662
Endorsement: &pb.Endorsement{},
5763
}
5864

59-
mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil)
65+
mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)
6066

6167
mockCF := &ChannelCmdFactory{
62-
EndorserClient: mockEndorerClient,
68+
EndorserClient: mockEndorserClient,
6369
BroadcastFactory: mockBroadcastClientFactory,
6470
Signer: signer,
6571
}
@@ -74,6 +80,8 @@ func TestJoin(t *testing.T) {
7480
}
7581

7682
func TestJoinNonExistentBlock(t *testing.T) {
83+
defer resetFlags()
84+
7785
InitMSP()
7886
resetFlags()
7987

@@ -87,10 +95,10 @@ func TestJoinNonExistentBlock(t *testing.T) {
8795
Endorsement: &pb.Endorsement{},
8896
}
8997

90-
mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil)
98+
mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)
9199

92100
mockCF := &ChannelCmdFactory{
93-
EndorserClient: mockEndorerClient,
101+
EndorserClient: mockEndorserClient,
94102
BroadcastFactory: mockBroadcastClientFactory,
95103
Signer: signer,
96104
}
@@ -108,6 +116,8 @@ func TestJoinNonExistentBlock(t *testing.T) {
108116
}
109117

110118
func TestBadProposalResponse(t *testing.T) {
119+
defer resetFlags()
120+
111121
InitMSP()
112122
resetFlags()
113123

@@ -122,10 +132,10 @@ func TestBadProposalResponse(t *testing.T) {
122132
Endorsement: &pb.Endorsement{},
123133
}
124134

125-
mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil)
135+
mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)
126136

127137
mockCF := &ChannelCmdFactory{
128-
EndorserClient: mockEndorerClient,
138+
EndorserClient: mockEndorserClient,
129139
BroadcastFactory: mockBroadcastClientFactory,
130140
Signer: signer,
131141
}
@@ -141,14 +151,19 @@ func TestBadProposalResponse(t *testing.T) {
141151
assert.Error(t, err, "expected join command to fail")
142152
assert.IsType(t, ProposalFailedErr(err.Error()), err, "expected error type of ProposalFailedErr")
143153
}
154+
144155
func TestJoinNilCF(t *testing.T) {
156+
defer viper.Reset()
157+
defer resetFlags()
158+
145159
InitMSP()
146160
resetFlags()
147161

148162
dir, err := ioutil.TempDir("/tmp", "jointest")
149163
assert.NoError(t, err, "Could not create the directory %s", dir)
150164
mockblockfile := filepath.Join(dir, "mockjointest.block")
151165
defer os.RemoveAll(dir)
166+
viper.Set("peer.client.connTimeout", 10*time.Millisecond)
152167
cmd := joinCmd(nil)
153168
AddFlags(cmd)
154169
args := []string{"-b", mockblockfile}

peer/common/common.go

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"io/ioutil"
1313
"os"
1414
"strings"
15+
"time"
1516

1617
"github.com/hyperledger/fabric/bccsp/factory"
1718
"github.com/hyperledger/fabric/common/channelconfig"
@@ -36,6 +37,7 @@ import (
3637
const UndefinedParamValue = ""
3738

3839
var (
40+
defaultConnTimeout = 3 * time.Second
3941
// These function variables (xyzFnc) can be used to invoke corresponding xyz function
4042
// this will allow the invoking packages to mock these functions in their unit test cases
4143

@@ -233,6 +235,11 @@ func configFromEnv(prefix string) (address, override string, clientConfig comm.C
233235
address = viper.GetString(prefix + ".address")
234236
override = viper.GetString(prefix + ".tls.serverhostoverride")
235237
clientConfig = comm.ClientConfig{}
238+
connTimeout := viper.GetDuration(prefix + ".client.connTimeout")
239+
if connTimeout == time.Duration(0) {
240+
connTimeout = defaultConnTimeout
241+
}
242+
clientConfig.Timeout = connTimeout
236243
secOpts := &comm.SecureOptions{
237244
UseTLS: viper.GetBool(prefix + ".tls.enabled"),
238245
RequireClientCert: viper.GetBool(prefix + ".tls.clientAuthRequired")}

peer/common/common_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,22 @@ func TestCheckLogLevel(t *testing.T) {
195195
wantErr: true,
196196
},
197197
{
198-
name: "Valie module name",
198+
name: "Valid module name",
199199
args: args{level: "warning"},
200200
wantErr: false,
201201
},
202202
{
203-
name: "Valie module name",
203+
name: "Valid module name",
204204
args: args{level: "foobaz"},
205205
wantErr: true,
206206
},
207207
{
208-
name: "Valie module name",
208+
name: "Valid module name",
209209
args: args{level: "error"},
210210
wantErr: false,
211211
},
212212
{
213-
name: "Valie module name",
213+
name: "Valid module name",
214214
args: args{level: "info"},
215215
wantErr: false,
216216
},

peer/common/ordererclient.go

-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"context"
1010
"crypto/tls"
1111
"fmt"
12-
"time"
1312

1413
"github.com/hyperledger/fabric/core/comm"
1514
ab "github.com/hyperledger/fabric/protos/orderer"
@@ -30,8 +29,6 @@ func NewOrdererClientFromEnv() (*OrdererClient, error) {
3029
return nil, errors.WithMessage(err,
3130
"failed to load config for OrdererClient")
3231
}
33-
// set timeout
34-
clientConfig.Timeout = time.Second * 3
3532
gClient, err := comm.NewGRPCClient(clientConfig)
3633
if err != nil {
3734
return nil, errors.WithMessage(err,

0 commit comments

Comments
 (0)