diff --git a/peer/channel/channel.go b/peer/channel/channel.go index ddb4156667a..f998c236fd5 100644 --- a/peer/channel/channel.go +++ b/peer/channel/channel.go @@ -74,12 +74,15 @@ var channelCmd = &cobra.Command{ Long: fmt.Sprintf("%s specific commands.", channelFuncName), } +type BroadcastClientFactory func() (common.BroadcastClient, error) + // ChannelCmdFactory holds the clients used by ChannelCmdFactory type ChannelCmdFactory struct { - EndorserClient pb.EndorserClient - Signer msp.SigningIdentity - BroadcastClient common.BroadcastClient - DeliverClient deliverClientIntf + EndorserClient pb.EndorserClient + Signer msp.SigningIdentity + BroadcastClient common.BroadcastClient + DeliverClient deliverClientIntf + BroadcastFactory BroadcastClientFactory } // InitCmdFactory init the ChannelCmdFactor with default clients @@ -93,7 +96,10 @@ func InitCmdFactory(isOrdererRequired bool) (*ChannelCmdFactory, error) { return nil, fmt.Errorf("Error getting default signer: %s", err) } - cmdFact.BroadcastClient, err = common.GetBroadcastClient() + cmdFact.BroadcastFactory = func() (common.BroadcastClient, error) { + return common.GetBroadcastClient() + } + if err != nil { return nil, fmt.Errorf("Error getting broadcast client: %s", err) } diff --git a/peer/channel/create.go b/peer/channel/create.go index c6443a4a6b6..16733be0b00 100644 --- a/peer/channel/create.go +++ b/peer/channel/create.go @@ -123,15 +123,19 @@ func sendCreateChainTransaction(cf *ChannelCmdFactory) error { return err } } + var broadcastClient common.BroadcastClient + broadcastClient, err = cf.BroadcastFactory() + if err != nil { + return err + } - err = cf.BroadcastClient.Send(chCrtEnv) + defer broadcastClient.Close() + err = broadcastClient.Send(chCrtEnv) return err } func executeCreate(cf *ChannelCmdFactory) error { - defer cf.BroadcastClient.Close() - var err error if err = sendCreateChainTransaction(cf); err != nil { diff --git a/peer/channel/create_test.go b/peer/channel/create_test.go index 783272cea07..90e5f5f1de0 100644 --- a/peer/channel/create_test.go +++ b/peer/channel/create_test.go @@ -77,6 +77,10 @@ func initMSP() { } } +func mockBroadcastClientFactory() (common.BroadcastClient, error) { + return common.GetMockBroadcastClient(nil), nil +} + func TestCreateChain(t *testing.T) { InitMSP() @@ -89,12 +93,10 @@ func TestCreateChain(t *testing.T) { t.Fatalf("Get default signer error: %v", err) } - mockBroadcastClient := common.GetMockBroadcastClient(nil) - mockCF := &ChannelCmdFactory{ - BroadcastClient: mockBroadcastClient, - Signer: signer, - DeliverClient: &mockDeliverClient{}, + BroadcastFactory: mockBroadcastClientFactory, + Signer: signer, + DeliverClient: &mockDeliverClient{}, } cmd := createCmd(mockCF) @@ -122,12 +124,10 @@ func TestCreateChainWithDefaultAnchorPeers(t *testing.T) { t.Fatalf("Get default signer error: %v", err) } - mockBroadcastClient := common.GetMockBroadcastClient(nil) - mockCF := &ChannelCmdFactory{ - BroadcastClient: mockBroadcastClient, - Signer: signer, - DeliverClient: &mockDeliverClient{}, + BroadcastFactory: mockBroadcastClientFactory, + Signer: signer, + DeliverClient: &mockDeliverClient{}, } cmd := createCmd(mockCF) @@ -156,12 +156,13 @@ func TestCreateChainBCFail(t *testing.T) { } sendErr := fmt.Errorf("send create tx failed") - mockBroadcastClient := common.GetMockBroadcastClient(sendErr) mockCF := &ChannelCmdFactory{ - BroadcastClient: mockBroadcastClient, - Signer: signer, - DeliverClient: &mockDeliverClient{}, + BroadcastFactory: func() (common.BroadcastClient, error) { + return common.GetMockBroadcastClient(sendErr), nil + }, + Signer: signer, + DeliverClient: &mockDeliverClient{}, } cmd := createCmd(mockCF) @@ -173,7 +174,7 @@ func TestCreateChainBCFail(t *testing.T) { expectedErrMsg := sendErr.Error() if err := cmd.Execute(); err == nil { - t.Errorf("expected create chain to fail with broadcast error") + t.Error("expected create chain to fail with broadcast error") } else { if err.Error() != expectedErrMsg { t.Errorf("Run create chain get unexpected error: %s(expected %s)", err.Error(), expectedErrMsg) @@ -193,14 +194,12 @@ func TestCreateChainDeliverFail(t *testing.T) { t.Fatalf("Get default signer error: %v", err) } - mockBroadcastClient := common.GetMockBroadcastClient(nil) - recvErr := fmt.Errorf("deliver create tx failed") mockCF := &ChannelCmdFactory{ - BroadcastClient: mockBroadcastClient, - Signer: signer, - DeliverClient: &mockDeliverClient{recvErr}, + BroadcastFactory: mockBroadcastClientFactory, + Signer: signer, + DeliverClient: &mockDeliverClient{recvErr}, } cmd := createCmd(mockCF) @@ -268,12 +267,10 @@ func TestCreateChainFromTx(t *testing.T) { t.Fatalf("Get default signer error: %v", err) } - mockBroadcastClient := common.GetMockBroadcastClient(nil) - mockCF := &ChannelCmdFactory{ - BroadcastClient: mockBroadcastClient, - Signer: signer, - DeliverClient: &mockDeliverClient{}, + BroadcastFactory: mockBroadcastClientFactory, + Signer: signer, + DeliverClient: &mockDeliverClient{}, } cmd := createCmd(mockCF) @@ -314,12 +311,10 @@ func TestCreateChainInvalidTx(t *testing.T) { t.Fatalf("Get default signer error: %v", err) } - mockBroadcastClient := common.GetMockBroadcastClient(nil) - mockCF := &ChannelCmdFactory{ - BroadcastClient: mockBroadcastClient, - Signer: signer, - DeliverClient: &mockDeliverClient{}, + BroadcastFactory: mockBroadcastClientFactory, + Signer: signer, + DeliverClient: &mockDeliverClient{}, } cmd := createCmd(mockCF) diff --git a/peer/channel/fetchconfig.go b/peer/channel/fetchconfig.go index 01242221516..28d96b48fa7 100644 --- a/peer/channel/fetchconfig.go +++ b/peer/channel/fetchconfig.go @@ -46,8 +46,6 @@ func fetch(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error { } } - defer cf.BroadcastClient.Close() - var block *cb.Block if block, err = cf.DeliverClient.getBlock(); err != nil { return err diff --git a/peer/channel/fetchconfig_test.go b/peer/channel/fetchconfig_test.go index 5d323ffbf2d..6b154fe3f57 100644 --- a/peer/channel/fetchconfig_test.go +++ b/peer/channel/fetchconfig_test.go @@ -33,12 +33,10 @@ func TestFetchChain(t *testing.T) { t.Fatalf("Get default signer error: %v", err) } - mockBroadcastClient := common.GetMockBroadcastClient(nil) - mockCF := &ChannelCmdFactory{ - BroadcastClient: mockBroadcastClient, - Signer: signer, - DeliverClient: &mockDeliverClient{}, + BroadcastFactory: mockBroadcastClientFactory, + Signer: signer, + DeliverClient: &mockDeliverClient{}, } cmd := createCmd(mockCF) diff --git a/peer/channel/join_test.go b/peer/channel/join_test.go index df8d00dd5fd..20896992e14 100644 --- a/peer/channel/join_test.go +++ b/peer/channel/join_test.go @@ -43,12 +43,10 @@ func TestJoin(t *testing.T) { mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) - mockBroadcastClient := common.GetMockBroadcastClient(nil) - mockCF := &ChannelCmdFactory{ - EndorserClient: mockEndorerClient, - BroadcastClient: mockBroadcastClient, - Signer: signer, + EndorserClient: mockEndorerClient, + BroadcastFactory: mockBroadcastClientFactory, + Signer: signer, } cmd := joinCmd(mockCF) @@ -79,12 +77,10 @@ func TestJoinNonExistentBlock(t *testing.T) { mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) - mockBroadcastClient := common.GetMockBroadcastClient(nil) - mockCF := &ChannelCmdFactory{ - EndorserClient: mockEndorerClient, - BroadcastClient: mockBroadcastClient, - Signer: signer, + EndorserClient: mockEndorerClient, + BroadcastFactory: mockBroadcastClientFactory, + Signer: signer, } cmd := joinCmd(mockCF) @@ -121,12 +117,10 @@ func TestBadProposalResponse(t *testing.T) { mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) - mockBroadcastClient := common.GetMockBroadcastClient(nil) - mockCF := &ChannelCmdFactory{ - EndorserClient: mockEndorerClient, - BroadcastClient: mockBroadcastClient, - Signer: signer, + EndorserClient: mockEndorerClient, + BroadcastFactory: mockBroadcastClientFactory, + Signer: signer, } cmd := joinCmd(mockCF)