From b9c57e3ddb8f82660b1f21007bdb71733fc16b68 Mon Sep 17 00:00:00 2001 From: scottinet Date: Tue, 20 Sep 2016 17:50:19 +0200 Subject: [PATCH 1/5] allow changing host --- src/kuzzle.js | 2 ++ test/kuzzle/methods.test.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/kuzzle.js b/src/kuzzle.js index 11a634172..a124f2634 100644 --- a/src/kuzzle.js +++ b/src/kuzzle.js @@ -109,6 +109,7 @@ module.exports = Kuzzle = function (host, options, cb) { }, host: { value: host, + writable: true, enumerable: true }, wsPort: { @@ -1003,6 +1004,7 @@ Kuzzle.prototype.disconnect = function () { this.state = 'disconnected'; this.network.close(); + this.network = null; for (collection in this.collections) { if (this.collections.hasOwnProperty(collection)) { diff --git a/test/kuzzle/methods.test.js b/test/kuzzle/methods.test.js index 37a1766c9..de4a5df86 100644 --- a/test/kuzzle/methods.test.js +++ b/test/kuzzle/methods.test.js @@ -399,7 +399,7 @@ describe('Kuzzle methods', function () { kuzzle.collections = { foo: {}, bar: {}, baz: {} }; kuzzle.disconnect(); - should(kuzzle.network.close.called).be.true(); + should(kuzzle.network).be.null(); should(kuzzle.collections).be.empty(); should(function () { kuzzle.isValid(); }).throw(Error); }); From 494069913fee0bdc6afc8d3b080d37f456813917 Mon Sep 17 00:00:00 2001 From: scottinet Date: Tue, 20 Sep 2016 17:52:12 +0200 Subject: [PATCH 2/5] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 820e7a94f..d5533cfc9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "2.1.0", + "version": "2.1.1", "description": "Official Javascript SDK for Kuzzle", "author": "The Kuzzle Team ", "repository": { From 7abd7aee8314c92e0b649eb190b3c4d606e8b428 Mon Sep 17 00:00:00 2001 From: scottinet Date: Wed, 21 Sep 2016 09:54:47 +0200 Subject: [PATCH 3/5] update unit tests --- src/kuzzle.js | 6 +++-- test/kuzzle/constructor.test.js | 39 +++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/kuzzle.js b/src/kuzzle.js index a124f2634..ff750f7cd 100644 --- a/src/kuzzle.js +++ b/src/kuzzle.js @@ -315,10 +315,12 @@ module.exports = Kuzzle = function (host, options, cb) { Kuzzle.prototype.connect = function () { var self = this; - if (!self.network) { - self.network = networkWrapper(self.host, self.wsPort, self.ioPort, self.sslConnection); + if (self.network) { + self.disconnect(); } + self.network = networkWrapper(self.host, self.wsPort, self.ioPort, self.sslConnection); + if (['initializing', 'ready', 'disconnected', 'error', 'offline'].indexOf(this.state) === -1) { if (self.connectCB) { self.connectCB(null, self); diff --git a/test/kuzzle/constructor.test.js b/test/kuzzle/constructor.test.js index 8c834d197..2163aeaa6 100644 --- a/test/kuzzle/constructor.test.js +++ b/test/kuzzle/constructor.test.js @@ -291,6 +291,20 @@ describe('Kuzzle constructor', () => { }, 10); }); + it('should first disconnect if it was connected', function () { + var + kuzzle = new Kuzzle('nowhere', {connect: 'manual'}), + disconnectStub = sinon.stub(); + + kuzzle.disconnect = disconnectStub; + kuzzle.connect(); + + should(kuzzle.disconnect.called).be.false(); + + kuzzle.connect(); + should(kuzzle.disconnect.called).be.true(); + }); + describe('=> on connection error', () => { beforeEach(function () { networkStub.onConnectError = function (cb) { @@ -382,22 +396,25 @@ describe('Kuzzle constructor', () => { it('should dequeue requests automatically on a connection success', function (done) { var - dequeued = false, - kuzzle, - KuzzleRewired = rewire(kuzzleSource), - revert = KuzzleRewired.__set__('dequeue', function () { dequeued = true; }); + dequeueStub = sinon.stub(), + kuzzle; this.timeout(500); - kuzzle = new KuzzleRewired('nowhere', {connect: 'manual', autoReplay: false, autoQueue: false}, () => { - should(kuzzle.state).be.exactly('connected'); - should(dequeued).be.true(); - revert(); - done(); - }); + kuzzle = new Kuzzle('nowhere', {connect: 'manual', autoReplay: false, autoQueue: false}); - kuzzle.network = networkStub; + kuzzle.queuing = true; + kuzzle.offlineQueue.push({query: 'foo'}); + kuzzle.addListener('offlineQueuePop', dequeueStub); kuzzle.connect(); + + setTimeout(function () { + should(dequeueStub.called).be.true(); + should(kuzzle.state).be.exactly('connected'); + should(kuzzle.queuing).be.false(); + should(kuzzle.offlineQueue).be.empty(); + done(); + }, 20); }); }); From 602932146fc522b9c11dc759a25607931570662a Mon Sep 17 00:00:00 2001 From: scottinet Date: Wed, 21 Sep 2016 11:06:45 +0200 Subject: [PATCH 4/5] update the ioPort and wsPort properties to be writable --- src/kuzzle.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/kuzzle.js b/src/kuzzle.js index ff750f7cd..ffe67c309 100644 --- a/src/kuzzle.js +++ b/src/kuzzle.js @@ -114,11 +114,13 @@ module.exports = Kuzzle = function (host, options, cb) { }, wsPort: { value: (options && typeof options.wsPort === 'number') ? options.wsPort : 7513, - enumerable: true + enumerable: true, + writable: true }, ioPort: { value: (options && typeof options.ioPort === 'number') ? options.ioPort : 7512, - enumerable: true + enumerable: true, + writable: true }, sslConnection: { value: (options && typeof options.sslConnection === 'boolean') ? options.sslConnection : false, From ddf8b713991e422a4a18f6427d53c482cab67116 Mon Sep 17 00:00:00 2001 From: scottinet Date: Wed, 21 Sep 2016 11:15:52 +0200 Subject: [PATCH 5/5] update unit tests --- test/kuzzle/constructor.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/kuzzle/constructor.test.js b/test/kuzzle/constructor.test.js index 2163aeaa6..3556773d2 100644 --- a/test/kuzzle/constructor.test.js +++ b/test/kuzzle/constructor.test.js @@ -69,8 +69,8 @@ describe('Kuzzle constructor', () => { should(kuzzle).have.propertyWithDescriptor('reconnectionDelay', { enumerable: true, writable: false, configurable: false }); should(kuzzle).have.propertyWithDescriptor('jwtToken', { enumerable: true, writable: true, configurable: false }); should(kuzzle).have.propertyWithDescriptor('offlineQueueLoader', { enumerable: true, writable: true, configurable: false }); - should(kuzzle).have.propertyWithDescriptor('wsPort', { enumerable: true, writable: false, configurable: false }); - should(kuzzle).have.propertyWithDescriptor('ioPort', { enumerable: true, writable: false, configurable: false }); + should(kuzzle).have.propertyWithDescriptor('wsPort', { enumerable: true, writable: true, configurable: false }); + should(kuzzle).have.propertyWithDescriptor('ioPort', { enumerable: true, writable: true, configurable: false }); should(kuzzle).have.propertyWithDescriptor('sslConnection', { enumerable: true, writable: false, configurable: false }); });