Skip to content

Commit

Permalink
[FABG-731] Some int tests missing sdk.Close call
Browse files Browse the repository at this point in the history
This change fixes integration tests to properly call
sdk.Close after completion.

Change-Id: Ifc40fe6942fcf79ffe77d665c2fc5a071454bd16
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Aug 22, 2018
1 parent e9474b4 commit c8ac55a
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 97 deletions.
7 changes: 2 additions & 5 deletions pkg/fabsdk/fabsdk.go
Expand Up @@ -279,22 +279,19 @@ func initSDK(sdk *FabricSDK, configProvider core.ConfigProvider, opts []Option)
}
}

logger.Debug("SDK initialized successfully")
return nil
}

// Close frees up caches and connections being maintained by the SDK
func (sdk *FabricSDK) Close() {
logger.Debug("Closing SDK... checking if local discovery provider is closable...")
logger.Debug("SDK closing")
if pvdr, ok := sdk.provider.LocalDiscoveryProvider().(closeable); ok {
logger.Debug("... closing local discovery provider")
pvdr.Close()
}
logger.Debug("... checking if channel provider is closable...")
if pvdr, ok := sdk.provider.ChannelProvider().(closeable); ok {
logger.Debug("... closing channel provider")
pvdr.Close()
}
logger.Debug("... closing infra provider")
sdk.provider.InfraProvider().Close()
}

Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/config/config_test_endpoints.yaml
Expand Up @@ -38,8 +38,6 @@ client:
# to prevent re-selecting them in subsequent retries.
# This interval will define how long a peer is greylisted
greylistExpiry: 5s
timeout:
connection: 3s
registrationResponse: 10s
orderer:
timeout:
Expand Down
1 change: 1 addition & 0 deletions test/integration/e2e/orgs/multi_orgs_singleorgconfig.go
Expand Up @@ -40,6 +40,7 @@ func TestMultiOrgWithSingleOrgConfig(t *testing.T, examplecc string) {
if err != nil {
t.Fatal("failed to created SDK,", err)
}
defer org1sdk.Close()

//prepare context
org1ChannelClientContext := org1sdk.ChannelContext("orgchannel", fabsdk.WithUser("User1"), fabsdk.WithOrg("Org1"))
Expand Down
16 changes: 7 additions & 9 deletions test/integration/e2e/orgs/multiple_orgs_test.go
Expand Up @@ -9,7 +9,6 @@ package orgs
import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -78,13 +77,12 @@ type multiorgContext struct {

func TestMain(m *testing.M) {
err := setup()
defer teardown()
var r int
if err == nil {
r = m.Run()
if err != nil {
panic(fmt.Sprintf("unable to setup [%s]", err))
}
defer os.Exit(r)
runtime.Goexit()
r := m.Run()
teardown()
os.Exit(r)
}

func setup() error {
Expand All @@ -97,12 +95,12 @@ func setup() error {

org1MspClient, err = mspclient.New(sdk.Context(), mspclient.WithOrg(org1))
if err != nil {
return errors.Wrap(err, "failed to create org1MspClient, err")
return errors.Wrap(err, "failed to create org1MspClient")
}

org2MspClient, err = mspclient.New(sdk.Context(), mspclient.WithOrg(org2))
if err != nil {
return errors.Wrap(err, "failed to create org2MspClient, err")
return errors.Wrap(err, "failed to create org2MspClient")
}

return nil
Expand Down
105 changes: 55 additions & 50 deletions test/integration/pkg/client/channel/channel_client_test.go
Expand Up @@ -43,7 +43,7 @@ func TestChannelClient(t *testing.T) {
}

// Synchronous query
testQuery("200", chaincodeID, chClient, t)
testQuery(t, chClient, "200", chaincodeID)

transientData := "Some data"
transientDataMap := make(map[string][]byte)
Expand All @@ -67,54 +67,54 @@ func TestChannelClient(t *testing.T) {
}

// Verify transaction using query
testQuery("201", chaincodeID, chClient, t)
testQuery(t, chClient, "201", chaincodeID)

// transaction
nestedCCID := integration.GenerateExampleID(true)
err = integration.PrepareExampleCC(sdk, fabsdk.WithUser("Admin"), testSetup.OrgID, nestedCCID)
require.Nil(t, err, "InstallAndInstantiateExampleCC return error")
testTransaction(chaincodeID, nestedCCID, chClient, t)
testTransaction(t, chClient, chaincodeID, nestedCCID)

// Verify transaction
testQuery("202", chaincodeID, chClient, t)
testQuery(t, chClient, "202", chaincodeID)

// Verify that filter error and commit error did not modify value
testQuery("202", chaincodeID, chClient, t)
testQuery(t, chClient, "202", chaincodeID)

// Test register and receive chaincode event
testChaincodeEvent(chaincodeID, chClient, t)

// Verify transaction with chain code event completed
testQuery("203", chaincodeID, chClient, t)
testQuery(t, chClient, "203", chaincodeID)

// Test invocation of custom handler
testInvokeHandler(chaincodeID, chClient, t)
testInvokeHandler(t, chClient, chaincodeID)

// Test chaincode error
testChaincodeError(chaincodeID, chClient, t)
testChaincodeError(t, chClient, chaincodeID)

// Test receive event using separate client
listener, err := channel.New(org1ChannelClientContext)
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}

testChaincodeEventListener(chaincodeID, chClient, listener, t)
testChaincodeEventListener(t, chaincodeID, chClient, listener)

testDuplicateTargets(chaincodeID, chClient, t)
testDuplicateTargets(t, chaincodeID, chClient)

//test if CCEvents for chaincode events are in sync when new channel client are created
// for each transaction
testMultipleClientChaincodeEvent(chaincodeID, t)
testMultipleClientChaincodeEventLoop(t, chaincodeID)
}

func testDuplicateTargets(chaincodeID string, chClient *channel.Client, t *testing.T) {
func testDuplicateTargets(t *testing.T, chaincodeID string, chClient *channel.Client) {

// Using shared SDK instance to increase test speed.
sdk := mainSDK

// Synchronous query
testQuery("205", chaincodeID, chClient, t)
testQuery(t, chClient, "205", chaincodeID)

transientData := "Some data"
transientDataMap := make(map[string][]byte)
Expand Down Expand Up @@ -152,7 +152,7 @@ func testDuplicateTargets(chaincodeID string, chClient *channel.Client, t *testi
}

// Verify transaction using query
testQuery("206", chaincodeID, chClient, t)
testQuery(t, chClient, "206", chaincodeID)
}

// TestCCToCC tests one chaincode invoking another chaincode. The first chaincode
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestCCToCC(t *testing.T) {
})
}

func testQuery(expected string, ccID string, chClient *channel.Client, t *testing.T) {
func testQuery(t *testing.T, chClient *channel.Client, expected string, ccID string) {
const (
maxRetries = 10
retrySleep = 500 * time.Millisecond
Expand All @@ -274,7 +274,7 @@ func testQuery(expected string, ccID string, chClient *channel.Client, t *testin
t.Fatal("Exceeded max retries")
}

func testTransaction(ccID, nestedCCID string, chClient *channel.Client, t *testing.T) {
func testTransaction(t *testing.T, chClient *channel.Client, ccID, nestedCCID string) {
response, err := chClient.Execute(
channel.Request{
ChaincodeID: ccID,
Expand Down Expand Up @@ -315,7 +315,7 @@ func (h *testHandler) Handle(requestContext *invoke.RequestContext, clientContex
}
}

func testInvokeHandler(ccID string, chClient *channel.Client, t *testing.T) {
func testInvokeHandler(t *testing.T, chClient *channel.Client, ccID string) {
// Insert a custom handler before and after the commit.
// Ensure that the handlers are being called by writing out some data
// and comparing with response.
Expand Down Expand Up @@ -396,53 +396,57 @@ func testChaincodeEvent(ccID string, chClient *channel.Client, t *testing.T) {

//TestMultipleEventClient tests if CCEvents for chaincode events are in sync when new channel client are created
// for each transaction
func testMultipleClientChaincodeEvent(chainCodeID string, t *testing.T) {
func testMultipleClientChaincodeEventLoop(t *testing.T, chainCodeID string) {

channelID := mainTestSetup.ChannelID
eventID := "([a-zA-Z]+)"

for i := 0; i < 10; i++ {
testMultipleClientChaincodeEvent(t, channelID, chainCodeID, eventID)
}
}

sdk, err := fabsdk.New(integration.ConfigBackend)
if err != nil {
t.Fatalf("Failed to create new SDK: %s", err)
}
func testMultipleClientChaincodeEvent(t *testing.T, channelID string, chainCodeID string, eventID string) {
sdk, err := fabsdk.New(integration.ConfigBackend)
if err != nil {
t.Fatalf("Failed to create new SDK: %s", err)
}
defer sdk.Close()

chContextProvider := sdk.ChannelContext(channelID, fabsdk.WithUser(org1User), fabsdk.WithOrg(org1Name))
chContextProvider := sdk.ChannelContext(channelID, fabsdk.WithUser(org1User), fabsdk.WithOrg(org1Name))

chClient, err := channel.New(chContextProvider)
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}
chClient, err := channel.New(chContextProvider)
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}

// Register chaincode event (pass in channel which receives event details when the event is complete)
reg, notifier, err := chClient.RegisterChaincodeEvent(chainCodeID, eventID)
if err != nil {
t.Fatalf("Failed to register cc event: %s", err)
}
defer chClient.UnregisterChaincodeEvent(reg)
// Register chaincode event (pass in channel which receives event details when the event is complete)
reg, notifier, err := chClient.RegisterChaincodeEvent(chainCodeID, eventID)
if err != nil {
t.Fatalf("Failed to register cc event: %s", err)
}
defer chClient.UnregisterChaincodeEvent(reg)

// Move funds
resp, err := chClient.Execute(channel.Request{ChaincodeID: chainCodeID, Fcn: "invoke",
Args: integration.ExampleCCTxArgs()}, channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}
// Move funds
resp, err := chClient.Execute(channel.Request{ChaincodeID: chainCodeID, Fcn: "invoke",
Args: integration.ExampleCCTxArgs()}, channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}

txID := resp.TransactionID
txID := resp.TransactionID

var ccEvent *fab.CCEvent
select {
case ccEvent = <-notifier:
t.Logf("Received CC eventID: %#v\n", ccEvent.TxID)
case <-time.After(time.Second * 20):
t.Fatalf("Did NOT receive CC event for eventId(%s)\n", eventID)
}
assert.Equal(t, string(txID), ccEvent.TxID, "mismatched ccEvent.TxID")
var ccEvent *fab.CCEvent
select {
case ccEvent = <-notifier:
t.Logf("Received CC eventID: %#v\n", ccEvent.TxID)
case <-time.After(time.Second * 20):
t.Fatalf("Did NOT receive CC event for eventId(%s)\n", eventID)
}
assert.Equal(t, string(txID), ccEvent.TxID, "mismatched ccEvent.TxID")
}

func testChaincodeEventListener(ccID string, chClient *channel.Client, listener *channel.Client, t *testing.T) {
func testChaincodeEventListener(t *testing.T, ccID string, chClient *channel.Client, listener *channel.Client) {

eventID := integration.GenerateRandomID()

Expand Down Expand Up @@ -471,7 +475,7 @@ func testChaincodeEventListener(ccID string, chClient *channel.Client, listener

}

func testChaincodeError(ccID string, client *channel.Client, t *testing.T) {
func testChaincodeError(t *testing.T, client *channel.Client, ccID string) {
// Try calling unknown function call and expect an error
r, err := client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "DUMMY_FUNCTION", Args: integration.ExampleCCTxArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
Expand Down Expand Up @@ -518,6 +522,7 @@ func TestNoEndpoints(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create new SDK: %s", err)
}
defer sdk.Close()

// Prepare channel context
org1AdminChannelContext := sdk.ChannelContext(testSetup.ChannelID, fabsdk.WithUser(org1AdminUser), fabsdk.WithOrg(org1Name))
Expand Down
1 change: 1 addition & 0 deletions test/integration/pkg/client/ledger/ledger_queries_test.go
Expand Up @@ -124,6 +124,7 @@ func TestNoLedgerEndpoints(t *testing.T) {
if err != nil {
panic(fmt.Sprintf("Failed to create new SDK: %s", err))
}
defer sdk.Close()

//prepare contexts
org1AdminChannelContext := sdk.ChannelContext(testSetup.ChannelID, fabsdk.WithUser(org1AdminUser), fabsdk.WithOrg(org1Name))
Expand Down
2 changes: 1 addition & 1 deletion test/integration/pkg/client/msp/enrollment_test.go
Expand Up @@ -25,10 +25,10 @@ func TestRegisterEnroll(t *testing.T) {

// Instantiate the SDK
sdk, err := fabsdk.New(integration.ConfigBackend)

if err != nil {
t.Fatalf("SDK init failed: %s", err)
}
defer sdk.Close()

// Delete all private keys from the crypto suite store
// and users from the user store at the end
Expand Down
2 changes: 1 addition & 1 deletion test/integration/pkg/client/msp/identity_test.go
Expand Up @@ -237,10 +237,10 @@ func setupClient(t *testing.T) (*msp.Client, *fabsdk.FabricSDK) {

// Instantiate the SDK
sdk, err := fabsdk.New(integration.ConfigBackend)

if err != nil {
t.Fatalf("SDK init failed: %s", err)
}
defer sdk.Close()

// Delete all private keys from the crypto suite store
// and users from the user store at the end
Expand Down
1 change: 1 addition & 0 deletions test/integration/pkg/client/msp/user_data_mgmt_test.go
Expand Up @@ -89,6 +89,7 @@ func TestWithCustomStores(t *testing.T) {
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}
defer sdk.Close()

ctxProvider := sdk.Context()

Expand Down

0 comments on commit c8ac55a

Please sign in to comment.