Skip to content

Commit

Permalink
FABN-1014: Use ChannelEventHub.connect callback
Browse files Browse the repository at this point in the history
In EventHubFactory, use the callback parameter to ChannelEventHub.connect
to wrap it as an async function and await the connect completion, rather
than listen for block events to identify when a connect has completed.

Change-Id: I42f1c562a35b04c38919e2317850085ff39385bc
Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
  • Loading branch information
bestbeforetoday committed Nov 14, 2018
1 parent ea1cdac commit 480fb4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
26 changes: 10 additions & 16 deletions fabric-network/lib/impl/event/eventhubfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

'use strict';

const util = require('util');

const logger = require('fabric-network/lib/logger').getLogger('EventHubFactory');

/**
Expand Down Expand Up @@ -67,22 +69,14 @@ class EventHubFactory {
* @param {ChannelEventHub} eventHub An event hub.
*/
async connectEventHub(eventHub) {
const connectPromise = new Promise((resolve) => {
const regId = eventHub.registerBlockEvent(
() => {
logger.debug('connectEventHub:', 'successfully connected event hub:', eventHub.getName());
eventHub.unregisterBlockEvent(regId);
resolve();
},
() => {
logger.info('connectEventHub:', 'failed to connect event hub:', eventHub.getName());
eventHub.unregisterBlockEvent(regId);
resolve();
}
);
});
eventHub.connect();
await connectPromise;
try {
// Need to wrap in an arrow function to protect the value of this in connect()
const connect = util.promisify((callback) => eventHub.connect({}, callback));
await connect();
logger.debug('connectEventHub:', 'successfully connected event hub:', eventHub.getName());
} catch (error) {
logger.info('connectEventHub:', 'failed to connect event hub:', eventHub.getName(), error);
}
}
}

Expand Down
24 changes: 14 additions & 10 deletions fabric-network/test/impl/event/eventhubfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ describe('EventHubFactory', () => {
stubEventHub2 = sinon.createStubInstance(ChannelEventHub);
stubEventHub2._stubInfo = 'eventHub2';
stubEventHub2.getName.returns('eventHub2');
// Fake a connection success event
stubEventHub2.registerBlockEvent.callsFake((block, error) => { // eslint-disable-line no-unused-vars
Promise.resolve().then(block);
return 2;
});
stubEventHub2.isconnected.returns(false);

// Fake a connection success callback
stubEventHub2.connect.callsFake((fullBlocks, callback) => {
// Invoke callback manually rather than using stub.callsArgWith() to ensure the code will hang rather than
// error if the callback is not passed correctly. An error will be swallowed by the code in EventHubFactory
// and produce a false positive test result.
if (typeof callback === 'function') {
callback(null, stubEventHub2);
}
});
stubChannel = sinon.createStubInstance(Channel);
stubChannel.getName.returns('channel');
stubChannel.getChannelEventHub.withArgs(stubPeer1.getName()).returns(stubEventHub1);
Expand Down Expand Up @@ -108,10 +111,11 @@ describe('EventHubFactory', () => {
});

it('does not fail on error connecting event hub', async () => {
// Fake a connection failure event
stubEventHub2.registerBlockEvent.callsFake((block, error) => {
Promise.resolve().then(error);
return 2;
// Fake a connection failure callback
stubEventHub2.connect.callsFake((fullBlocks, callback) => {
if (typeof callback === 'function') {
callback(new Error('connect failed'));
}
});
const results = await factory.getEventHubs([stubPeer2]);
expect(results[0].connect.called).to.be.true;
Expand Down

0 comments on commit 480fb4c

Please sign in to comment.