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
6 changes: 3 additions & 3 deletions src/Kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Kuzzle extends KuzzleEventEmitter {
object.jwt = this.jwt;
}

Object.assign(object.volatile, room.volatile, {sdkVersion: this.sdkVersion});
Object.assign(object.volatile, room.volatile, {sdkInstanceId: this.network.id, sdkVersion: this.sdkVersion});

this.network.subscribe(object, opts, notificationCB, subscribeCB);
}
Expand All @@ -143,7 +143,7 @@ class Kuzzle extends KuzzleEventEmitter {
object.jwt = this.jwt;
}

Object.assign(object.volatile, room.volatile, {sdkVersion: this.sdkVersion});
Object.assign(object.volatile, room.volatile, {sdkInstanceId: this.network.id, sdkVersion: this.sdkVersion});

this.network.unsubscribe(object, room.channel, unsubscribeCB);
}
Expand Down Expand Up @@ -1157,7 +1157,7 @@ class Kuzzle extends KuzzleEventEmitter {
throw new Error('Invalid query parameter: ' + query);
}

Object.assign(object.volatile, query.volatile, {sdkVersion: this.sdkVersion});
Object.assign(object.volatile, query.volatile, {sdkInstanceId: this.network.id, sdkVersion: this.sdkVersion});

for (const attr of Object.keys(query)) {
if (attr !== 'volatile') {
Expand Down
44 changes: 4 additions & 40 deletions src/networkWrapper/protocols/abstract/realtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const
uuidv4 = require('uuid/v4'),
KuzzleEventEmitter = require('../../../eventEmitter');

class RTWrapper extends KuzzleEventEmitter {
Expand All @@ -9,9 +10,8 @@ class RTWrapper extends KuzzleEventEmitter {
super();

Object.defineProperties(this, {
cleanHistoryTimer: {
value: null,
writable: true
id: {
value: uuidv4()
},
host: {
value: host,
Expand Down Expand Up @@ -107,11 +107,6 @@ class RTWrapper extends KuzzleEventEmitter {
}
}

Object.defineProperty(this, 'requestHistory', {
value: {},
writable: true
});

this.wasConnected = false;
this.stopRetryingToConnect = false;
this.retrying = false;
Expand Down Expand Up @@ -140,12 +135,6 @@ class RTWrapper extends KuzzleEventEmitter {
if (this.autoReplay) {
this.playQueue();
}

if (!this.cleanHistoryTimer) {
this.cleanHistoryTimer = setInterval(() => {
cleanHistory(this.requestHistory);
}, 1000);
}
}

/**
Expand All @@ -157,7 +146,6 @@ class RTWrapper extends KuzzleEventEmitter {
this.startQueuing();
}

this.clearHistoryTimer();
this.emit('disconnect');
}

Expand All @@ -172,8 +160,6 @@ class RTWrapper extends KuzzleEventEmitter {
this.startQueuing();
}

this.clearHistoryTimer();

this.emit('networkError', error);
if (this.autoReconnect && !this.retrying && !this.stopRetryingToConnect) {
this.retrying = true;
Expand Down Expand Up @@ -226,7 +212,7 @@ class RTWrapper extends KuzzleEventEmitter {
return cb(error);
}
this.on(response.result.channel, data => {
data.fromSelf = this.requestHistory[data.requestId] !== undefined;
data.fromSelf = data.volatile !== undefined && data.volatile.sdkInstanceId === this.id;
notificationCB(data);
});
cb(null, response.result);
Expand Down Expand Up @@ -261,13 +247,6 @@ class RTWrapper extends KuzzleEventEmitter {

return discardRequest(object, cb);
}

clearHistoryTimer() {
if (this.cleanHistoryTimer) {
clearInterval(this.cleanHistoryTimer);
this.cleanHistoryTimer = null;
}
}
}
/**
* Emit a request to Kuzzle
Expand Down Expand Up @@ -298,7 +277,6 @@ function emitRequest (network, request, cb) {
});
}
// Track requests made to allow Room.subscribeToSelf to work
network.requestHistory[request.requestId] = Date.now();
network.send(request);
}

Expand Down Expand Up @@ -383,18 +361,4 @@ function dequeue (network) {
dequeuingProcess();
}

/**
* Clean history from requests made more than 10s ago
*/
function cleanHistory (requestHistory) {
var
now = Date.now();

Object.keys(requestHistory).forEach(function (key) {
if (requestHistory[key] < now - 10000) {
delete requestHistory[key];
}
});
}

module.exports = RTWrapper;
2 changes: 1 addition & 1 deletion test/SearchResult/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('SearchResult methods', function () {
collection.scroll = sinon.stub();
});

it.only('should be able to perform a search-after request', function (done) {
it('should be able to perform a search-after request', function (done) {
var firstSearchResult;

searchFilters = {sort: [{foo: 'asc'}, 'bar']};
Expand Down
8 changes: 4 additions & 4 deletions test/kuzzle/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Kuzzle query management', function () {
collection: 'collection',
controller: 'controller',
index: 'index',
volatile: { sdkVersion: kuzzle.sdkVersion },
volatile: { sdkInstanceId: kuzzle.network.id, sdkVersion: kuzzle.sdkVersion },
requestId: sinon.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
});
});
Expand All @@ -50,7 +50,7 @@ describe('Kuzzle query management', function () {
collection: 'collection',
controller: 'controller',
index: 'index',
volatile: { sdkVersion: kuzzle.sdkVersion },
volatile: { sdkInstanceId: kuzzle.network.id, sdkVersion: kuzzle.sdkVersion },
requestId: sinon.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
});
});
Expand All @@ -66,7 +66,7 @@ describe('Kuzzle query management', function () {
collection: 'collection',
controller: 'controller',
index: 'index',
volatile: { sdkVersion: kuzzle.sdkVersion },
volatile: { sdkInstanceId: kuzzle.network.id, sdkVersion: kuzzle.sdkVersion },
requestId: sinon.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
}, null, sinon.match(function(f) {return f === cb;}));
});
Expand All @@ -81,7 +81,7 @@ describe('Kuzzle query management', function () {
collection: 'collection',
controller: 'controller',
index: 'index',
volatile: { sdkVersion: kuzzle.sdkVersion },
volatile: { sdkInstanceId: kuzzle.network.id, sdkVersion: kuzzle.sdkVersion },
requestId: sinon.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
}, null, sinon.match(function(f) {return f === cb;}));
});
Expand Down
89 changes: 10 additions & 79 deletions test/network/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ describe('Network query management', function () {
should(sendSpy).be.calledWith(request);
});

it('should populate the request History when the request has been emitted', function (done) {
var
now = Date.now();

network.requestHistory = {};

emitRequest(network, {requestId: 'foo', response: 'bar'}, function() {
should(network.requestHistory.foo).not.be.undefined().and.be.approximately(now, 10);
done();
});
});

it('should trigger a "tokenExpired" event if the token has expired', function (done) {
var
eventStub = sinon.stub(),
Expand Down Expand Up @@ -339,20 +327,26 @@ describe('Network query management', function () {
notificationCB = sinon.stub(),
cb = sinon.stub();

network.requestHistory.bar = {};
network.subscribe({foo: 'bar'}, {bar: 'foo'}, notificationCB, cb);
network.emit('foobar', {type: 'document', result: {}, action: 'foo', requestId: 'bar'});
network.emit('foobar', {type: 'document', result: {}, action: 'foo', volatile: {sdkInstanceId: network.id}});

should(notificationCB)
.be.calledOnce()
.be.calledWithMatch({fromSelf: true});

notificationCB.reset();
network.emit('foobar', {type: 'document', result: {}, action: 'foo', requestId: 'foo'});
network.emit('foobar', {type: 'document', result: {}, action: 'foo', volatile: {sdkInstanceId: 'foobar'}});

should(notificationCB)
.be.calledOnce()
.be.calledWithMatch({fromSelf: false});

notificationCB.reset();
network.emit('foobar', {type: 'document', result: {}, action: 'foo'});

should(notificationCB)
.be.calledOnce()
.not.be.calledWithMatch({fromSelf: true});
.be.calledWithMatch({fromSelf: false});
});
});

Expand Down Expand Up @@ -431,67 +425,4 @@ describe('Network query management', function () {
should(notificationCB).not.be.called();
});
});

describe('#cleanHistory', function () {
it('should be started by network wrapper constructor', function () {
var
clock = sinon.useFakeTimers(),
cleanStub = sinon.stub(),
network;

RTWrapper = rewire('../../src/networkWrapper/protocols/abstract/realtime');
RTWrapper.__set__('cleanHistory', cleanStub);
network = new RTWrapper('somewhere');

should(network.cleanHistoryTimer).be.null();
should(cleanStub).not.be.called();

network.clientConnected();

should(network.cleanHistoryTimer).be.not.null();
should(cleanStub).not.be.called();

clock.tick(1000);

should(network.cleanHistoryTimer).be.not.null();
should(cleanStub).be.calledOnce();

network.clearHistoryTimer();
should(network.cleanHistoryTimer).be.null();

clock.restore();
});

it('should clean oldest entries every 1s', function () {
var
i,
clock = sinon.useFakeTimers(),
network;

RTWrapper = rewire('../../src/networkWrapper/protocols/abstract/realtime');
network = new RTWrapper('somewhere');
network.clientConnected();

for (i = 100000; i >= 0; i -= 10000) {
network.requestHistory[i] = -i;
}

should(Object.keys(network.requestHistory).length).be.eql(11);
clock.tick(1000);

// should only contains i == 0 entry
should(Object.keys(network.requestHistory).length).be.eql(1);
should(Object.keys(network.requestHistory)).eql(['0']);

network.requestHistory.foobar = -100000;
should(Object.keys(network.requestHistory).length).be.eql(2);

clock.tick(1000);
should(Object.keys(network.requestHistory).length).be.eql(1);
should(Object.keys(network.requestHistory)).eql(['0']);

clock.restore();
network.clearHistoryTimer();
});
});
});