Skip to content

Commit

Permalink
FAB-10910 default channel not returned
Browse files Browse the repository at this point in the history
Client.getChannel() no longer returns first channel when using a network
configuration and nothing is passed to the getChannel call. The comments
and doc comments say that passing in a channel name is still optional.

Change-Id: I732be47778dbe11271a5a6ffd3c090e5f914efd9
Signed-off-by: Dave Kelsey <[email protected]>
  • Loading branch information
Dave Kelsey committed Jun 28, 2018
1 parent 20da621 commit 9f32dbd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
18 changes: 16 additions & 2 deletions fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,27 @@ const Client = class extends BaseClient {
* @returns {Channel} The channel instance
*/
getChannel(name, throwError = true) {
let channel = this._channels.get(name);
let channel;
if (name) {
channel = this._channels.get(name);
} else if (this._channels.size > 0) {
// not sure it's deterministic which channel would be returned if more than 1.
channel = this._channels.values().next().value;
}

if (channel) return channel;
else {
// maybe it is defined in the network config
if (this._network_config) {
channel = this._network_config.getChannel(name);
if (!name) {
let channel_names = Object.keys(this._network_config._network_config.channels);
if(channel_names) {
name = channel_names[0];
}
}
if (name) {
channel = this._network_config.getChannel(name);
}
}
if (channel) {
this._channels.set(name, channel);
Expand Down
64 changes: 64 additions & 0 deletions test/unit/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,3 +1074,67 @@ test('\n\n*** Test Set and Add TLS ClientCert ***\n', function(t) {

t.end();
});

test('\n\n*** Test channel selection if no channel name provided ***\n', (t) => {
let config = {
'name': 'test',
'version': '1.0.0',
'channels': {
'testchannel': {
'orderers': [
'orderer.example.com'
],
'peers': {
'peer0.org1.example.com': {}
}
},
'anotherchannel': {
'orderers': [
'orderer.example.com'
],
'peers': {
'peer0.org1.example.com': {}
}
}
},
'organizations': {
'Org1': {
'mspid': 'Org1MSP',
'peers': [
'peer0.org1.example.com'
]
}
},
'orderers': {
'orderer.example.com': {
'url': 'grpc://localhost:7050'
}
},
'peers': {
'peer0.org1.example.com': {
'url': 'grpc://localhost:7051',
'eventUrl': 'grpc://localhost:7053'
}
}
};

let client = Client.loadFromConfig(config);
t.doesNotThrow(() => {
// TODO: really ? have to set this even if it's not used
client.setTlsClientCertAndKey(aPem, aPem);
let channel = client.getChannel();
t.equals(channel.getName(), 'testchannel', 'correct channel is returned from network config');
});

client = new Client();
client._channels.set('aChannel', 'SomeChannelObject');
t.doesNotThrow(() => {
client.setTlsClientCertAndKey(aPem, aPem);
let channel = client.getChannel();
t.equals(channel, 'SomeChannelObject', 'correct channel is returned from channel map');
});


t.pass('Should get default channel if no channel name provided defined');
t.end();
});

0 comments on commit 9f32dbd

Please sign in to comment.