Skip to content

Commit

Permalink
Add event listener to chain genesis block commit
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jimthematrix committed Mar 7, 2017
1 parent 932f6e4 commit ad5831f
Showing 1 changed file with 65 additions and 10 deletions.
75 changes: 65 additions & 10 deletions test/integration/e2e/join-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,50 @@ 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');

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();
Expand All @@ -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
Expand All @@ -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);
}
}
}
Expand All @@ -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');
}
Expand Down

0 comments on commit ad5831f

Please sign in to comment.