Skip to content

Commit

Permalink
adding a comment on clone method performance and a test of clone method
Browse files Browse the repository at this point in the history
update "test" script
  • Loading branch information
iamigo committed Nov 1, 2016
1 parent 6926d4c commit 2ee9589
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"test-realtime": "mocha -R dot --recursive tests/realtime",
"test-subj-tag-filter": "NODE_ENV=testSubjTagFilter mocha -R dot --recursive tests/subjectTagFilter",
"test-view": "NODE_ENV=test mocha -R dot --recursive --compilers js:babel-core/register --require ./tests/view/setup.js tests/view",
"test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R dot --recursive tests/api tests/db && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage && npm run test-view && npm run test-realtime && npm run test-token-req && npm run test-token-notreq && npm run test-disablehttp && npm run test-subj-tag-filter",
"test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R dot --recursive tests/api tests/db tests/config tests/view/perspectives/eventsQueue && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage && npm run test-view && npm run test-realtime && npm run test-token-req && npm run test-token-notreq && npm run test-disablehttp && npm run test-subj-tag-filter",
"postinstall": "NODE_ENV=production gulp browserifyViews && gulp movecss && gulp movesocket && gulp movelensutil && npm run checkdb",
"prestart": "npm run checkprivatedb"
},
Expand Down
86 changes: 86 additions & 0 deletions tests/view/perspectives/eventsQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,92 @@ const eventsQueue = require(
'../../../view/perspective/eventsQueue'
);

describe('eventsQueue', () => {
it('clone', (done) => {
const evt = {
'refocus.internal.realtime.sample.update': {
new: {
id: '189c61e6-e310-404c-81a5-f526e0ea51e1',
isDeleted: '0',
messageBody: null,
messageCode: null,
name: 'FirstFloor.LivingRoom.Rug|Color',
status: 'OK',
previousStatus: 'Timeout',
statusChangedAt: '2016-10-31T18:23:26.651Z',
value: '0',
relatedLinks: [
{
url: 'https://foo.bar.baz.com?abc=def',
name: 'Foo',
}, {
url: 'https://bar.baz.foo.com?def=abc&x=y',
name: 'Bar',
},
],
createdAt: '2016-09-02T17:09:51.822Z',
updatedAt: '2016-11-01T00:21:44.012Z',
deletedAt: null,
aspectId: '5352e458-e69e-4de7-a7e0-0303e107ceab',
provider: null,
subjectId: 'afe98f09-9fb0-42aa-9dfa-8b77329619ed',
aspect: {
id: '5352e458-e69e-4de7-a7e0-0303e107ceab',
description: 'The color of the subject',
isPublished: true,
helpEmail: 'abc@baz.com',
helpUrl: 'https://foo.bar.baz.com/FirstFloor',
name: 'Color',
timeout: '360s',
criticalRange: [3, 4],
warningRange: [2, 2],
infoRange: [1, 1],
okRange: [0, 0],
valueLabel: null,
tags: [],
},
subject: {
absolutePath: 'FirstFloor.LivingRoom.Rug',
childCount: 0,
description: 'The rug in the living room on the first floor',
geolocation: null,
helpEmail: null,
helpUrl: null,
id: 'afe98f09-9fb0-42aa-9dfa-8b77329619ed',
imageUrl: null,
isDeleted: '0',
isPublished: true,
name: 'NA39',
parentAbsolutePath: 'FirstFloor.LivingRoom',
relatedLinks: [
{
url: 'https://foo.bar.baz.com?abc=def',
name: 'XXXXXX',
}, {
url: 'https://foo.bar.baz.com?ghi=jkl',
name: 'YYYYYY',
}, {
url: 'https://foo.bar.baz.com?mno=pqr',
name: 'ZZZZZZZ',
},
],
tags: ['Room', 'House'],
createdAt: '2016-09-02T17:08:38.247Z',
updatedAt: '2016-10-31T06:45:36.569Z',
deletedAt: null,
hierarchyLevel: 5,
parentId: '26f56955-56f4-4aef-b42b-c4725cd4a8ed',
createdBy: null,
},
absolutePath: 'FirstFloor.LivingRoom.Rug',
},
},
};
expect(eventsQueue.clone(evt)).to.be.eql(JSON.parse(JSON.stringify(evt)));
done();
});
});

describe('event queue', () => {
after((done) => {
eventsQueue.queue.splice(0, eventsQueue.queue.length);
Expand Down
5 changes: 5 additions & 0 deletions view/perspective/eventsQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ function createAndDispatchLensEvent(queueToFlush, lensElement) {

/**
* Clone object to handle race conditions.
*
* A note on performance: calling this "clone" function 3000 times with a
* sample or subject event takes about ~70ms.
* Running JSON.parse(JSON.stringify(...)) for the same objects takes ~129ms.
*
* @param {Object} obj - Object to copy
* @returns {Object} copy - New copied object
*/
Expand Down

0 comments on commit 2ee9589

Please sign in to comment.