Skip to content

Commit 18a89e5

Browse files
authored
Merge pull request #2960 from kobaska/avoid-cleanup
Avoid periodic cleanup/rectification of changes
2 parents fe1c0b6 + b3a5bc7 commit 18a89e5

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

lib/persisted-model.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,14 +1597,16 @@ module.exports = function(registry) {
15971597
var idDefn = idProp && idProp.defaultFn;
15981598
if (idType !== String || !(idDefn === 'uuid' || idDefn === 'guid')) {
15991599
deprecated('The model ' + this.modelName + ' is tracking changes, ' +
1600-
'which requries a string id with GUID/UUID default value.');
1600+
'which requires a string id with GUID/UUID default value.');
16011601
}
16021602

16031603
Model.observe('after save', rectifyOnSave);
16041604

16051605
Model.observe('after delete', rectifyOnDelete);
16061606

1607-
if (runtime.isServer) {
1607+
// Only run if the run time is server
1608+
// Can switch off cleanup by setting the interval to -1
1609+
if (runtime.isServer && cleanupInterval > 0) {
16081610
// initial cleanup
16091611
cleanup();
16101612

test/replication.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var defineModelTestsWithDataSource = require('./util/model-tests');
1111
var PersistedModel = loopback.PersistedModel;
1212
var expect = require('chai').expect;
1313
var debug = require('debug')('test');
14+
var runtime = require('./../lib/runtime');
1415

1516
describe('Replication / Change APIs', function() {
1617
this.timeout(10000);
@@ -60,6 +61,77 @@ describe('Replication / Change APIs', function() {
6061
};
6162
});
6263

64+
describe('cleanup check for enableChangeTracking', function() {
65+
describe('when no changeCleanupInterval set', function() {
66+
it('should call rectifyAllChanges if running on server', function(done) {
67+
var calls = mockRectifyAllChanges(SourceModel);
68+
SourceModel.enableChangeTracking();
69+
70+
if (runtime.isServer) {
71+
expect(calls).to.eql(['rectifyAllChanges']);
72+
} else {
73+
expect(calls).to.eql([]);
74+
}
75+
76+
done();
77+
});
78+
});
79+
80+
describe('when changeCleanupInterval set to -1', function() {
81+
var Model;
82+
beforeEach(function() {
83+
Model = this.Model = PersistedModel.extend(
84+
'Model-' + tid,
85+
{id: {id: true, type: String, defaultFn: 'guid'}},
86+
{trackChanges: true, changeCleanupInterval: -1});
87+
88+
Model.attachTo(dataSource);
89+
});
90+
91+
it('should not call rectifyAllChanges', function(done) {
92+
var calls = mockRectifyAllChanges(Model);
93+
Model.enableChangeTracking();
94+
expect(calls).to.eql([]);
95+
done();
96+
});
97+
});
98+
99+
describe('when changeCleanupInterval set to 10000', function() {
100+
var Model;
101+
beforeEach(function() {
102+
Model = this.Model = PersistedModel.extend(
103+
'Model-' + tid,
104+
{id: {id: true, type: String, defaultFn: 'guid'}},
105+
{trackChanges: true, changeCleanupInterval: 10000});
106+
107+
Model.attachTo(dataSource);
108+
});
109+
110+
it('should call rectifyAllChanges if running on server', function(done) {
111+
var calls = mockRectifyAllChanges(Model);
112+
Model.enableChangeTracking();
113+
if (runtime.isServer) {
114+
expect(calls).to.eql(['rectifyAllChanges']);
115+
} else {
116+
expect(calls).to.eql([]);
117+
}
118+
119+
done();
120+
});
121+
});
122+
123+
function mockRectifyAllChanges(Model) {
124+
var calls = [];
125+
126+
Model.rectifyAllChanges = function(cb) {
127+
calls.push('rectifyAllChanges');
128+
process.nextTick(cb);
129+
};
130+
131+
return calls;
132+
}
133+
});
134+
63135
describe('optimization check rectifyChange Vs rectifyAllChanges', function() {
64136
beforeEach(function initialData(done) {
65137
var data = [{name: 'John', surname: 'Doe'}, {name: 'Jane', surname: 'Roe'}];

0 commit comments

Comments
 (0)