Merge pull request #3047 from strongloop/forward-port/avoid-change-cleanup

Add option disabling periodic change rectification
This commit is contained in:
Miroslav Bajtoš 2017-01-04 16:39:44 +01:00 committed by GitHub
commit 0d1f74e2cd
2 changed files with 76 additions and 2 deletions

View File

@ -1628,14 +1628,16 @@ module.exports = function(registry) {
var idDefn = idProp && idProp.defaultFn;
if (idType !== String || !(idDefn === 'uuid' || idDefn === 'guid')) {
deprecated('The model ' + this.modelName + ' is tracking changes, ' +
'which requries a string id with GUID/UUID default value.');
'which requires a string id with GUID/UUID default value.');
}
Model.observe('after save', rectifyOnSave);
Model.observe('after delete', rectifyOnDelete);
if (runtime.isServer) {
// Only run if the run time is server
// Can switch off cleanup by setting the interval to -1
if (runtime.isServer && cleanupInterval > 0) {
// initial cleanup
cleanup();

View File

@ -12,6 +12,7 @@ var defineModelTestsWithDataSource = require('./util/model-tests');
var PersistedModel = loopback.PersistedModel;
var expect = require('./helpers/expect');
var debug = require('debug')('test');
var runtime = require('./../lib/runtime');
describe('Replication / Change APIs', function() {
this.timeout(10000);
@ -60,6 +61,77 @@ describe('Replication / Change APIs', function() {
};
});
describe('cleanup check for enableChangeTracking', function() {
describe('when no changeCleanupInterval set', function() {
it('should call rectifyAllChanges if running on server', function(done) {
var calls = mockRectifyAllChanges(SourceModel);
SourceModel.enableChangeTracking();
if (runtime.isServer) {
expect(calls).to.eql(['rectifyAllChanges']);
} else {
expect(calls).to.eql([]);
}
done();
});
});
describe('when changeCleanupInterval set to -1', function() {
var Model;
beforeEach(function() {
Model = this.Model = PersistedModel.extend(
'Model-' + tid,
{id: {id: true, type: String, defaultFn: 'guid'}},
{trackChanges: true, changeCleanupInterval: -1});
Model.attachTo(dataSource);
});
it('should not call rectifyAllChanges', function(done) {
var calls = mockRectifyAllChanges(Model);
Model.enableChangeTracking();
expect(calls).to.eql([]);
done();
});
});
describe('when changeCleanupInterval set to 10000', function() {
var Model;
beforeEach(function() {
Model = this.Model = PersistedModel.extend(
'Model-' + tid,
{id: {id: true, type: String, defaultFn: 'guid'}},
{trackChanges: true, changeCleanupInterval: 10000});
Model.attachTo(dataSource);
});
it('should call rectifyAllChanges if running on server', function(done) {
var calls = mockRectifyAllChanges(Model);
Model.enableChangeTracking();
if (runtime.isServer) {
expect(calls).to.eql(['rectifyAllChanges']);
} else {
expect(calls).to.eql([]);
}
done();
});
});
function mockRectifyAllChanges(Model) {
var calls = [];
Model.rectifyAllChanges = function(cb) {
calls.push('rectifyAllChanges');
process.nextTick(cb);
};
return calls;
}
});
describe('optimization check rectifyChange Vs rectifyAllChanges', function() {
beforeEach(function initialData(done) {
var data = [{name: 'John', surname: 'Doe'}, {name: 'Jane', surname: 'Roe'}];