From de7bf5f85c07a4c4bb92c345eb6c2fa48bc1b6ce Mon Sep 17 00:00:00 2001 From: Sudesh Shetty Date: Wed, 29 Aug 2018 14:00:41 -0400 Subject: [PATCH] [FABG-748] fix for intermittent SeekTypes test failure Change-Id: Id496f84510cbaef1d0d2ef5dffe21441824ae83d Signed-off-by: Sudesh Shetty --- .../pkg/client/channel/channel_client_test.go | 4 +-- test/integration/pkg/fab/eventclient_test.go | 36 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/test/integration/pkg/client/channel/channel_client_test.go b/test/integration/pkg/client/channel/channel_client_test.go index 4d936db4ca..48ce19f983 100644 --- a/test/integration/pkg/client/channel/channel_client_test.go +++ b/test/integration/pkg/client/channel/channel_client_test.go @@ -93,7 +93,7 @@ func TestChannelClient(t *testing.T) { testQuery(t, chClient, "202", chaincodeID, bKey) // Test register and receive chaincode event - testChaincodeEvent(chaincodeID, chClient, t, moveOneTx) + testChaincodeEvent(t, chClient, moveOneTx, chaincodeID) // Verify transaction with chain code event completed testQuery(t, chClient, "203", chaincodeID, bKey) @@ -440,7 +440,7 @@ func testInvokeHandler(t *testing.T, chClient *channel.Client, ccID string, args } } -func testChaincodeEvent(ccID string, chClient *channel.Client, t *testing.T, args [][]byte) { +func testChaincodeEvent(t *testing.T, chClient *channel.Client, args [][]byte, ccID string) { eventID := "test([a-zA-Z]+)" diff --git a/test/integration/pkg/fab/eventclient_test.go b/test/integration/pkg/fab/eventclient_test.go index 1ea8a50693..74515fceea 100644 --- a/test/integration/pkg/fab/eventclient_test.go +++ b/test/integration/pkg/fab/eventclient_test.go @@ -21,7 +21,6 @@ import ( "github.com/pkg/errors" "github.com/stretchr/testify/require" - "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context" "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/deliverclient/seek" "github.com/hyperledger/fabric-sdk-go/test/integration" pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer" @@ -272,18 +271,27 @@ func TestMultipleEventsBySeekTypes(t *testing.T) { testSetup := mainTestSetup //Run with seek type default and test behaviour - + //If seek type is default, then event dispatcher uses first block only for block height calculations, it doesn't publish anything + //to event channel, and first event we get from event channel actually belongs to first transaction after registration. + var txIDMatched bool for i := 0; i < 4; i++ { - testSeekTypeDefault(t, testSetup, chaincodeID) + txIDMatched = testSeekTypeDefault(t, testSetup, chaincodeID) + //In case of seektype DEFAULT, txID from event always match with transaction happened after event registration + require.True(t, txIDMatched, "TxID from one of the event didn't match with test transaction TxID") } //Run with seek type newest and test behaviour + //If seek type is newest then the first event we get from event channel is not related to the first transaction happened after registration, it is + //actually latest block from the chain. So TxID from event will not always match with TxID from test transaction + txIDMatched = true for i := 0; i < 4; i++ { - testSeekTypeNewest(t, testSetup, chaincodeID) + txIDMatched = txIDMatched && testSeekTypeNewest(t, testSetup, chaincodeID) } + //In case of seektype NEWEST, txID from event will not always match with transaction happened after event registration + require.False(t, txIDMatched, "TxID from each event matched with TxID of transaction after each registration, which isn't conventional seektype NEWEST behavior") } -func testSeekTypeDefault(t *testing.T, testSetup *integration.BaseSetupImpl, chaincodeID string) { +func testSeekTypeDefault(t *testing.T, testSetup *integration.BaseSetupImpl, chaincodeID string) bool { //create new sdk sdk, err := fabsdk.New(integration.ConfigBackend) require.NoError(t, err, "failed to get new sdk instance") @@ -298,10 +306,10 @@ func testSeekTypeDefault(t *testing.T, testSetup *integration.BaseSetupImpl, cha eventService, err := chContext.ChannelService().EventService() require.NoError(t, err, "error getting event service") - testChannelEventsSeekOptions(t, testSetup, sdk, chContext, chaincodeID, false, eventService, "") + return testChannelEventsSeekOptions(t, testSetup, sdk, chaincodeID, false, eventService, "") } -func testSeekTypeNewest(t *testing.T, testSetup *integration.BaseSetupImpl, chaincodeID string) { +func testSeekTypeNewest(t *testing.T, testSetup *integration.BaseSetupImpl, chaincodeID string) bool { //create new sdk sdk, err := fabsdk.New(integration.ConfigBackend) require.NoError(t, err, "failed to get new sdk instance") @@ -316,10 +324,10 @@ func testSeekTypeNewest(t *testing.T, testSetup *integration.BaseSetupImpl, chai eventService, err := chContext.ChannelService().EventService(deliverclient.WithSeekType(seek.Newest)) require.NoError(t, err, "error getting event service") - testChannelEventsSeekOptions(t, testSetup, sdk, chContext, chaincodeID, false, eventService, seek.Newest) + return testChannelEventsSeekOptions(t, testSetup, sdk, chaincodeID, false, eventService, seek.Newest) } -func testChannelEventsSeekOptions(t *testing.T, testSetup *integration.BaseSetupImpl, sdk *fabsdk.FabricSDK, chContext context.Channel, chainCodeID string, blockEvents bool, eventService fab.EventService, seekType seek.Type) { +func testChannelEventsSeekOptions(t *testing.T, testSetup *integration.BaseSetupImpl, sdk *fabsdk.FabricSDK, chainCodeID string, blockEvents bool, eventService fab.EventService, seekType seek.Type) bool { //get transactor _, cancel, transactor, err := getTransactor(sdk, testSetup.ChannelID, "Admin", testSetup.OrgID) @@ -359,16 +367,10 @@ func testChannelEventsSeekOptions(t *testing.T, testSetup *integration.BaseSetup } case <-time.After(eventTimeWindow): t.Fatal("Timeout waiting for event") - return + return false } - //If seek type is newest then the first event we get from event channel is not related to the first transaction happened after registration, it is - //actually latest block from the chain - require.Equal(t, seekType == seek.Newest, txID != event.TxID, "for seek type[%s], txID [%s], event.txID[%s] ,condition didn't match", seekType, txID, event.TxID) - - //If seek type is default, then event dispatcher uses first block only for block height calculations, it doesn't publish anything - //to event channel, and first event we get from event channel actually belongs to first transaction after registration. - require.Equal(t, seekType == "", txID == event.TxID, "for seek type[%s], txID [%s], event.txID[%s] ,condition didn't match", seekType, txID, event.TxID) + return txID == event.TxID } //TestEventClientWithMVCCReadConflicts tests behavior of chaincode events when MVCC_READ_CONFLICT happens