Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <support@kuzzle.io>",
"repository": {
Expand Down
14 changes: 10 additions & 4 deletions src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,18 @@ module.exports = Kuzzle = function (host, options, cb) {
},
host: {
value: host,
writable: true,
enumerable: true
},
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,
Expand Down Expand Up @@ -314,10 +317,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);
Expand Down Expand Up @@ -1003,6 +1008,7 @@ Kuzzle.prototype.disconnect = function () {

this.state = 'disconnected';
this.network.close();
this.network = null;

for (collection in this.collections) {
if (this.collections.hasOwnProperty(collection)) {
Expand Down
43 changes: 30 additions & 13 deletions test/kuzzle/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/kuzzle/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down