Skip to content

Commit

Permalink
FABN-858 improve tests
Browse files Browse the repository at this point in the history
add more checking to verify that notification
occurs when expected

Change-Id: I0f80250e461a3db6d5895c9435d3ec1af0d18b9c
Signed-off-by: Dave Kelsey <[email protected]>
  • Loading branch information
Dave Kelsey committed Sep 6, 2018
1 parent 767598e commit 399bd25
Showing 1 changed file with 149 additions and 4 deletions.
153 changes: 149 additions & 4 deletions test/integration/network-e2e/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ async function createContract(t, gateway, gatewayOptions) {
return contract;
}

const getEventHubForOrg = async (gateway, orgMSP) => {
// bit horrible until we provide a proper api to get the underlying event hubs
const network = await gateway.getNetwork(channelName);
const orgpeer = network.getPeerMap().get(orgMSP)[0];
return network.getChannel().getChannelEventHub(orgpeer.getName());
};

test('\n\n***** Network End-to-end flow: import identity into wallet *****\n\n', async (t) => {
await inMemoryIdentitySetup();
const exists = await inMemoryWallet.exists('[email protected]');
Expand All @@ -65,6 +72,7 @@ test('\n\n***** Network End-to-end flow: import identity into wallet *****\n\n',

test('\n\n***** Network End-to-end flow: invoke transaction to move money using in memory wallet and default event strategy *****\n\n', async (t) => {
const gateway = new Gateway();
let org1EventHub;

try {
await inMemoryIdentitySetup();
Expand All @@ -77,8 +85,28 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
clientTlsIdentity: 'tlsId'
});

// Obtain an event hub that that will be used by the underlying implementation
org1EventHub = await getEventHubForOrg(gateway, 'Org1MSP');
const org2EventHub = await getEventHubForOrg(gateway, 'Org2MSP');

// eventFired set to -1 to ignore the connected event returned because
// connection takes place during submitTransaction and not upfront
// once upfront connection is provided, set this value to 0
let eventFired = -1;

// have to register for all transaction events (a new feature in 1.3) as
// there is no way to know what the initial transaction id is
org1EventHub.registerTxEvent('all', (txId, code) => {
if (code === 'VALID') {
eventFired++;
}
}, () => {});

const response = await contract.submitTransaction('move', 'a', 'b','100');

t.false(org2EventHub.isconnected(), 'org2 event hub correctly not connected');
t.equal(eventFired, 1, 'single event for org1 correctly unblocked submitTransaction');

const expectedResult = 'move succeed';
if(response.toString() === expectedResult){
t.pass('Successfully invoked transaction chaincode on channel');
Expand All @@ -89,6 +117,7 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
} catch(err) {
t.fail('Failed to invoke transaction chaincode on channel. ' + err.stack ? err.stack : err);
} finally {
org1EventHub.disconnect(); // remove when gateway.disconnect implements eventhub cleanup
gateway.disconnect();
}

Expand All @@ -98,6 +127,7 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using

test('\n\n***** Network End-to-end flow: invoke transaction to move money using in memory wallet and MSPID_SCOPE_ALLFORTX event strategy *****\n\n', async (t) => {
const gateway = new Gateway();
let org1EventHub;

try {
await inMemoryIdentitySetup();
Expand All @@ -107,11 +137,32 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
wallet: inMemoryWallet,
identity: '[email protected]',
clientTlsIdentity: 'tlsId',
eventStrategy: DefaultEventHandlerStrategies.MSPID_SCOPE_ALLFORTX
eventHandlerOptions: {
strategy: DefaultEventHandlerStrategies.MSPID_SCOPE_ALLFORTX
}
});

// Obtain an event hub that that will be used by the underlying implementation
org1EventHub = await getEventHubForOrg(gateway, 'Org1MSP');
const org2EventHub = await getEventHubForOrg(gateway, 'Org2MSP');

// eventFired set to -1 to ignore the connected event returned because
// connection takes place during submitTransaction and not upfront
// once upfront connection is provided, set this value to 0
let eventFired = -1;

// have to register for all transaction events (a new feature in 1.3) as
// there is no way to know what the initial transaction id is
org1EventHub.registerTxEvent('all', (txId, code) => {
if (code === 'VALID') {
eventFired++;
}
}, () => {});

const response = await contract.submitTransaction('move', 'a', 'b','100');

t.false(org2EventHub.isconnected(), 'org2 event hub correctly not connected');
t.equal(eventFired, 1, 'single event for org1 correctly unblocked submitTransaction');
const expectedResult = 'move succeed';
if(response.toString() === expectedResult){
t.pass('Successfully invoked transaction chaincode on channel');
Expand All @@ -122,6 +173,7 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
} catch(err) {
t.fail('Failed to invoke transaction chaincode on channel. ' + err.stack ? err.stack : err);
} finally {
org1EventHub.disconnect(); // remove when gateway.disconnect implements eventhub cleanup
gateway.disconnect();
}

Expand All @@ -130,6 +182,7 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using

test('\n\n***** Network End-to-end flow: invoke transaction to move money using in memory wallet and MSPID_SCOPE_ANYFORTX event strategy *****\n\n', async (t) => {
const gateway = new Gateway();
let org1EventHub;
try {
await inMemoryIdentitySetup();
await tlsSetup();
Expand All @@ -138,11 +191,33 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
wallet: inMemoryWallet,
identity: '[email protected]',
clientTlsIdentity: 'tlsId',
eventStrategy: DefaultEventHandlerStrategies.MSPID_SCOPE_ANYFORTX
eventHandlerOptions: {
strategy: DefaultEventHandlerStrategies.MSPID_SCOPE_ANYFORTX
}
});

// Obtain an event hub that that will be used by the underlying implementation
org1EventHub = await getEventHubForOrg(gateway, 'Org1MSP');
const org2EventHub = await getEventHubForOrg(gateway, 'Org2MSP');


// eventFired set to -1 to ignore the connected event returned because
// connection takes place during submitTransaction and not upfront
// once upfront connection is provided, set this value to 0
let eventFired = -1;

// have to register for all transaction events (a new feature in 1.3) as
// there is no way to know what the initial transaction id is
org1EventHub.registerTxEvent('all', (txId, code) => {
if (code === 'VALID') {
eventFired++;
}
}, () => {});

const response = await contract.submitTransaction('move', 'a', 'b','100');

t.false(org2EventHub.isconnected(), 'org2 event hub correctly not connected');
t.equal(eventFired, 1, 'single event for org1 correctly unblocked submitTransaction');
const expectedResult = 'move succeed';
if(response.toString() === expectedResult){
t.pass('Successfully invoked transaction chaincode on channel');
Expand All @@ -153,6 +228,7 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
} catch(err) {
t.fail('Failed to invoke transaction chaincode on channel. ' + err.stack ? err.stack : err);
} finally {
org1EventHub.disconnect(); // because gateway.disconnect doesn't do it yet
gateway.disconnect();
}

Expand All @@ -161,6 +237,8 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using

test('\n\n***** Network End-to-end flow: invoke transaction to move money using in memory wallet and NETWORK_SCOPE_ALLFORTX event strategy *****\n\n', async (t) => {
const gateway = new Gateway();
let org1EventHub;
let org2EventHub;

try {
await inMemoryIdentitySetup();
Expand All @@ -170,11 +248,38 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
wallet: inMemoryWallet,
identity: '[email protected]',
clientTlsIdentity: 'tlsId',
eventStrategy: DefaultEventHandlerStrategies.NETWORK_SCOPE_ALLFORTX
eventHandlerOptions: {
strategy: DefaultEventHandlerStrategies.NETWORK_SCOPE_ALLFORTX
}
});

// Obtain the event hubs that that will be used by the underlying implementation
org1EventHub = await getEventHubForOrg(gateway, 'Org1MSP');
org2EventHub = await getEventHubForOrg(gateway, 'Org2MSP');

// eventFired set to -1 to ignore the connected event returned because
// connection takes place during submitTransaction and not upfront
// once upfront connection is provided, set this value to 0
let org1EventFired = -1;
let org2EventFired = -1;
org1EventHub.registerTxEvent('all', (txId, code) => {
if (code === 'VALID') {
org1EventFired++;
}
}, () => {});

org2EventHub.registerTxEvent('all', (txId, code) => {
if (code === 'VALID') {
org2EventFired++;
}
}, () => {});

const response = await contract.submitTransaction('move', 'a', 'b','100');

const unblockCorrectly = (org1EventFired === 1) && (org2EventFired === 1);
t.pass(`org1 events: ${org1EventFired}, org2 events: ${org2EventFired}`);
t.true(unblockCorrectly, 'got single events at both org event hubs before submitTransaction was unblocked');

const expectedResult = 'move succeed';
if(response.toString() === expectedResult){
t.pass('Successfully invoked transaction chaincode on channel');
Expand All @@ -185,6 +290,9 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
} catch(err) {
t.fail('Failed to invoke transaction chaincode on channel. ' + err.stack ? err.stack : err);
} finally {
// remove the disconnects once gateway disconnect cleans up event hubs
org1EventHub.disconnect();
org2EventHub.disconnect();
gateway.disconnect();
}

Expand All @@ -193,6 +301,8 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using

test('\n\n***** Network End-to-end flow: invoke transaction to move money using in memory wallet and NETWORK_SCOPE_ANYFORTX event strategy *****\n\n', async (t) => {
const gateway = new Gateway();
let org1EventHub;
let org2EventHub;

try {
await inMemoryIdentitySetup();
Expand All @@ -202,11 +312,43 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
wallet: inMemoryWallet,
identity: '[email protected]',
clientTlsIdentity: 'tlsId',
eventStrategy: DefaultEventHandlerStrategies.NETWORK_SCOPE_ANYFORTX
eventHandlerOptions: {
strategy: DefaultEventHandlerStrategies.NETWORK_SCOPE_ANYFORTX
}
});

// Obtain the event hubs that that will be used by the underlying implementation
org1EventHub = await getEventHubForOrg(gateway, 'Org1MSP');
org2EventHub = await getEventHubForOrg(gateway, 'Org2MSP');

// eventFired set to -1 to ignore the connected event returned because
// connection takes place during submitTransaction and not upfront
// once upfront connection is provided, set this value to 0
let org1EventFired = -1;
let org2EventFired = -1;

org1EventHub.registerTxEvent('all', (txId, code) => {
if (code === 'VALID') {
org1EventFired++;
}
}, () => {});

org2EventHub.registerTxEvent('all', (txId, code) => {
if (code === 'VALID') {
org2EventFired++;
}
}, () => {});

const response = await contract.submitTransaction('move', 'a', 'b','100');

const unblockCorrectly = (org1EventFired === 1 && org2EventFired === 0)
|| (org1EventFired === 0 && org2EventFired === 1)
// || (org1EventFired === 1 && org2EventFired === 1) hopefully this doesn't have to be included due to timing
;

t.pass(`org1 events: ${org1EventFired}, org2 events: ${org2EventFired}`);
t.true(unblockCorrectly, 'single event received by one of the event hubs caused submitTransaction to unblock, before other event received');

const expectedResult = 'move succeed';
if(response.toString() === expectedResult){
t.pass('Successfully invoked transaction chaincode on channel');
Expand All @@ -217,6 +359,9 @@ test('\n\n***** Network End-to-end flow: invoke transaction to move money using
} catch(err) {
t.fail('Failed to invoke transaction chaincode on channel. ' + err.stack ? err.stack : err);
} finally {
// remove the disconnects once gateway disconnect cleans up event hubs
org1EventHub.disconnect();
org2EventHub.disconnect();
gateway.disconnect();
}

Expand Down

0 comments on commit 399bd25

Please sign in to comment.