From ad5831f08a0cded862a3ba43b4925ec97c0b11ab Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Tue, 7 Mar 2017 16:32:43 -0500 Subject: [PATCH] Add event listener to chain genesis block commit FAB-2617 This is the last remaining gap in the multi-org end to end test. Need to listen on the chain's genesis block creation after calling joinChannel() to ensure the channel is fully ready to be used, before continuing on to try to call instantiate() etc. Change-Id: I76c737e69d4b5b3effccd7a0dd45d15579591066 Signed-off-by: Jim Zhang --- test/integration/e2e/join-channel.js | 75 ++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 10 deletions(-) diff --git a/test/integration/e2e/join-channel.js b/test/integration/e2e/join-channel.js index 511ecf5cfd..94360254d7 100644 --- a/test/integration/e2e/join-channel.js +++ b/test/integration/e2e/join-channel.js @@ -20,11 +20,13 @@ var test = _test(tape); var util = require('util'); var path = require('path'); +var grpc = require('grpc'); var hfc = require('fabric-client'); var utils = require('fabric-client/lib/utils.js'); var Peer = require('fabric-client/lib/Peer.js'); var Orderer = require('fabric-client/lib/Orderer.js'); +var EventHub = require('fabric-client/lib/EventHub.js'); var testUtil = require('../../unit/util.js'); @@ -32,19 +34,36 @@ var the_user = null; var tx_id = null; var nonce = null; -var logger = utils.getLogger('join-channel'); - hfc.addConfigFile(path.join(__dirname, './config.json')); var ORGS = hfc.getConfigSetting('test-network'); +var allEventhubs = []; + +var _commonProto = grpc.load(path.join(__dirname, '../../../fabric-client/lib/protos/common/common.proto')).common; + // //Attempt to send a request to the orderer with the sendCreateChain method // test('\n\n***** End-to-end flow: join channel *****\n\n', function(t) { + // override t.end function so it'll always disconnect the event hub + t.end = ((context, ehs, f) => { + return function() { + for(var key in ehs) { + var eventhub = ehs[key]; + if (eventhub && eventhub.isconnected()) { + t.comment('Disconnecting the event hub'); + eventhub.disconnect(); + } + } + + f.apply(context, arguments); + }; + })(t, allEventhubs, t.end); + joinChannel('org1', t) .then(() => { t.pass(util.format('Successfully joined peers in organization "%s" to the channel', ORGS['org1'].name)); - joinChannel('org2', t); + return joinChannel('org2', t); }, (err) => { t.fail(util.format('Failed to join peers in organization "%s" to the channel', ORGS['org1'].name)); t.end(); @@ -63,7 +82,7 @@ test('\n\n***** End-to-end flow: join channel *****\n\n', function(t) { }); function joinChannel(org, t) { - t.comment(util.format('Calling peers in organization "%s" to join the channel')); + t.comment(util.format('Calling peers in organization "%s" to join the channel', org)); // // Create and configure the test chain @@ -74,11 +93,18 @@ function joinChannel(org, t) { var orgName = ORGS[org].name; - var targets = []; + var targets = [], + eventhubs = []; for (let key in ORGS[org]) { if (ORGS[org].hasOwnProperty(key)) { if (key.indexOf('peer') === 0) { targets.push(new Peer(ORGS[org][key].requests)); + + let eh = new EventHub(); + eh.setPeerAddr(ORGS[org][key].events); + eh.connect(); + eventhubs.push(eh); + allEventhubs.push(eh); } } } @@ -104,17 +130,46 @@ function joinChannel(org, t) { txId : tx_id, nonce : nonce }; - return chain.joinChannel(request); + + var eventPromises = []; + eventhubs.forEach((eh) => { + let txPromise = new Promise((resolve, reject) => { + let handle = setTimeout(reject, 30000); + + eh.registerBlockEvent((block) => { + clearTimeout(handle); + + // in real-world situations, a peer may have more than one channels so + // we must check that this block came from the channel we asked the peer to join + if(block.data.data.length === 1) { + // Config block must only contain one transaction + var envelope = _commonProto.Envelope.decode(block.data.data[0]); + var payload = _commonProto.Payload.decode(envelope.payload); + var channel_header = _commonProto.ChannelHeader.decode(payload.header.channel_header); + + if (channel_header.channel_id === testUtil.END2END.channel) { + t.pass('The new channel has been successfully joined on peer '+ eh.ep.addr); + resolve(); + } + } + }); + }); + + eventPromises.push(txPromise); + }); + + sendPromise = chain.joinChannel(request); + return Promise.all([sendPromise].concat(eventPromises)); }, (err) => { t.fail('Failed to enroll user \'admin\' due to error: ' + err.stack ? err.stack : err); throw new Error('Failed to enroll user \'admin\' due to error: ' + err.stack ? err.stack : err); }) .then((results) => { - logger.info(util.format('Join Channel R E S P O N S E : %j', results)); + t.comment(util.format('Join Channel R E S P O N S E : %j', results)); - if(results[0] && results[0].response && results[0].response.status == 200) - t.pass('Successfully joined channel.'); - else { + if(results[0] && results[0][0] && results[0][0].response && results[0][0].response.status == 200) { + t.pass(util.format('Successfully joined peers in organization %s to join the channel', orgName)); + } else { t.fail(' Failed to join channel'); throw new Error('Failed to join channel'); }