Skip to content

Commit

Permalink
[FAB-6062] NodeSDK - add close method
Browse files Browse the repository at this point in the history
The Peer, Orderer, and EventHub object will have
a close method that will close the connection if
required by the application.

Change-Id: I004efe1d3fc0fc6ca6ae6f69e2a310e8aa776d5d
Signed-off-by: Bret Harrison <[email protected]>
  • Loading branch information
harrisob authored and jimthematrix committed Nov 7, 2017
1 parent 47f63a8 commit 6214f05
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 3 deletions.
12 changes: 12 additions & 0 deletions fabric-client/lib/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ var Channel = class {
!this._devMode);
}

/**
* Close the service connection off all assigned peers and orderers
*/
close() {
logger.info('close - closing connections');
var closer = function (ep) {
ep.close();
}
this._peers.map(closer);
this._orderers.map(closer);
}

/**
* Initializes the channel object with the Membership Service Providers (MSPs). The channel's
* MSPs are critical in providing applications the ability to validate certificates and verify
Expand Down
3 changes: 3 additions & 0 deletions fabric-client/lib/EventHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ var EventHub = class {
this._stream.end();
this._stream = null;
}
if(this._event_client) {
this._event_client.close();
}
}

/*
Expand Down
10 changes: 10 additions & 0 deletions fabric-client/lib/Orderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ var Orderer = class extends Remote {
this._ordererClient = new _abProto.AtomicBroadcast(this._endpoint.addr, this._endpoint.creds, this._options);
}

/**
* Close the service connection.
*/
close() {
if(this._ordererClient) {
logger.info('close - closing orderer connection ' + this._endpoint.addr);
this._ordererClient.close();
}
}

/**
* @typedef {Object} BroadcastResponse
* @property {string} status - Value is 'SUCCESS' or a descriptive error string
Expand Down
10 changes: 10 additions & 0 deletions fabric-client/lib/Peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ var Peer = class extends Remote {
this._roles = {};
}

/**
* Close the service connection.
*/
close() {
if(this._endorserClient) {
logger.info('close - closing peer connection ' + this._endpoint.addr);
this._endorserClient.close();
}
}

/**
* Set a role for this peer.
* @param {string} role - The name of the role
Expand Down
4 changes: 4 additions & 0 deletions test/integration/e2e/e2eUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,10 @@ function invokeChaincode(userOrg, version, chaincodeId, t, useStore){
t.comment('export E2E_TX_ID='+'\''+tx_id.getTransactionID()+'\'');
t.comment('******************************************************************');
logger.debug('invokeChaincode end');

// close the connections
channel.close();
t.pass('Successfully closed all connections');
return true;
} else {
t.fail('Failed to order the transaction. Error code: ' + response.status);
Expand Down
2 changes: 1 addition & 1 deletion test/integration/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,4 @@ test('Test chaincode instantiate with event, transaction invocation with chainco

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
3 changes: 3 additions & 0 deletions test/integration/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ async function actions(t) {
logger.debug(' queryTransaction ::%j',results);
t.equals(0, results.validationCode, 'Should be able to find our transaction validationCode by admin');

// close out connection
channel.close();

logMemory();
//await sleep(2000);
logger.info('*********** pass all done *************');
Expand Down
11 changes: 11 additions & 0 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ test('\n\n ** Channel - method tests **\n\n', function (t) {
t.doesNotThrow(
function () {
var orderer = new Orderer('grpc://somehost.com:1234');
_channel.close();
_channel.addOrderer(orderer);
_channel.close();
},
null,
'checking the channel addOrderer()'
Expand Down Expand Up @@ -286,6 +288,15 @@ test('\n\n ** Channel addPeer() duplicate tests **\n\n', function (t) {
t.fail('Failed to detect duplicate peer (' + expected +
' expected | ' + channel_duplicate.getPeers().length + ' found)');
}

t.doesNotThrow(
function () {
channel_duplicate.close();
},
null,
'checking the channel close()'
);

t.end();
});

Expand Down
17 changes: 17 additions & 0 deletions test/unit/orderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ test('orderer missing data test', function(t) {
},
function(err) {
t.pass('Successfully found missing data: ' + err);
client.close();
t.end();
}
).catch(function(err) {
Expand Down Expand Up @@ -177,3 +178,19 @@ test('orderer unknown address test', function(t) {
t.end();
});
});

test('Orderer test', function(t) {
var orderer = new Orderer('grpc://127.0.0.1:5005');

t.doesNotThrow(
function () {
orderer.setName('name');
orderer.close();
},
null,
'checking the orderer setName() and close()'
);
t.equals('name', orderer.getName(), 'checking getName on orderer');

t.end();
});
5 changes: 3 additions & 2 deletions test/unit/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ test('Peer test', function(t) {
t.doesNotThrow(
function () {
peer.setName('name');
peer.close();
},
null,
'checking the peer setName()'
'checking the peer setName() and close()'
);
t.equals('name', peer.getName(), 'checking getName on Peer');

Expand Down Expand Up @@ -94,7 +95,6 @@ test('Peer missing address test', function(t) {

test('Peer missing data test', function(t) {
var client = new Peer('grpc://127.0.0.1:5005');

client.sendProposal()
.then(
function(status) {
Expand All @@ -103,6 +103,7 @@ test('Peer missing data test', function(t) {
},
function(err) {
t.pass('Successfully found missing data: ' + err);
client.close();
t.end();
}
).catch(function(err) {
Expand Down

0 comments on commit 6214f05

Please sign in to comment.