Skip to content

Commit

Permalink
Merge pull request #9 from ph0bos/get-client-connection-check
Browse files Browse the repository at this point in the history
Check connection before returning client
  • Loading branch information
ph0bos committed Sep 5, 2015
2 parents aee8934 + 5d8da04 commit 2279bd1
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 73 deletions.
2 changes: 1 addition & 1 deletion lib/GetChildrenBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ GetChildrenBuilder.prototype.client = function(client) {
* @method close
*/
GetChildrenBuilder.prototype.forPath = function(path, callback) {
var client = Promise.promisifyAll(this.client.getClient());
var client = Promise.promisifyAll(this.client.getClientwithConnectionCheck());
var services = [];

var recurse = function (path) {
Expand Down
6 changes: 3 additions & 3 deletions lib/ServiceDiscovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ServiceDiscovery.prototype.registerService = function (callback) {
var servicePath = [ self.basePath, self.serviceInstance.name ].join('/');

self.client
.getClient()
.getClientwithConnectionCheck()
.mkdirp(servicePath, cb);
}

Expand Down Expand Up @@ -110,7 +110,7 @@ ServiceDiscovery.prototype.unRegisterService = function (serviceId, callback) {
var serviceInstancePath = [ this.basePath, this.serviceInstance.name, serviceId ].join('/');

this.client
.getClient()
.getClientwithConnectionCheck()
.transaction()
.remove(serviceInstancePath)
.commit(callback);
Expand Down Expand Up @@ -151,7 +151,7 @@ ServiceDiscovery.prototype.serviceProviderBuilder = function () {
*/
ServiceDiscovery.prototype.queryForInstances = function (serviceName, callback) {
var servicePath = [ this.basePath, serviceName ].join('/');
this.client.getClient().getChildren(servicePath, null, callback);
this.client.getClientwithConnectionCheck().getChildren(servicePath, null, callback);
};

module.exports = ServiceDiscovery;
4 changes: 3 additions & 1 deletion lib/ServiceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ ServiceProvider.prototype.getInstance = function(callback) {

// List available service instances
function getAvailableServices (cb) {
self.serviceDiscovery.client.getClient().getChildren(absPath, null, cb);
self.serviceDiscovery.client.getClientwithConnectionCheck().getChildren(absPath, null, function(err, serviceList, stat) {
cb(err, serviceList, stat);
});
}

// Select a random service instance
Expand Down
23 changes: 21 additions & 2 deletions lib/Zoologist.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var ZooKeeperError = customErr('ZooKeeperError', Error);
util.inherits(Zoologist, EventEmitter);

// Defaults
var DEFAULT_RETRY_COUNT = 9999;
var DEFAULT_RETRY_COUNT = 5;
var DEFAULT_RETRY_WAIT_MS = 1000;
var DEFAULT_CONNECT_TIMEOUT_MS = 1000;
var DEFAULT_SESSION_TIMEOUT_MS = 5000;
Expand Down Expand Up @@ -60,12 +60,27 @@ function newClient(connectionString, retryCount, retryWait, connectTimeout, sess
* @param zkClient {Object} ZooKeeper client.
*/
function Zoologist(zkClient) {
this.started = false;
this.client = zkClient;

this.client.on('connected', this.emit.bind(this, 'connected'));
this.client.on('disconnected', this.emit.bind(this, 'disconnected'));
};

/**
* Return the ZooKeeper client.
*
* @public
* @method getClient
*/
Zoologist.prototype.getClientwithConnectionCheck = function() {
if (this.client.getState() !== zkClient.State.SYNC_CONNECTED) {
throw new ZooKeeperError('Connection Not Established');
}

return this.client;
};

/**
* Return the ZooKeeper client.
*
Expand All @@ -83,7 +98,11 @@ Zoologist.prototype.getClient = function() {
* @method start
*/
Zoologist.prototype.start = function() {
var self = this;
this.client.connect();
this.client.once('connected', function () {
self.started = true;
});
};

/**
Expand All @@ -93,7 +112,7 @@ Zoologist.prototype.start = function() {
* @method close
*/
Zoologist.prototype.close = function() {
// Not implemented
this.started = false;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zoologist",
"version": "0.4.3",
"version": "0.4.4",
"description": "A Curator-esque framework for ZooKeeper",
"main": "index.js",
"keywords": [
Expand Down
42 changes: 22 additions & 20 deletions test/GetChildrenBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@ describe('GetChildrenBuilder', function() {
client = Zoologist.newClient('127.0.0.1:2181');
client.start();

serviceInstance =
ServiceInstanceBuilder
.builder()
.address('localhost')
.port(12345)
.name('my/service/v2')
.build();

serviceDiscovery =
ServiceDiscoveryBuilder
.builder()
.client(client)
.thisInstance(serviceInstance)
.basePath('services')
.build();

serviceProvider = new ServiceProvider(serviceDiscovery, 'my/service/v2');

serviceDiscovery.registerService(function onRegister(err, data) {
done();
client.once('connected', function () {
serviceInstance =
ServiceInstanceBuilder
.builder()
.address('localhost')
.port(12345)
.name('my/service/v2')
.build();

serviceDiscovery =
ServiceDiscoveryBuilder
.builder()
.client(client)
.thisInstance(serviceInstance)
.basePath('services')
.build();

serviceProvider = new ServiceProvider(serviceDiscovery, 'my/service/v2');

serviceDiscovery.registerService(function onRegister(err, data) {
done();
});
});
});

Expand Down
40 changes: 21 additions & 19 deletions test/ServiceDiscovery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@ describe('ServiceDiscovery', function() {
client = Zoologist.newClient('127.0.0.1:2181');
client.start();

builder = ServiceDiscoveryBuilder.builder();

serviceInstance =
ServiceInstanceBuilder
.builder()
.address('localhost')
.port(12345)
.name('my/service/v1')
.build();

serviceDiscovery =
builder
.client(client)
.thisInstance(serviceInstance)
.basePath('services')
.build();

serviceDiscovery.registerService(function onRegister(err, data) {
done();
client.once('connected', function () {
builder = ServiceDiscoveryBuilder.builder();

serviceInstance =
ServiceInstanceBuilder
.builder()
.address('localhost')
.port(12345)
.name('my/service/v1')
.build();

serviceDiscovery =
builder
.client(client)
.thisInstance(serviceInstance)
.basePath('services')
.build();

serviceDiscovery.registerService(function onRegister(err, data) {
done();
});
});
});

Expand Down
22 changes: 13 additions & 9 deletions test/ServiceDiscoveryBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ var ServiceDiscoveryBuilder = require('..').ServiceDiscoveryBuilder;

describe('ServiceDiscoveryBuilder', function() {

beforeEach(function(){
beforeEach(function(done){
client = Zoologist.newClient('127.0.0.1:2181');
client.start();

builder = ServiceDiscoveryBuilder.builder();

serviceInstance =
ServiceInstanceBuilder
.builder()
.address('localhost')
.port(12345)
.build();
client.once('connected', function () {
builder = ServiceDiscoveryBuilder.builder();

serviceInstance =
ServiceInstanceBuilder
.builder()
.address('localhost')
.port(12345)
.build();

done();
});
});

it('should instantiate an instance of ServiceDiscoveryBuilder when calling builder()', function() {
Expand Down
36 changes: 19 additions & 17 deletions test/ServiceProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@ describe('ServiceProvider', function() {
client = Zoologist.newClient('127.0.0.1:2181');
client.start();

serviceInstance =
ServiceInstanceBuilder
.builder()
.address('localhost')
.port(12345)
.name('my/service/v1')
.build();
client.once('connected', function () {
serviceInstance =
ServiceInstanceBuilder
.builder()
.address('localhost')
.port(12345)
.name('my/service/v1')
.build();

serviceDiscovery =
ServiceDiscoveryBuilder
.builder()
.client(client)
.thisInstance(serviceInstance)
.basePath('services')
.build();
serviceDiscovery =
ServiceDiscoveryBuilder
.builder()
.client(client)
.thisInstance(serviceInstance)
.basePath('services')
.build();

serviceProvider = new ServiceProvider(serviceDiscovery, 'my/service/v1');
serviceProvider = new ServiceProvider(serviceDiscovery, 'my/service/v1');

serviceDiscovery.registerService(function onRegister(err, data) {
done();
serviceDiscovery.registerService(function onRegister(err, data) {
done();
});
});
});

Expand Down
14 changes: 14 additions & 0 deletions test/Zoologist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ describe('Zoologist', function() {
zoologist.start();
});

it('should be classified as started once start() has been called', function() {
zoologist.start();
});

it('should be classified as not started if the start() function has not been called', function() {
zoologist.getStarted().should.equal(false);
});

it('should be classified as not started if the close() function has been called', function() {
zoologist.start();
zoologist.close();
zoologist.getStarted().should.equal(false);
});

it('should close the framework when calling close()', function() {
zoologist.close();
});
Expand Down

0 comments on commit 2279bd1

Please sign in to comment.