Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add config option: autoReadResources #15

Merged
merged 1 commit into from
Feb 27, 2019
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
11 changes: 8 additions & 3 deletions lib/components/reqHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ function clientRegisterHandler (shepherd, req, rsp) {
setTimeout(function() {
cnode = shepherd.find(devAttrs.clientName);
if (cnode) {
cnode._readAllResource().then(function (rspObj) {
return cnode.dbSave();
}).then(function () {
var promise;
if (shepherd._config.autoReadResources)
promise = cnode._readAllResource().then(function (rspObj) {
return cnode.dbSave();
});
else
promise = Q.fcall(function () {});
promise.then(function () {
if (cnode.heartbeatEnabled)
return cnode.observeReq('/heartbeat');
}).then(function () {
Expand Down
3 changes: 3 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ module.exports = {
// default is 60 secs.
hbTimeout: 60,

// auto read client resources when it's registering.
autoReadResources: true,

defaultDbFolder: __dirname + '/database',

defaultDbPath: __dirname + '/database/coap.db'
Expand Down
54 changes: 52 additions & 2 deletions test/coap-shepherd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ describe('coap-shepherd', function () {
});

describe('Functional Check', function () {
var CoapShepherd = shepherd.constructor;
var _updateNetInfoStub, testDbPath = __dirname + '/../lib/database/test.db';

before(function () {
Expand Down Expand Up @@ -273,8 +274,6 @@ describe('coap-shepherd', function () {
this.timeout(5000);

describe('#.constructor()', function () {
var CoapShepherd = shepherd.constructor;

it('should create an instance when passing no arguments', function () {
var created = new CoapShepherd();
expect(created).to.be.not.null;
Expand Down Expand Up @@ -494,6 +493,57 @@ describe('coap-shepherd', function () {
});
});

describe('#config.autoReadResources', function () {
it('should not call cnode._readAllResource when autoReadResources is false', function (done) {
var shepherd = new CoapShepherd({port: 5684, defaultDbPath: testDbPath, autoReadResources: false});
shepherd.start().then(function () {
shepherd.alwaysPermitJoin(true);
var _readAllResourceStub = sinon.stub(CoapNode.prototype, '_readAllResource', function (path, callback) {
return null;
}),
observeReqStub = sinon.stub(CoapNode.prototype, 'observeReq', function (callback) {
return Q.resolve({
status: '2.05',
data: 'hb'
});
}),
rsp = {},
cnode,
regCallback = function (msg) {
if (msg.type === 'devIncoming') {
cnode = msg.cnode;
expect(rsp.setOption).to.have.been.calledWith('Location-Path', [new Buffer('rd'),new Buffer(cnode.clientId.toString())]);
expect(rsp.end).to.have.been.calledWith('');
expect(_readAllResourceStub).to.have.not.been.called;
if (shepherd.find('cnode02') === cnode) {
_readAllResourceStub.restore();
observeReqStub.restore();
shepherd.removeListener('ind', regCallback);
done();
}
}
};

rsp.setOption = sinon.spy();
rsp.end = sinon.spy();

shepherd.on('ind', regCallback);

emitClintReqMessage(shepherd, {
code: '0.01',
method: 'POST',
url: '/rd?ep=cnode02&lt=86400&lwm2m=1.0.0&mac=BB:BB:BB',
rsinfo: {
address: '127.0.0.1',
port: '5687'
},
payload: '</a/0>,</a/1>,</b/0>,</b/1>',
headers: {}
}, rsp);
});
});
});

describe('#update cnode', function () {
it('should update cnode lifetime', function (done) {
var rsp = {},
Expand Down