2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2014,2018. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2015-04-03 14:41:32 +00:00
|
|
|
var assert = require('assert');
|
2014-05-16 00:27:02 +00:00
|
|
|
var async = require('async');
|
|
|
|
var loopback = require('../');
|
|
|
|
var Change = loopback.Change;
|
|
|
|
var defineModelTestsWithDataSource = require('./util/model-tests');
|
2014-06-05 07:45:09 +00:00
|
|
|
var PersistedModel = loopback.PersistedModel;
|
2016-11-22 14:30:04 +00:00
|
|
|
var expect = require('./helpers/expect');
|
2015-03-05 12:13:21 +00:00
|
|
|
var debug = require('debug')('test');
|
2016-11-21 08:19:11 +00:00
|
|
|
var runtime = require('./../lib/runtime');
|
2014-05-16 00:27:02 +00:00
|
|
|
|
|
|
|
describe('Replication / Change APIs', function() {
|
2016-08-08 13:12:56 +00:00
|
|
|
this.timeout(10000);
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
var dataSource, SourceModel, TargetModel, useSinceFilter;
|
2015-03-03 10:23:13 +00:00
|
|
|
var tid = 0; // per-test unique id used e.g. to build unique model names
|
2015-02-20 18:28:33 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
beforeEach(function() {
|
2015-03-03 10:23:13 +00:00
|
|
|
tid++;
|
2015-03-05 13:29:30 +00:00
|
|
|
useSinceFilter = false;
|
2014-05-16 00:27:02 +00:00
|
|
|
var test = this;
|
2015-02-20 18:28:33 +00:00
|
|
|
dataSource = this.dataSource = loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2014-05-16 00:27:02 +00:00
|
|
|
});
|
2015-03-03 10:23:13 +00:00
|
|
|
SourceModel = this.SourceModel = PersistedModel.extend(
|
|
|
|
'SourceModel-' + tid,
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: {id: true, type: String, defaultFn: 'guid'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true}
|
|
|
|
);
|
2015-02-26 15:36:44 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
SourceModel.attachTo(dataSource);
|
2014-11-21 02:35:36 +00:00
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
TargetModel = this.TargetModel = PersistedModel.extend(
|
|
|
|
'TargetModel-' + tid,
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: {id: true, type: String, defaultFn: 'guid'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true}
|
|
|
|
);
|
2015-02-26 15:36:44 +00:00
|
|
|
|
2015-03-03 13:23:46 +00:00
|
|
|
// NOTE(bajtos) At the moment, all models share the same Checkpoint
|
|
|
|
// model. This causes the in-process replication to work differently
|
|
|
|
// than client-server replication.
|
|
|
|
// As a workaround, we manually setup unique Checkpoint for TargetModel.
|
|
|
|
var TargetChange = TargetModel.Change;
|
|
|
|
TargetChange.Checkpoint = loopback.Checkpoint.extend('TargetCheckpoint');
|
|
|
|
TargetChange.Checkpoint.attachTo(dataSource);
|
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
TargetModel.attachTo(dataSource);
|
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
test.startingCheckpoint = -1;
|
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
this.createInitalData = function(cb) {
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({name: 'foo'}, function(err, inst) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
test.model = inst;
|
2015-03-05 12:13:21 +00:00
|
|
|
SourceModel.replicate(TargetModel, cb);
|
2014-05-16 00:27:02 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-11-21 08:19:11 +00:00
|
|
|
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'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true, changeCleanupInterval: -1}
|
|
|
|
);
|
2016-11-21 08:19:11 +00:00
|
|
|
|
|
|
|
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'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true, changeCleanupInterval: 10000}
|
|
|
|
);
|
2016-11-21 08:19:11 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-12-17 21:40:19 +00:00
|
|
|
describe('optimization check rectifyChange Vs rectifyAllChanges', function() {
|
|
|
|
beforeEach(function initialData(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
var data = [{name: 'John', surname: 'Doe'}, {name: 'Jane', surname: 'Roe'}];
|
2015-12-17 21:40:19 +00:00
|
|
|
async.waterfall([
|
|
|
|
function(callback) {
|
|
|
|
SourceModel.create(data, callback);
|
|
|
|
},
|
|
|
|
function(data, callback) {
|
|
|
|
SourceModel.replicate(TargetModel, callback);
|
|
|
|
}], function(err, result) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call rectifyAllChanges if no id is passed for rectifyOnDelete', function(done) {
|
2016-02-05 08:21:25 +00:00
|
|
|
var calls = mockSourceModelRectify();
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.destroyAll({name: 'John'}, function(err, data) {
|
2016-02-05 08:21:25 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-02-05 08:21:25 +00:00
|
|
|
expect(calls).to.eql(['rectifyAllChanges']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-02-05 08:21:25 +00:00
|
|
|
done();
|
2015-12-17 21:40:19 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call rectifyAllChanges if no id is passed for rectifyOnSave', function(done) {
|
2016-02-05 08:21:25 +00:00
|
|
|
var calls = mockSourceModelRectify();
|
2016-11-15 21:46:23 +00:00
|
|
|
var newData = {'name': 'Janie'};
|
|
|
|
SourceModel.update({name: 'Jane'}, newData, function(err, data) {
|
2016-02-05 08:21:25 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-02-05 08:21:25 +00:00
|
|
|
expect(calls).to.eql(['rectifyAllChanges']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-02-05 08:21:25 +00:00
|
|
|
done();
|
2015-12-17 21:40:19 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
it('rectifyOnDelete for Delete should call rectifyChange instead of rectifyAllChanges',
|
2017-12-12 08:33:15 +00:00
|
|
|
function(done) {
|
|
|
|
var calls = mockTargetModelRectify();
|
|
|
|
async.waterfall([
|
|
|
|
function(callback) {
|
|
|
|
SourceModel.destroyAll({name: 'John'}, callback);
|
|
|
|
},
|
|
|
|
function(data, callback) {
|
|
|
|
SourceModel.replicate(TargetModel, callback);
|
2015-12-17 21:40:19 +00:00
|
|
|
// replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation
|
2017-12-12 08:33:15 +00:00
|
|
|
},
|
|
|
|
], function(err, results) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
expect(calls).to.eql(['rectifyChange']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-12-17 21:40:19 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
it('rectifyOnSave for Update should call rectifyChange instead of rectifyAllChanges',
|
2017-12-12 08:33:15 +00:00
|
|
|
function(done) {
|
|
|
|
var calls = mockTargetModelRectify();
|
|
|
|
var newData = {'name': 'Janie'};
|
|
|
|
async.waterfall([
|
|
|
|
function(callback) {
|
|
|
|
SourceModel.update({name: 'Jane'}, newData, callback);
|
|
|
|
},
|
|
|
|
function(data, callback) {
|
|
|
|
SourceModel.replicate(TargetModel, callback);
|
2015-12-17 21:40:19 +00:00
|
|
|
// replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation
|
2017-12-12 08:33:15 +00:00
|
|
|
},
|
|
|
|
], function(err, result) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
expect(calls).to.eql(['rectifyChange']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-12-17 21:40:19 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
it('rectifyOnSave for Create should call rectifyChange instead of rectifyAllChanges',
|
2017-12-12 08:33:15 +00:00
|
|
|
function(done) {
|
|
|
|
var calls = mockTargetModelRectify();
|
|
|
|
var newData = [{name: 'Janie', surname: 'Doe'}];
|
|
|
|
async.waterfall([
|
|
|
|
function(callback) {
|
|
|
|
SourceModel.create(newData, callback);
|
|
|
|
},
|
|
|
|
function(data, callback) {
|
|
|
|
SourceModel.replicate(TargetModel, callback);
|
2015-12-17 21:40:19 +00:00
|
|
|
// replicate should call `rectifyOnSave` and then `rectifyChange` not `rectifyAllChanges` through `after save` operation
|
2017-12-12 08:33:15 +00:00
|
|
|
},
|
|
|
|
], function(err, result) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
expect(calls).to.eql(['rectifyChange']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-12-17 21:40:19 +00:00
|
|
|
});
|
2016-02-05 08:21:25 +00:00
|
|
|
|
|
|
|
function mockSourceModelRectify() {
|
|
|
|
var calls = [];
|
|
|
|
|
|
|
|
SourceModel.rectifyChange = function(id, cb) {
|
|
|
|
calls.push('rectifyChange');
|
|
|
|
process.nextTick(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
SourceModel.rectifyAllChanges = function(cb) {
|
|
|
|
calls.push('rectifyAllChanges');
|
|
|
|
process.nextTick(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
return calls;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mockTargetModelRectify() {
|
|
|
|
var calls = [];
|
|
|
|
|
|
|
|
TargetModel.rectifyChange = function(id, cb) {
|
|
|
|
calls.push('rectifyChange');
|
|
|
|
process.nextTick(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
TargetModel.rectifyAllChanges = function(cb) {
|
|
|
|
calls.push('rectifyAllChanges');
|
|
|
|
process.nextTick(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
return calls;
|
|
|
|
}
|
2015-12-17 21:40:19 +00:00
|
|
|
});
|
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
describe('Model.changes(since, filter, callback)', function() {
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Get changes since the given checkpoint', function(done) {
|
2014-05-16 00:27:02 +00:00
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
this.SourceModel.create({name: 'foo'}, function(err) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
test.SourceModel.changes(test.startingCheckpoint, {}, function(err, changes) {
|
|
|
|
assert.equal(changes.length, 1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, 1);
|
|
|
|
});
|
|
|
|
});
|
2015-02-20 18:28:33 +00:00
|
|
|
|
|
|
|
it('excludes changes from older checkpoints', function(done) {
|
|
|
|
var FUTURE_CHECKPOINT = 999;
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({name: 'foo'}, function(err) {
|
2015-02-20 18:28:33 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
SourceModel.changes(FUTURE_CHECKPOINT, {}, function(err, changes) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(changes).to.be.empty();
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-16 00:27:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.replicate(since, targetModel, options, callback)', function() {
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Replicate data using the target model', function(done) {
|
2014-05-16 00:27:02 +00:00
|
|
|
var test = this;
|
|
|
|
var options = {};
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
this.SourceModel.create({name: 'foo'}, function(err) {
|
2015-03-12 09:47:11 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
test.SourceModel.replicate(test.startingCheckpoint, test.TargetModel,
|
2017-12-12 08:33:15 +00:00
|
|
|
options, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2014-05-16 00:27:02 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
assertTargetModelEqualsSourceModel(conflicts, test.SourceModel,
|
|
|
|
test.TargetModel, done);
|
|
|
|
});
|
2015-03-12 09:47:11 +00:00
|
|
|
});
|
2014-05-16 00:27:02 +00:00
|
|
|
});
|
2015-03-03 10:23:13 +00:00
|
|
|
|
2015-07-20 12:00:05 +00:00
|
|
|
it('Replicate data using the target model - promise variant', function(done) {
|
|
|
|
var test = this;
|
|
|
|
var options = {};
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
this.SourceModel.create({name: 'foo'}, function(err) {
|
2015-07-20 12:00:05 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-20 12:00:05 +00:00
|
|
|
test.SourceModel.replicate(test.startingCheckpoint, test.TargetModel,
|
2017-12-12 08:33:15 +00:00
|
|
|
options)
|
2015-07-20 12:00:05 +00:00
|
|
|
.then(function(conflicts) {
|
|
|
|
assertTargetModelEqualsSourceModel(conflicts, test.SourceModel,
|
|
|
|
test.TargetModel, done);
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
it('applies "since" filter on source changes', function(done) {
|
|
|
|
async.series([
|
|
|
|
function createModelInSourceCp1(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({id: '1'}, next);
|
2015-03-03 10:23:13 +00:00
|
|
|
},
|
|
|
|
function checkpoint(next) {
|
|
|
|
SourceModel.checkpoint(next);
|
|
|
|
},
|
|
|
|
function createModelInSourceCp2(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({id: '2'}, next);
|
2015-03-03 10:23:13 +00:00
|
|
|
},
|
|
|
|
function replicateLastChangeOnly(next) {
|
|
|
|
SourceModel.currentCheckpoint(function(err, cp) {
|
|
|
|
if (err) return done(err);
|
|
|
|
SourceModel.replicate(cp, TargetModel, next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function verify(next) {
|
|
|
|
TargetModel.find(function(err, list) {
|
|
|
|
if (err) return done(err);
|
|
|
|
// '1' should be skipped by replication
|
2015-03-03 10:50:06 +00:00
|
|
|
expect(getIds(list)).to.eql(['2']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-03-03 10:23:13 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
2015-07-20 12:00:05 +00:00
|
|
|
it('applies "since" filter on source changes - promise variant', function(done) {
|
|
|
|
async.series([
|
|
|
|
function createModelInSourceCp1(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({id: '1'}, next);
|
2015-07-20 12:00:05 +00:00
|
|
|
},
|
|
|
|
function checkpoint(next) {
|
|
|
|
SourceModel.checkpoint(next);
|
|
|
|
},
|
|
|
|
function createModelInSourceCp2(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({id: '2'}, next);
|
2015-07-20 12:00:05 +00:00
|
|
|
},
|
|
|
|
function replicateLastChangeOnly(next) {
|
|
|
|
SourceModel.currentCheckpoint(function(err, cp) {
|
|
|
|
if (err) return done(err);
|
|
|
|
SourceModel.replicate(cp, TargetModel, {})
|
|
|
|
.then(function(next) {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function verify(next) {
|
|
|
|
TargetModel.find(function(err, list) {
|
|
|
|
if (err) return done(err);
|
|
|
|
// '1' should be skipped by replication
|
|
|
|
expect(getIds(list)).to.eql(['2']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-20 12:00:05 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-07-20 12:00:05 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
it('applies "since" filter on target changes', function(done) {
|
|
|
|
// Because the "since" filter is just an optimization,
|
|
|
|
// there isn't really any observable behaviour we could
|
|
|
|
// check to assert correct implementation.
|
|
|
|
var diffSince = [];
|
|
|
|
spyAndStoreSinceArg(TargetModel, 'diff', diffSince);
|
|
|
|
|
|
|
|
SourceModel.replicate(10, TargetModel, function(err) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
expect(diffSince).to.eql([10]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-20 12:00:05 +00:00
|
|
|
it('applies "since" filter on target changes - promise variant', function(done) {
|
|
|
|
// Because the "since" filter is just an optimization,
|
|
|
|
// there isn't really any observable behaviour we could
|
|
|
|
// check to assert correct implementation.
|
|
|
|
var diffSince = [];
|
|
|
|
spyAndStoreSinceArg(TargetModel, 'diff', diffSince);
|
|
|
|
|
|
|
|
SourceModel.replicate(10, TargetModel, {})
|
|
|
|
.then(function() {
|
|
|
|
expect(diffSince).to.eql([10]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-20 12:00:05 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
it('uses different "since" value for source and target', function(done) {
|
|
|
|
var sourceSince = [];
|
|
|
|
var targetSince = [];
|
|
|
|
|
|
|
|
spyAndStoreSinceArg(SourceModel, 'changes', sourceSince);
|
|
|
|
spyAndStoreSinceArg(TargetModel, 'diff', targetSince);
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
var since = {source: 1, target: 2};
|
2015-03-03 10:23:13 +00:00
|
|
|
SourceModel.replicate(since, TargetModel, function(err) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
expect(sourceSince).to.eql([1]);
|
|
|
|
expect(targetSince).to.eql([2]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-03 10:50:06 +00:00
|
|
|
|
2015-07-20 12:00:05 +00:00
|
|
|
it('uses different "since" value for source and target - promise variant', function(done) {
|
|
|
|
var sourceSince = [];
|
|
|
|
var targetSince = [];
|
|
|
|
|
|
|
|
spyAndStoreSinceArg(SourceModel, 'changes', sourceSince);
|
|
|
|
spyAndStoreSinceArg(TargetModel, 'diff', targetSince);
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
var since = {source: 1, target: 2};
|
2015-07-20 12:00:05 +00:00
|
|
|
SourceModel.replicate(since, TargetModel, {})
|
|
|
|
.then(function() {
|
|
|
|
expect(sourceSince).to.eql([1]);
|
|
|
|
expect(targetSince).to.eql([2]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-20 12:00:05 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-03 10:50:06 +00:00
|
|
|
it('picks up changes made during replication', function(done) {
|
2015-03-16 12:38:37 +00:00
|
|
|
setupRaceConditionInReplication(function(cb) {
|
2015-03-03 10:50:06 +00:00
|
|
|
// simulate the situation when another model is created
|
|
|
|
// while a replication run is in progress
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({id: 'racer'}, cb);
|
2015-03-16 12:38:37 +00:00
|
|
|
});
|
2015-03-03 10:50:06 +00:00
|
|
|
|
|
|
|
var lastCp;
|
|
|
|
async.series([
|
|
|
|
function buildSomeDataToReplicate(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({id: 'init'}, next);
|
2015-03-03 10:50:06 +00:00
|
|
|
},
|
|
|
|
function getLastCp(next) {
|
|
|
|
SourceModel.currentCheckpoint(function(err, cp) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 10:50:06 +00:00
|
|
|
lastCp = cp;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 10:50:06 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function replicate(next) {
|
|
|
|
SourceModel.replicate(TargetModel, next);
|
|
|
|
},
|
|
|
|
function verifyAssumptions(next) {
|
|
|
|
SourceModel.find(function(err, list) {
|
|
|
|
expect(getIds(list), 'source ids')
|
|
|
|
.to.eql(['init', 'racer']);
|
|
|
|
|
|
|
|
TargetModel.find(function(err, list) {
|
|
|
|
expect(getIds(list), 'target ids after first sync')
|
2015-03-05 12:13:21 +00:00
|
|
|
.to.include.members(['init']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 10:50:06 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function replicateAgain(next) {
|
|
|
|
SourceModel.replicate(lastCp + 1, TargetModel, next);
|
|
|
|
},
|
|
|
|
function verify(next) {
|
|
|
|
TargetModel.find(function(err, list) {
|
|
|
|
expect(getIds(list), 'target ids').to.eql(['init', 'racer']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 10:50:06 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-03-03 10:50:06 +00:00
|
|
|
], done);
|
|
|
|
});
|
2015-03-03 13:23:46 +00:00
|
|
|
|
|
|
|
it('returns new current checkpoints to callback', function(done) {
|
|
|
|
var sourceCp, targetCp;
|
|
|
|
async.series([
|
|
|
|
bumpSourceCheckpoint,
|
|
|
|
bumpTargetCheckpoint,
|
|
|
|
bumpTargetCheckpoint,
|
|
|
|
function replicate(cb) {
|
|
|
|
expect(sourceCp).to.not.equal(targetCp);
|
|
|
|
|
|
|
|
SourceModel.replicate(
|
|
|
|
TargetModel,
|
|
|
|
function(err, conflicts, newCheckpoints) {
|
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 13:23:46 +00:00
|
|
|
expect(conflicts, 'conflicts').to.eql([]);
|
|
|
|
expect(newCheckpoints, 'currentCheckpoints').to.eql({
|
|
|
|
source: sourceCp + 1,
|
2016-04-01 09:14:26 +00:00
|
|
|
target: targetCp + 1,
|
2015-03-03 13:23:46 +00:00
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 13:23:46 +00:00
|
|
|
cb();
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-03-03 13:23:46 +00:00
|
|
|
], done);
|
|
|
|
|
|
|
|
function bumpSourceCheckpoint(cb) {
|
|
|
|
SourceModel.checkpoint(function(err, inst) {
|
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 13:23:46 +00:00
|
|
|
sourceCp = inst.seq;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 13:23:46 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function bumpTargetCheckpoint(cb) {
|
|
|
|
TargetModel.checkpoint(function(err, inst) {
|
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 13:23:46 +00:00
|
|
|
targetCp = inst.seq;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 13:23:46 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
it('leaves current target checkpoint empty', function(done) {
|
|
|
|
async.series([
|
|
|
|
function createTestData(next) {
|
|
|
|
SourceModel.create({}, next);
|
|
|
|
},
|
|
|
|
replicateExpectingSuccess(),
|
|
|
|
function verify(next) {
|
|
|
|
TargetModel.currentCheckpoint(function(err, cp) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
TargetModel.getChangeModel().find(
|
2016-11-15 21:46:23 +00:00
|
|
|
{where: {checkpoint: {gte: cp}}},
|
2015-03-05 12:13:21 +00:00
|
|
|
function(err, changes) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-06 10:22:30 +00:00
|
|
|
expect(changes).to.have.length(0);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
done();
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2015-03-05 12:13:21 +00:00
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-03-05 12:13:21 +00:00
|
|
|
], done);
|
|
|
|
});
|
2015-03-16 12:38:37 +00:00
|
|
|
|
|
|
|
describe('with 3rd-party changes', function() {
|
|
|
|
it('detects UPDATE made during UPDATE', function(done) {
|
|
|
|
async.series([
|
2016-11-15 21:46:23 +00:00
|
|
|
createModel(SourceModel, {id: '1'}),
|
2015-03-16 12:38:37 +00:00
|
|
|
replicateExpectingSuccess(),
|
|
|
|
function updateModel(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.updateAll({id: '1'}, {name: 'source'}, next);
|
2015-03-16 12:38:37 +00:00
|
|
|
},
|
|
|
|
function replicateWith3rdPartyModifyingData(next) {
|
|
|
|
setupRaceConditionInReplication(function(cb) {
|
2015-05-13 19:52:31 +00:00
|
|
|
var connector = TargetModel.dataSource.connector;
|
|
|
|
if (connector.updateAttributes.length <= 4) {
|
|
|
|
connector.updateAttributes(
|
|
|
|
TargetModel.modelName,
|
|
|
|
'1',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: '3rd-party'},
|
2018-08-08 15:22:20 +00:00
|
|
|
cb
|
|
|
|
);
|
2015-05-13 19:52:31 +00:00
|
|
|
} else {
|
|
|
|
// 2.x connectors require `options`
|
|
|
|
connector.updateAttributes(
|
|
|
|
TargetModel.modelName,
|
|
|
|
'1',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: '3rd-party'},
|
2015-05-13 19:52:31 +00:00
|
|
|
{}, // options
|
2018-08-08 15:22:20 +00:00
|
|
|
cb
|
|
|
|
);
|
2015-05-13 19:52:31 +00:00
|
|
|
}
|
2015-03-16 12:38:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
SourceModel.replicate(
|
|
|
|
TargetModel,
|
|
|
|
function(err, conflicts, cps, updates) {
|
|
|
|
if (err) return next(err);
|
|
|
|
|
|
|
|
var conflictedIds = getPropValue(conflicts || [], 'modelId');
|
|
|
|
expect(conflictedIds).to.eql(['1']);
|
|
|
|
|
|
|
|
// resolve the conflict using ours
|
|
|
|
conflicts[0].resolve(next);
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2015-03-16 12:38:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
replicateExpectingSuccess(),
|
2016-04-01 09:14:26 +00:00
|
|
|
verifyInstanceWasReplicated(SourceModel, TargetModel, '1'),
|
2015-03-16 12:38:37 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('detects CREATE made during CREATE', function(done) {
|
|
|
|
async.series([
|
|
|
|
// FIXME(bajtos) Remove the 'name' property once the implementation
|
|
|
|
// of UPDATE is fixed to correctly remove properties
|
2016-11-15 21:46:23 +00:00
|
|
|
createModel(SourceModel, {id: '1', name: 'source'}),
|
2015-03-16 12:38:37 +00:00
|
|
|
function replicateWith3rdPartyModifyingData(next) {
|
2015-05-13 19:52:31 +00:00
|
|
|
var connector = TargetModel.dataSource.connector;
|
2015-03-16 12:38:37 +00:00
|
|
|
setupRaceConditionInReplication(function(cb) {
|
2015-05-13 19:52:31 +00:00
|
|
|
if (connector.create.length <= 3) {
|
|
|
|
connector.create(
|
|
|
|
TargetModel.modelName,
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: '1', name: '3rd-party'},
|
2018-08-08 15:22:20 +00:00
|
|
|
cb
|
|
|
|
);
|
2015-05-13 19:52:31 +00:00
|
|
|
} else {
|
|
|
|
// 2.x connectors require `options`
|
|
|
|
connector.create(
|
|
|
|
TargetModel.modelName,
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: '1', name: '3rd-party'},
|
2015-05-13 19:52:31 +00:00
|
|
|
{}, // options
|
2018-08-08 15:22:20 +00:00
|
|
|
cb
|
|
|
|
);
|
2015-05-13 19:52:31 +00:00
|
|
|
}
|
2015-03-16 12:38:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
SourceModel.replicate(
|
|
|
|
TargetModel,
|
|
|
|
function(err, conflicts, cps, updates) {
|
|
|
|
if (err) return next(err);
|
|
|
|
|
|
|
|
var conflictedIds = getPropValue(conflicts || [], 'modelId');
|
|
|
|
expect(conflictedIds).to.eql(['1']);
|
|
|
|
|
|
|
|
// resolve the conflict using ours
|
|
|
|
conflicts[0].resolve(next);
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2015-03-16 12:38:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
replicateExpectingSuccess(),
|
2016-04-01 09:14:26 +00:00
|
|
|
verifyInstanceWasReplicated(SourceModel, TargetModel, '1'),
|
2015-03-16 12:38:37 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('detects UPDATE made during DELETE', function(done) {
|
|
|
|
async.series([
|
2016-11-15 21:46:23 +00:00
|
|
|
createModel(SourceModel, {id: '1'}),
|
2015-03-16 12:38:37 +00:00
|
|
|
replicateExpectingSuccess(),
|
|
|
|
function deleteModel(next) {
|
|
|
|
SourceModel.deleteById('1', next);
|
|
|
|
},
|
|
|
|
function replicateWith3rdPartyModifyingData(next) {
|
|
|
|
setupRaceConditionInReplication(function(cb) {
|
2015-05-13 19:52:31 +00:00
|
|
|
var connector = TargetModel.dataSource.connector;
|
|
|
|
if (connector.updateAttributes.length <= 4) {
|
|
|
|
connector.updateAttributes(
|
|
|
|
TargetModel.modelName,
|
|
|
|
'1',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: '3rd-party'},
|
2018-08-08 15:22:20 +00:00
|
|
|
cb
|
|
|
|
);
|
2015-05-13 19:52:31 +00:00
|
|
|
} else {
|
|
|
|
// 2.x connectors require `options`
|
|
|
|
connector.updateAttributes(
|
|
|
|
TargetModel.modelName,
|
|
|
|
'1',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: '3rd-party'},
|
2015-05-13 19:52:31 +00:00
|
|
|
{}, // options
|
2018-08-08 15:22:20 +00:00
|
|
|
cb
|
|
|
|
);
|
2015-05-13 19:52:31 +00:00
|
|
|
}
|
2015-03-16 12:38:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
SourceModel.replicate(
|
|
|
|
TargetModel,
|
|
|
|
function(err, conflicts, cps, updates) {
|
|
|
|
if (err) return next(err);
|
|
|
|
|
|
|
|
var conflictedIds = getPropValue(conflicts || [], 'modelId');
|
|
|
|
expect(conflictedIds).to.eql(['1']);
|
|
|
|
|
|
|
|
// resolve the conflict using ours
|
|
|
|
conflicts[0].resolve(next);
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2015-03-16 12:38:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
replicateExpectingSuccess(),
|
2016-04-01 09:14:26 +00:00
|
|
|
verifyInstanceWasReplicated(SourceModel, TargetModel, '1'),
|
2015-03-16 12:38:37 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles DELETE made during DELETE', function(done) {
|
|
|
|
async.series([
|
2016-11-15 21:46:23 +00:00
|
|
|
createModel(SourceModel, {id: '1'}),
|
2015-03-16 12:38:37 +00:00
|
|
|
replicateExpectingSuccess(),
|
|
|
|
function deleteModel(next) {
|
|
|
|
SourceModel.deleteById('1', next);
|
|
|
|
},
|
|
|
|
function setup3rdPartyModifyingData(next) {
|
2015-05-13 19:52:31 +00:00
|
|
|
var connector = TargetModel.dataSource.connector;
|
2015-03-16 12:38:37 +00:00
|
|
|
setupRaceConditionInReplication(function(cb) {
|
2015-05-13 19:52:31 +00:00
|
|
|
if (connector.destroy.length <= 3) {
|
|
|
|
connector.destroy(
|
|
|
|
TargetModel.modelName,
|
|
|
|
'1',
|
2018-08-08 15:22:20 +00:00
|
|
|
cb
|
|
|
|
);
|
2015-05-13 19:52:31 +00:00
|
|
|
} else {
|
|
|
|
// 2.x connectors require `options`
|
|
|
|
connector.destroy(
|
|
|
|
TargetModel.modelName,
|
|
|
|
'1',
|
|
|
|
{}, // options
|
2018-08-08 15:22:20 +00:00
|
|
|
cb
|
|
|
|
);
|
2015-05-13 19:52:31 +00:00
|
|
|
}
|
2015-03-16 12:38:37 +00:00
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
next();
|
|
|
|
},
|
|
|
|
replicateExpectingSuccess(),
|
2016-04-01 09:14:26 +00:00
|
|
|
verifyInstanceWasReplicated(SourceModel, TargetModel, '1'),
|
2015-03-16 12:38:37 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
});
|
2014-05-16 00:27:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('conflict detection - both updated', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
var SourceModel = this.SourceModel;
|
|
|
|
var TargetModel = this.TargetModel;
|
|
|
|
var test = this;
|
|
|
|
|
|
|
|
test.createInitalData(createConflict);
|
|
|
|
|
|
|
|
function createConflict(err, conflicts) {
|
|
|
|
async.parallel([
|
|
|
|
function(cb) {
|
|
|
|
SourceModel.findOne(function(err, inst) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return cb(err);
|
2014-05-16 00:27:02 +00:00
|
|
|
inst.name = 'source update';
|
|
|
|
inst.save(cb);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(cb) {
|
|
|
|
TargetModel.findOne(function(err, inst) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return cb(err);
|
2014-05-16 00:27:02 +00:00
|
|
|
inst.name = 'target update';
|
|
|
|
inst.save(cb);
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2014-05-16 00:27:02 +00:00
|
|
|
], function(err) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
SourceModel.replicate(TargetModel, function(err, conflicts) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
test.conflicts = conflicts;
|
|
|
|
test.conflict = conflicts[0];
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it('should detect a single conflict', function() {
|
|
|
|
assert.equal(this.conflicts.length, 1);
|
|
|
|
assert(this.conflict);
|
|
|
|
});
|
|
|
|
it('type should be UPDATE', function(done) {
|
|
|
|
this.conflict.type(function(err, type) {
|
|
|
|
assert.equal(type, Change.UPDATE);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('conflict.changes()', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.conflict.changes(function(err, sourceChange, targetChange) {
|
|
|
|
assert.equal(typeof sourceChange.id, 'string');
|
|
|
|
assert.equal(typeof targetChange.id, 'string');
|
|
|
|
assert.equal(test.model.getId(), sourceChange.getModelId());
|
|
|
|
assert.equal(sourceChange.type(), Change.UPDATE);
|
|
|
|
assert.equal(targetChange.type(), Change.UPDATE);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('conflict.models()', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.conflict.models(function(err, source, target) {
|
|
|
|
assert.deepEqual(source.toJSON(), {
|
2015-02-26 15:36:44 +00:00
|
|
|
id: test.model.id,
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'source update',
|
2014-05-16 00:27:02 +00:00
|
|
|
});
|
|
|
|
assert.deepEqual(target.toJSON(), {
|
2015-02-26 15:36:44 +00:00
|
|
|
id: test.model.id,
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'target update',
|
2014-05-16 00:27:02 +00:00
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('conflict detection - source deleted', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
var SourceModel = this.SourceModel;
|
|
|
|
var TargetModel = this.TargetModel;
|
|
|
|
var test = this;
|
|
|
|
|
|
|
|
test.createInitalData(createConflict);
|
|
|
|
|
|
|
|
function createConflict() {
|
|
|
|
async.parallel([
|
|
|
|
function(cb) {
|
|
|
|
SourceModel.findOne(function(err, inst) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
test.model = inst;
|
|
|
|
inst.remove(cb);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(cb) {
|
|
|
|
TargetModel.findOne(function(err, inst) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
inst.name = 'target update';
|
|
|
|
inst.save(cb);
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2014-05-16 00:27:02 +00:00
|
|
|
], function(err) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
SourceModel.replicate(TargetModel, function(err, conflicts) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
test.conflicts = conflicts;
|
|
|
|
test.conflict = conflicts[0];
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it('should detect a single conflict', function() {
|
|
|
|
assert.equal(this.conflicts.length, 1);
|
|
|
|
assert(this.conflict);
|
|
|
|
});
|
|
|
|
it('type should be DELETE', function(done) {
|
|
|
|
this.conflict.type(function(err, type) {
|
|
|
|
assert.equal(type, Change.DELETE);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('conflict.changes()', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.conflict.changes(function(err, sourceChange, targetChange) {
|
|
|
|
assert.equal(typeof sourceChange.id, 'string');
|
|
|
|
assert.equal(typeof targetChange.id, 'string');
|
|
|
|
assert.equal(test.model.getId(), sourceChange.getModelId());
|
|
|
|
assert.equal(sourceChange.type(), Change.DELETE);
|
|
|
|
assert.equal(targetChange.type(), Change.UPDATE);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('conflict.models()', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.conflict.models(function(err, source, target) {
|
|
|
|
assert.equal(source, null);
|
|
|
|
assert.deepEqual(target.toJSON(), {
|
2015-02-26 15:36:44 +00:00
|
|
|
id: test.model.id,
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'target update',
|
2014-05-16 00:27:02 +00:00
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('conflict detection - target deleted', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
var SourceModel = this.SourceModel;
|
|
|
|
var TargetModel = this.TargetModel;
|
|
|
|
var test = this;
|
|
|
|
|
|
|
|
test.createInitalData(createConflict);
|
|
|
|
|
|
|
|
function createConflict() {
|
|
|
|
async.parallel([
|
|
|
|
function(cb) {
|
|
|
|
SourceModel.findOne(function(err, inst) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return cb(err);
|
2014-05-16 00:27:02 +00:00
|
|
|
test.model = inst;
|
|
|
|
inst.name = 'source update';
|
|
|
|
inst.save(cb);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(cb) {
|
|
|
|
TargetModel.findOne(function(err, inst) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
inst.remove(cb);
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2014-05-16 00:27:02 +00:00
|
|
|
], function(err) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
SourceModel.replicate(TargetModel, function(err, conflicts) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
test.conflicts = conflicts;
|
|
|
|
test.conflict = conflicts[0];
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it('should detect a single conflict', function() {
|
|
|
|
assert.equal(this.conflicts.length, 1);
|
|
|
|
assert(this.conflict);
|
|
|
|
});
|
|
|
|
it('type should be DELETE', function(done) {
|
|
|
|
this.conflict.type(function(err, type) {
|
|
|
|
assert.equal(type, Change.DELETE);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('conflict.changes()', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.conflict.changes(function(err, sourceChange, targetChange) {
|
|
|
|
assert.equal(typeof sourceChange.id, 'string');
|
|
|
|
assert.equal(typeof targetChange.id, 'string');
|
|
|
|
assert.equal(test.model.getId(), sourceChange.getModelId());
|
|
|
|
assert.equal(sourceChange.type(), Change.UPDATE);
|
|
|
|
assert.equal(targetChange.type(), Change.DELETE);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('conflict.models()', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.conflict.models(function(err, source, target) {
|
|
|
|
assert.equal(target, null);
|
|
|
|
assert.deepEqual(source.toJSON(), {
|
2015-02-26 15:36:44 +00:00
|
|
|
id: test.model.id,
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'source update',
|
2014-05-16 00:27:02 +00:00
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('conflict detection - both deleted', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
var SourceModel = this.SourceModel;
|
|
|
|
var TargetModel = this.TargetModel;
|
|
|
|
var test = this;
|
|
|
|
|
|
|
|
test.createInitalData(createConflict);
|
|
|
|
|
|
|
|
function createConflict() {
|
|
|
|
async.parallel([
|
|
|
|
function(cb) {
|
|
|
|
SourceModel.findOne(function(err, inst) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
test.model = inst;
|
|
|
|
inst.remove(cb);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(cb) {
|
|
|
|
TargetModel.findOne(function(err, inst) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
inst.remove(cb);
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2014-05-16 00:27:02 +00:00
|
|
|
], function(err) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
SourceModel.replicate(TargetModel, function(err, conflicts) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
test.conflicts = conflicts;
|
|
|
|
test.conflict = conflicts[0];
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it('should not detect a conflict', function() {
|
|
|
|
assert.equal(this.conflicts.length, 0);
|
|
|
|
assert(!this.conflict);
|
|
|
|
});
|
|
|
|
});
|
2015-02-20 18:28:33 +00:00
|
|
|
|
|
|
|
describe('change detection', function() {
|
|
|
|
it('detects "create"', function(done) {
|
|
|
|
SourceModel.create({}, function(err, inst) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
assertChangeRecordedForId(inst.id, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('detects "updateOrCreate"', function(done) {
|
|
|
|
givenReplicatedInstance(function(err, created) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
var data = created.toObject();
|
|
|
|
created.name = 'updated';
|
|
|
|
SourceModel.updateOrCreate(created, function(err, inst) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
assertChangeRecordedForId(inst.id, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-09-02 12:40:47 +00:00
|
|
|
it('detects "upsertWithWhere"', function(done) {
|
|
|
|
givenReplicatedInstance(function(err, inst) {
|
|
|
|
if (err) return done(err);
|
|
|
|
SourceModel.upsertWithWhere(
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: inst.name},
|
|
|
|
{name: 'updated'},
|
2016-09-02 12:40:47 +00:00
|
|
|
function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assertChangeRecordedForId(inst.id, done);
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2016-09-02 12:40:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
it('detects "findOrCreate"', function(done) {
|
|
|
|
// make sure we bypass find+create and call the connector directly
|
|
|
|
SourceModel.dataSource.connector.findOrCreate =
|
|
|
|
function(model, query, data, callback) {
|
2015-05-13 19:52:31 +00:00
|
|
|
if (this.all.length <= 3) {
|
|
|
|
this.all(model, query, function(err, list) {
|
|
|
|
if (err || (list && list[0]))
|
|
|
|
return callback(err, list && list[0], false);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-13 19:52:31 +00:00
|
|
|
this.create(model, data, function(err) {
|
|
|
|
callback(err, data, true);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
|
|
|
// 2.x connectors requires `options`
|
|
|
|
this.all(model, query, {}, function(err, list) {
|
|
|
|
if (err || (list && list[0]))
|
|
|
|
return callback(err, list && list[0], false);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-13 19:52:31 +00:00
|
|
|
this.create(model, data, {}, function(err) {
|
|
|
|
callback(err, data, true);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2015-02-20 18:28:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SourceModel.findOrCreate(
|
2016-11-15 21:46:23 +00:00
|
|
|
{where: {name: 'does-not-exist'}},
|
|
|
|
{name: 'created'},
|
2015-02-20 18:28:33 +00:00
|
|
|
function(err, inst) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
assertChangeRecordedForId(inst.id, done);
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2015-02-20 18:28:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('detects "deleteById"', function(done) {
|
|
|
|
givenReplicatedInstance(function(err, inst) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
SourceModel.deleteById(inst.id, function(err) {
|
|
|
|
assertChangeRecordedForId(inst.id, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('detects "deleteAll"', function(done) {
|
|
|
|
givenReplicatedInstance(function(err, inst) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.deleteAll({name: inst.name}, function(err) {
|
2015-02-20 18:28:33 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
assertChangeRecordedForId(inst.id, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('detects "updateAll"', function(done) {
|
|
|
|
givenReplicatedInstance(function(err, inst) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
SourceModel.updateAll(
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: inst.name},
|
|
|
|
{name: 'updated'},
|
2015-02-20 18:28:33 +00:00
|
|
|
function(err) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
assertChangeRecordedForId(inst.id, done);
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2015-02-20 18:28:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('detects "prototype.save"', function(done) {
|
|
|
|
givenReplicatedInstance(function(err, inst) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
inst.name = 'updated';
|
|
|
|
inst.save(function(err) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
assertChangeRecordedForId(inst.id, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('detects "prototype.updateAttributes"', function(done) {
|
|
|
|
givenReplicatedInstance(function(err, inst) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
inst.updateAttributes({name: 'updated'}, function(err) {
|
2015-02-20 18:28:33 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
assertChangeRecordedForId(inst.id, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('detects "prototype.delete"', function(done) {
|
|
|
|
givenReplicatedInstance(function(err, inst) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
inst.delete(function(err) {
|
|
|
|
assertChangeRecordedForId(inst.id, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function givenReplicatedInstance(cb) {
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({name: 'a-name'}, function(err, inst) {
|
2015-02-20 18:28:33 +00:00
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
SourceModel.checkpoint(function(err) {
|
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
cb(null, inst);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertChangeRecordedForId(id, cb) {
|
|
|
|
SourceModel.getChangeModel().getCheckpointModel()
|
|
|
|
.current(function(err, cp) {
|
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
SourceModel.changes(cp - 1, {}, function(err, pendingChanges) {
|
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
expect(pendingChanges, 'list of changes').to.have.length(1);
|
|
|
|
var change = pendingChanges[0].toObject();
|
|
|
|
expect(change).to.have.property('checkpoint', cp); // sanity check
|
|
|
|
expect(change).to.have.property('modelName', SourceModel.modelName);
|
|
|
|
// NOTE(bajtos) Change.modelId is always String
|
|
|
|
// regardless of the type of the changed model's id property
|
|
|
|
expect(change).to.have.property('modelId', '' + id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-02-20 18:28:33 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-03-03 10:23:13 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
describe('complex setup', function() {
|
2015-03-05 13:29:30 +00:00
|
|
|
var sourceInstance, sourceInstanceId, AnotherModel;
|
2015-03-05 12:13:21 +00:00
|
|
|
|
|
|
|
beforeEach(function createReplicatedInstance(done) {
|
|
|
|
async.series([
|
|
|
|
function createInstance(next) {
|
2016-11-15 21:46:23 +00:00
|
|
|
SourceModel.create({id: 'test-instance'}, function(err, result) {
|
2015-03-05 12:13:21 +00:00
|
|
|
sourceInstance = result;
|
2015-03-05 13:29:30 +00:00
|
|
|
sourceInstanceId = result.id;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
replicateExpectingSuccess(),
|
2016-04-01 09:14:26 +00:00
|
|
|
verifySourceWasReplicated(),
|
2015-03-05 12:13:21 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
2015-03-05 13:29:30 +00:00
|
|
|
beforeEach(function setupThirdModel() {
|
|
|
|
AnotherModel = this.AnotherModel = PersistedModel.extend(
|
|
|
|
'AnotherModel-' + tid,
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: {id: true, type: String, defaultFn: 'guid'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true}
|
|
|
|
);
|
2015-03-05 13:29:30 +00:00
|
|
|
|
|
|
|
// NOTE(bajtos) At the moment, all models share the same Checkpoint
|
|
|
|
// model. This causes the in-process replication to work differently
|
|
|
|
// than client-server replication.
|
|
|
|
// As a workaround, we manually setup unique Checkpoint for AnotherModel.
|
|
|
|
var AnotherChange = AnotherModel.Change;
|
|
|
|
AnotherChange.Checkpoint = loopback.Checkpoint.extend('AnotherCheckpoint');
|
|
|
|
AnotherChange.Checkpoint.attachTo(dataSource);
|
|
|
|
|
|
|
|
AnotherModel.attachTo(dataSource);
|
|
|
|
});
|
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
it('correctly replicates without checkpoint filter', function(done) {
|
|
|
|
async.series([
|
|
|
|
updateSourceInstanceNameTo('updated'),
|
|
|
|
replicateExpectingSuccess(),
|
2015-03-05 13:29:30 +00:00
|
|
|
verifySourceWasReplicated(),
|
2015-03-05 12:13:21 +00:00
|
|
|
|
|
|
|
function deleteInstance(next) {
|
|
|
|
sourceInstance.remove(next);
|
|
|
|
},
|
|
|
|
replicateExpectingSuccess(),
|
|
|
|
function verifyTargetModelWasDeleted(next) {
|
|
|
|
TargetModel.find(function(err, list) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
expect(getIds(list)).to.not.contain(sourceInstance.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-03-05 12:13:21 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('replicates multiple updates within the same CP', function(done) {
|
|
|
|
async.series([
|
|
|
|
replicateExpectingSuccess(),
|
2015-03-05 13:29:30 +00:00
|
|
|
verifySourceWasReplicated(),
|
2015-03-05 12:13:21 +00:00
|
|
|
|
|
|
|
updateSourceInstanceNameTo('updated'),
|
|
|
|
updateSourceInstanceNameTo('again'),
|
|
|
|
replicateExpectingSuccess(),
|
2016-04-01 09:14:26 +00:00
|
|
|
verifySourceWasReplicated(),
|
2015-03-05 12:13:21 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
2015-03-05 13:29:30 +00:00
|
|
|
describe('clientA-server-clientB', function() {
|
|
|
|
var ClientA, Server, ClientB;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2017-12-12 08:33:15 +00:00
|
|
|
ClientA = SourceModel;
|
2015-03-05 13:29:30 +00:00
|
|
|
Server = TargetModel;
|
|
|
|
ClientB = AnotherModel;
|
|
|
|
|
|
|
|
// NOTE(bajtos) The tests should ideally pass without the since
|
|
|
|
// filter too. Unfortunately that's not possible with the current
|
|
|
|
// implementation that remembers only the last two changes made.
|
|
|
|
useSinceFilter = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('replicates new models', function(done) {
|
|
|
|
async.series([
|
|
|
|
// Note that ClientA->Server was already replicated during setup
|
|
|
|
replicateExpectingSuccess(Server, ClientB),
|
2016-04-01 09:14:26 +00:00
|
|
|
verifySourceWasReplicated(ClientB),
|
2015-03-05 13:29:30 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('propagates updates with no false conflicts', function(done) {
|
|
|
|
async.series([
|
|
|
|
updateSourceInstanceNameTo('v2'),
|
|
|
|
replicateExpectingSuccess(ClientA, Server),
|
|
|
|
|
|
|
|
replicateExpectingSuccess(Server, ClientB),
|
|
|
|
|
|
|
|
updateSourceInstanceNameTo('v3'),
|
|
|
|
replicateExpectingSuccess(ClientA, Server),
|
|
|
|
updateSourceInstanceNameTo('v4'),
|
|
|
|
replicateExpectingSuccess(ClientA, Server),
|
|
|
|
|
|
|
|
replicateExpectingSuccess(Server, ClientB),
|
2016-04-01 09:14:26 +00:00
|
|
|
verifySourceWasReplicated(ClientB),
|
2015-03-05 13:29:30 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('propagates deletes with no false conflicts', function(done) {
|
|
|
|
async.series([
|
|
|
|
deleteSourceInstance(),
|
|
|
|
replicateExpectingSuccess(ClientA, Server),
|
|
|
|
replicateExpectingSuccess(Server, ClientB),
|
2016-04-01 09:14:26 +00:00
|
|
|
verifySourceWasReplicated(ClientB),
|
2015-03-05 13:29:30 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('bidirectional sync', function() {
|
|
|
|
beforeEach(function finishInitialSync(next) {
|
|
|
|
// The fixture setup creates a new model instance and replicates
|
|
|
|
// it from ClientA to Server. Since we are performing bidirectional
|
|
|
|
// synchronization in this suite, we must complete the first sync,
|
|
|
|
// otherwise some of the tests may fail.
|
|
|
|
replicateExpectingSuccess(Server, ClientA)(next);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('propagates CREATE', function(done) {
|
|
|
|
async.series([
|
|
|
|
sync(ClientA, Server),
|
2016-04-01 09:14:26 +00:00
|
|
|
sync(ClientB, Server),
|
2015-03-05 13:29:30 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('propagates CREATE+UPDATE', function(done) {
|
|
|
|
async.series([
|
|
|
|
// NOTE: ClientB has not fetched the new model instance yet
|
|
|
|
updateSourceInstanceNameTo('v2'),
|
|
|
|
sync(ClientA, Server),
|
|
|
|
|
|
|
|
// ClientB fetches the created & updated instance from the server
|
|
|
|
sync(ClientB, Server),
|
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('propagates DELETE', function(done) {
|
|
|
|
async.series([
|
|
|
|
// NOTE: ClientB has not fetched the new model instance yet
|
|
|
|
updateSourceInstanceNameTo('v2'),
|
|
|
|
sync(ClientA, Server),
|
|
|
|
|
|
|
|
// ClientB fetches the created & updated instance from the server
|
|
|
|
sync(ClientB, Server),
|
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not report false conflicts', function(done) {
|
|
|
|
async.series([
|
|
|
|
// client A makes some work
|
|
|
|
updateSourceInstanceNameTo('v2'),
|
|
|
|
sync(ClientA, Server),
|
|
|
|
|
|
|
|
// ClientB fetches the change from the server
|
|
|
|
sync(ClientB, Server),
|
|
|
|
verifySourceWasReplicated(ClientB),
|
|
|
|
|
|
|
|
// client B makes some work
|
|
|
|
updateClientB('v5'),
|
|
|
|
sync(Server, ClientB),
|
|
|
|
updateClientB('v6'),
|
|
|
|
sync(ClientB, Server),
|
|
|
|
|
|
|
|
// client A fetches the changes
|
2016-04-01 09:14:26 +00:00
|
|
|
sync(ClientA, Server),
|
2015-03-05 13:29:30 +00:00
|
|
|
], done);
|
|
|
|
});
|
2015-03-06 13:43:43 +00:00
|
|
|
|
2015-03-20 16:47:07 +00:00
|
|
|
it('handles UPDATE conflict resolved using "ours"', function(done) {
|
|
|
|
testUpdateConflictIsResolved(
|
2015-03-06 13:43:43 +00:00
|
|
|
function resolveUsingOurs(conflict, cb) {
|
2015-03-20 16:47:07 +00:00
|
|
|
conflict.resolveUsingSource(cb);
|
2015-03-06 13:43:43 +00:00
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
done
|
|
|
|
);
|
2015-03-06 13:43:43 +00:00
|
|
|
});
|
|
|
|
|
2015-03-20 16:47:07 +00:00
|
|
|
it('handles UPDATE conflict resolved using "theirs"', function(done) {
|
|
|
|
testUpdateConflictIsResolved(
|
2015-03-06 13:43:43 +00:00
|
|
|
function resolveUsingTheirs(conflict, cb) {
|
2015-03-20 16:47:07 +00:00
|
|
|
// We sync ClientA->Server first
|
|
|
|
expect(conflict.SourceModel.modelName)
|
|
|
|
.to.equal(ClientB.modelName);
|
|
|
|
conflict.resolveUsingTarget(cb);
|
2015-03-06 13:43:43 +00:00
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
done
|
|
|
|
);
|
2015-03-06 13:43:43 +00:00
|
|
|
});
|
|
|
|
|
2015-03-20 16:47:07 +00:00
|
|
|
it('handles UPDATE conflict resolved manually', function(done) {
|
|
|
|
testUpdateConflictIsResolved(
|
2015-03-06 13:43:43 +00:00
|
|
|
function resolveManually(conflict, cb) {
|
2016-11-15 21:46:23 +00:00
|
|
|
conflict.resolveManually({name: 'manual'}, cb);
|
2015-03-20 16:47:07 +00:00
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
done
|
|
|
|
);
|
2015-03-20 16:47:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles DELETE conflict resolved using "ours"', function(done) {
|
|
|
|
testDeleteConflictIsResolved(
|
|
|
|
function resolveUsingOurs(conflict, cb) {
|
|
|
|
conflict.resolveUsingSource(cb);
|
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
done
|
|
|
|
);
|
2015-03-20 16:47:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles DELETE conflict resolved using "theirs"', function(done) {
|
|
|
|
testDeleteConflictIsResolved(
|
|
|
|
function resolveUsingTheirs(conflict, cb) {
|
|
|
|
// We sync ClientA->Server first
|
|
|
|
expect(conflict.SourceModel.modelName)
|
|
|
|
.to.equal(ClientB.modelName);
|
|
|
|
conflict.resolveUsingTarget(cb);
|
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
done
|
|
|
|
);
|
2015-03-20 16:47:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles DELETE conflict resolved as manual delete', function(done) {
|
|
|
|
testDeleteConflictIsResolved(
|
|
|
|
function resolveManually(conflict, cb) {
|
|
|
|
conflict.resolveManually(null, cb);
|
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
done
|
|
|
|
);
|
2015-03-20 16:47:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles DELETE conflict resolved manually', function(done) {
|
|
|
|
testDeleteConflictIsResolved(
|
|
|
|
function resolveManually(conflict, cb) {
|
2016-11-15 21:46:23 +00:00
|
|
|
conflict.resolveManually({name: 'manual'}, cb);
|
2015-03-06 13:43:43 +00:00
|
|
|
},
|
2018-08-08 15:22:20 +00:00
|
|
|
done
|
|
|
|
);
|
2015-03-06 13:43:43 +00:00
|
|
|
});
|
2015-03-05 13:29:30 +00:00
|
|
|
});
|
|
|
|
|
2015-03-20 16:47:07 +00:00
|
|
|
function testUpdateConflictIsResolved(resolver, cb) {
|
2015-03-06 13:43:43 +00:00
|
|
|
async.series([
|
|
|
|
// sync the new model to ClientB
|
|
|
|
sync(ClientB, Server),
|
2015-03-16 12:38:37 +00:00
|
|
|
verifyInstanceWasReplicated(ClientA, ClientB, sourceInstanceId),
|
2015-03-06 13:43:43 +00:00
|
|
|
|
|
|
|
// ClientA makes a change
|
|
|
|
updateSourceInstanceNameTo('a'),
|
|
|
|
sync(ClientA, Server),
|
2015-03-20 16:47:07 +00:00
|
|
|
|
|
|
|
// ClientB changes the same instance
|
|
|
|
updateClientB('b'),
|
|
|
|
|
|
|
|
function syncAndResolveConflict(next) {
|
|
|
|
replicate(ClientB, Server, function(err, conflicts, cps) {
|
|
|
|
if (err) return next(err);
|
|
|
|
|
|
|
|
expect(conflicts).to.have.length(1);
|
|
|
|
expect(conflicts[0].SourceModel.modelName)
|
|
|
|
.to.equal(ClientB.modelName);
|
|
|
|
|
|
|
|
debug('Resolving the conflict %j', conflicts[0]);
|
|
|
|
resolver(conflicts[0], next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// repeat the last sync, it should pass now
|
|
|
|
sync(ClientB, Server),
|
|
|
|
// and sync back to ClientA too
|
|
|
|
sync(ClientA, Server),
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
verifyInstanceWasReplicated(ClientB, ClientA, sourceInstanceId),
|
2015-03-20 16:47:07 +00:00
|
|
|
], cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDeleteConflictIsResolved(resolver, cb) {
|
|
|
|
async.series([
|
|
|
|
// sync the new model to ClientB
|
|
|
|
sync(ClientB, Server),
|
|
|
|
verifyInstanceWasReplicated(ClientA, ClientB, sourceInstanceId),
|
|
|
|
|
|
|
|
// ClientA makes a change
|
|
|
|
function deleteInstanceOnClientA(next) {
|
|
|
|
ClientA.deleteById(sourceInstanceId, next);
|
|
|
|
},
|
|
|
|
|
|
|
|
sync(ClientA, Server),
|
2015-03-06 13:43:43 +00:00
|
|
|
|
|
|
|
// ClientB changes the same instance
|
|
|
|
updateClientB('b'),
|
|
|
|
|
|
|
|
function syncAndResolveConflict(next) {
|
|
|
|
replicate(ClientB, Server, function(err, conflicts, cps) {
|
|
|
|
if (err) return next(err);
|
|
|
|
|
|
|
|
expect(conflicts).to.have.length(1);
|
|
|
|
expect(conflicts[0].SourceModel.modelName)
|
|
|
|
.to.equal(ClientB.modelName);
|
|
|
|
|
|
|
|
debug('Resolving the conflict %j', conflicts[0]);
|
|
|
|
resolver(conflicts[0], next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// repeat the last sync, it should pass now
|
|
|
|
sync(ClientB, Server),
|
|
|
|
// and sync back to ClientA too
|
|
|
|
sync(ClientA, Server),
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
verifyInstanceWasReplicated(ClientB, ClientA, sourceInstanceId),
|
2015-03-06 13:43:43 +00:00
|
|
|
], cb);
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:29:30 +00:00
|
|
|
function updateClientB(name) {
|
|
|
|
return function updateInstanceB(next) {
|
|
|
|
ClientB.findById(sourceInstanceId, function(err, instance) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 13:29:30 +00:00
|
|
|
instance.name = name;
|
|
|
|
instance.save(next);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function sync(client, server) {
|
|
|
|
return function syncBothWays(next) {
|
|
|
|
async.series([
|
2015-03-06 13:43:43 +00:00
|
|
|
// NOTE(bajtos) It's important to replicate from the client to the
|
|
|
|
// server first, so that we can resolve any conflicts at the client
|
|
|
|
replicateExpectingSuccess(client, server),
|
2016-04-01 09:14:26 +00:00
|
|
|
replicateExpectingSuccess(server, client),
|
2015-03-05 13:29:30 +00:00
|
|
|
], next);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
function updateSourceInstanceNameTo(value) {
|
|
|
|
return function updateInstance(next) {
|
2015-03-16 12:38:37 +00:00
|
|
|
debug('update source instance name to %j', value);
|
2015-03-05 12:13:21 +00:00
|
|
|
sourceInstance.name = value;
|
|
|
|
sourceInstance.save(next);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:29:30 +00:00
|
|
|
function deleteSourceInstance(value) {
|
|
|
|
return function deleteInstance(next) {
|
2015-03-16 12:38:37 +00:00
|
|
|
debug('delete source instance', value);
|
2015-03-05 13:29:30 +00:00
|
|
|
sourceInstance.remove(function(err) {
|
|
|
|
sourceInstance = null;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 13:29:30 +00:00
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifySourceWasReplicated(target) {
|
|
|
|
if (!target) target = TargetModel;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
return function verify(next) {
|
2015-03-05 13:29:30 +00:00
|
|
|
target.findById(sourceInstanceId, function(err, targetInstance) {
|
2015-03-05 12:13:21 +00:00
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 13:29:30 +00:00
|
|
|
expect(targetInstance && targetInstance.toObject())
|
|
|
|
.to.eql(sourceInstance && sourceInstance.toObject());
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-15 02:02:23 +00:00
|
|
|
describe('ensure options object is set on context during bulkUpdate', function() {
|
|
|
|
var syncPropertyExists = false;
|
|
|
|
var OptionsSourceModel;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
OptionsSourceModel = PersistedModel.extend(
|
|
|
|
'OptionsSourceModel-' + tid,
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: {id: true, type: String, defaultFn: 'guid'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true}
|
|
|
|
);
|
2016-11-15 02:02:23 +00:00
|
|
|
|
|
|
|
OptionsSourceModel.attachTo(dataSource);
|
|
|
|
|
|
|
|
OptionsSourceModel.observe('before save', function updateTimestamp(ctx, next) {
|
|
|
|
if (ctx.options.sync) {
|
|
|
|
syncPropertyExists = true;
|
|
|
|
} else {
|
|
|
|
syncPropertyExists = false;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('bulkUpdate should call Model updates with the provided options object', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
var testData = {name: 'Janie', surname: 'Doe'};
|
2016-11-15 02:02:23 +00:00
|
|
|
var updates = [
|
|
|
|
{
|
|
|
|
data: null,
|
|
|
|
change: null,
|
|
|
|
type: 'create',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
sync: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function(callback) {
|
|
|
|
TargetModel.create(testData, callback);
|
|
|
|
},
|
|
|
|
function(data, callback) {
|
|
|
|
updates[0].data = data;
|
2016-11-15 21:46:23 +00:00
|
|
|
TargetModel.getChangeModel().find({where: {modelId: data.id}}, callback);
|
2016-11-15 02:02:23 +00:00
|
|
|
},
|
|
|
|
function(data, callback) {
|
|
|
|
updates[0].change = data;
|
|
|
|
OptionsSourceModel.bulkUpdate(updates, options, callback);
|
|
|
|
}],
|
2017-12-12 08:33:15 +00:00
|
|
|
function(err, result) {
|
|
|
|
if (err) return done(err);
|
2016-11-15 02:02:23 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
expect(syncPropertyExists).to.eql(true);
|
2016-11-15 02:02:23 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done();
|
2018-08-08 15:22:20 +00:00
|
|
|
});
|
2016-11-15 02:02:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ensure bulkUpdate works with just 2 args', function() {
|
|
|
|
it('bulkUpdate should successfully finish without options', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
var testData = {name: 'Janie', surname: 'Doe'};
|
2016-11-15 02:02:23 +00:00
|
|
|
var updates = [{
|
|
|
|
data: null,
|
|
|
|
change: null,
|
|
|
|
type: 'create',
|
|
|
|
}];
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function(callback) {
|
|
|
|
TargetModel.create(testData, callback);
|
|
|
|
},
|
|
|
|
function(data, callback) {
|
|
|
|
updates[0].data = data;
|
2016-11-15 21:46:23 +00:00
|
|
|
TargetModel.getChangeModel().find({where: {modelId: data.id}}, callback);
|
2016-11-15 02:02:23 +00:00
|
|
|
},
|
|
|
|
function(data, callback) {
|
|
|
|
updates[0].change = data;
|
|
|
|
SourceModel.bulkUpdate(updates, callback);
|
|
|
|
},
|
|
|
|
], function(err, result) {
|
|
|
|
if (err) return done(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-21 08:48:43 +00:00
|
|
|
describe('Replication with chunking', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
var test = this;
|
|
|
|
SourceModel = this.SourceModel = PersistedModel.extend(
|
|
|
|
'SourceModel-' + tid,
|
|
|
|
{id: {id: true, type: String, defaultFn: 'guid'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true, replicationChunkSize: 1}
|
|
|
|
);
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
SourceModel.attachTo(dataSource);
|
|
|
|
|
|
|
|
TargetModel = this.TargetModel = PersistedModel.extend(
|
|
|
|
'TargetModel-' + tid,
|
|
|
|
{id: {id: true, type: String, defaultFn: 'guid'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true, replicationChunkSize: 1}
|
|
|
|
);
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
var TargetChange = TargetModel.Change;
|
|
|
|
TargetChange.Checkpoint = loopback.Checkpoint.extend('TargetCheckpoint');
|
|
|
|
TargetChange.Checkpoint.attachTo(dataSource);
|
|
|
|
|
|
|
|
TargetModel.attachTo(dataSource);
|
|
|
|
|
|
|
|
test.startingCheckpoint = -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.replicate(since, targetModel, options, callback)', function() {
|
|
|
|
it('calls bulkUpdate multiple times', function(done) {
|
|
|
|
var test = this;
|
|
|
|
var options = {};
|
|
|
|
var calls = mockBulkUpdate(TargetModel);
|
|
|
|
|
|
|
|
SourceModel.create([{name: 'foo'}, {name: 'bar'}], function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
test.SourceModel.replicate(test.startingCheckpoint, test.TargetModel,
|
|
|
|
options, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
assertTargetModelEqualsSourceModel(conflicts, test.SourceModel,
|
|
|
|
test.TargetModel, done);
|
|
|
|
expect(calls.length).to.eql(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Replication without chunking', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
var test = this;
|
|
|
|
SourceModel = this.SourceModel = PersistedModel.extend(
|
|
|
|
'SourceModel-' + tid,
|
|
|
|
{id: {id: true, type: String, defaultFn: 'guid'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true}
|
|
|
|
);
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
SourceModel.attachTo(dataSource);
|
|
|
|
|
|
|
|
TargetModel = this.TargetModel = PersistedModel.extend(
|
|
|
|
'TargetModel-' + tid,
|
|
|
|
{id: {id: true, type: String, defaultFn: 'guid'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
{trackChanges: true}
|
|
|
|
);
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
var TargetChange = TargetModel.Change;
|
|
|
|
TargetChange.Checkpoint = loopback.Checkpoint.extend('TargetCheckpoint');
|
|
|
|
TargetChange.Checkpoint.attachTo(dataSource);
|
|
|
|
|
|
|
|
TargetModel.attachTo(dataSource);
|
|
|
|
|
|
|
|
test.startingCheckpoint = -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.replicate(since, targetModel, options, callback)', function() {
|
|
|
|
it('calls bulkUpdate only once', function(done) {
|
|
|
|
var test = this;
|
|
|
|
var options = {};
|
|
|
|
var calls = mockBulkUpdate(TargetModel);
|
|
|
|
|
|
|
|
SourceModel.create([{name: 'foo'}, {name: 'bar'}], function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
test.SourceModel.replicate(test.startingCheckpoint, test.TargetModel,
|
|
|
|
options, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
assertTargetModelEqualsSourceModel(conflicts, test.SourceModel,
|
|
|
|
test.TargetModel, done);
|
|
|
|
expect(calls.length).to.eql(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function mockBulkUpdate(modelToMock) {
|
|
|
|
var calls = [];
|
|
|
|
|
|
|
|
var originalBulkUpdateFunction = modelToMock.bulkUpdate;
|
|
|
|
|
|
|
|
modelToMock.bulkUpdate = function(since, filter, callback) {
|
|
|
|
calls.push('bulkUpdate');
|
|
|
|
originalBulkUpdateFunction.call(this, since, filter, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
return calls;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:29:30 +00:00
|
|
|
var _since = {};
|
2015-03-06 13:43:43 +00:00
|
|
|
function replicate(source, target, since, next) {
|
|
|
|
if (typeof since === 'function') {
|
|
|
|
next = since;
|
|
|
|
since = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sinceIx = source.modelName + ':to:' + target.modelName;
|
|
|
|
if (since === undefined) {
|
|
|
|
since = useSinceFilter ?
|
|
|
|
_since[sinceIx] || -1 :
|
|
|
|
-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('replicate from %s to %s since %j',
|
|
|
|
source.modelName, target.modelName, since);
|
2015-03-05 13:29:30 +00:00
|
|
|
|
2015-03-06 13:43:43 +00:00
|
|
|
source.replicate(since, target, function(err, conflicts, cps) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-06 13:43:43 +00:00
|
|
|
if (conflicts.length === 0) {
|
|
|
|
_since[sinceIx] = cps;
|
2015-03-05 13:29:30 +00:00
|
|
|
}
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-06 13:43:43 +00:00
|
|
|
next(err, conflicts, cps);
|
|
|
|
});
|
|
|
|
}
|
2015-03-05 13:29:30 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
function createModel(Model, data) {
|
|
|
|
return function create(next) {
|
|
|
|
Model.create(data, next);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-03-06 13:43:43 +00:00
|
|
|
function replicateExpectingSuccess(source, target, since) {
|
|
|
|
if (!source) source = SourceModel;
|
|
|
|
if (!target) target = TargetModel;
|
2015-03-05 13:29:30 +00:00
|
|
|
|
2015-03-06 13:43:43 +00:00
|
|
|
return function doReplicate(next) {
|
|
|
|
replicate(source, target, since, function(err, conflicts, cps) {
|
2015-03-05 12:13:21 +00:00
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
if (conflicts.length) {
|
|
|
|
return next(new Error('Unexpected conflicts\n' +
|
|
|
|
conflicts.map(JSON.stringify).join('\n')));
|
|
|
|
}
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
function setupRaceConditionInReplication(fn) {
|
|
|
|
var bulkUpdate = TargetModel.bulkUpdate;
|
2016-11-15 02:02:23 +00:00
|
|
|
TargetModel.bulkUpdate = function(data, options, cb) {
|
2015-03-16 12:38:37 +00:00
|
|
|
// simulate the situation when a 3rd party modifies the database
|
|
|
|
// while a replication run is in progress
|
|
|
|
var self = this;
|
|
|
|
fn(function(err) {
|
|
|
|
if (err) return cb(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-15 02:02:23 +00:00
|
|
|
bulkUpdate.call(self, data, options, cb);
|
2015-03-16 12:38:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// apply the 3rd party modification only once
|
|
|
|
TargetModel.bulkUpdate = bulkUpdate;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifyInstanceWasReplicated(source, target, id) {
|
|
|
|
return function verify(next) {
|
|
|
|
source.findById(id, function(err, expected) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
target.findById(id, function(err, actual) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
expect(actual && actual.toObject())
|
|
|
|
.to.eql(expected && expected.toObject());
|
|
|
|
debug('replicated instance: %j', actual);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-03-03 10:23:13 +00:00
|
|
|
function spyAndStoreSinceArg(Model, methodName, store) {
|
|
|
|
var orig = Model[methodName];
|
|
|
|
Model[methodName] = function(since) {
|
|
|
|
store.push(since);
|
|
|
|
orig.apply(this, arguments);
|
|
|
|
};
|
|
|
|
}
|
2015-03-03 10:50:06 +00:00
|
|
|
|
|
|
|
function getPropValue(obj, name) {
|
|
|
|
return Array.isArray(obj) ?
|
|
|
|
obj.map(function(it) { return getPropValue(it, name); }) :
|
|
|
|
obj[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIds(list) {
|
|
|
|
return getPropValue(list, 'id');
|
|
|
|
}
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
function assertTargetModelEqualsSourceModel(conflicts, sourceModel,
|
2017-12-12 08:33:15 +00:00
|
|
|
targetModel, done) {
|
2016-11-21 08:48:43 +00:00
|
|
|
var sourceData, targetData;
|
|
|
|
|
|
|
|
assert(conflicts.length === 0);
|
|
|
|
async.parallel([
|
|
|
|
function(cb) {
|
|
|
|
sourceModel.find(function(err, result) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
sourceData = result;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(cb) {
|
|
|
|
targetModel.find(function(err, result) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
targetData = result;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
assert.deepEqual(sourceData, targetData);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2014-05-19 22:56:26 +00:00
|
|
|
});
|
2016-11-21 08:13:16 +00:00
|
|
|
|
|
|
|
describe('Replication / Change APIs with custom change properties', function() {
|
|
|
|
this.timeout(10000);
|
|
|
|
var dataSource, useSinceFilter, SourceModel, TargetModel, startingCheckpoint;
|
|
|
|
var tid = 0; // per-test unique id used e.g. to build unique model names
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
tid++;
|
|
|
|
useSinceFilter = false;
|
|
|
|
var test = this;
|
|
|
|
|
|
|
|
dataSource = this.dataSource = loopback.createDataSource({
|
|
|
|
connector: loopback.Memory,
|
|
|
|
});
|
|
|
|
SourceModel = this.SourceModel = PersistedModel.extend(
|
|
|
|
'SourceModelWithCustomChangeProperties-' + tid,
|
|
|
|
{
|
|
|
|
id: {id: true, type: String, defaultFn: 'guid'},
|
|
|
|
customProperty: {type: 'string'},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
trackChanges: true,
|
|
|
|
additionalChangeModelProperties: {customProperty: {type: 'string'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2016-11-21 08:13:16 +00:00
|
|
|
|
|
|
|
SourceModel.createChangeFilter = function(since, modelFilter) {
|
|
|
|
const filter = this.base.createChangeFilter.apply(this, arguments);
|
|
|
|
if (modelFilter && modelFilter.where && modelFilter.where.customProperty)
|
|
|
|
filter.where.customProperty = modelFilter.where.customProperty;
|
|
|
|
return filter;
|
|
|
|
};
|
|
|
|
|
|
|
|
SourceModel.prototype.fillCustomChangeProperties = function(change, cb) {
|
|
|
|
const customProperty = this.customProperty;
|
|
|
|
const base = this.constructor.base;
|
|
|
|
base.prototype.fillCustomChangeProperties.call(this, change, err => {
|
|
|
|
if (err) return cb(err);
|
|
|
|
change.customProperty = customProperty;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
SourceModel.attachTo(dataSource);
|
|
|
|
|
|
|
|
TargetModel = this.TargetModel = PersistedModel.extend(
|
|
|
|
'TargetModelWithCustomChangeProperties-' + tid,
|
|
|
|
{
|
|
|
|
id: {id: true, type: String, defaultFn: 'guid'},
|
|
|
|
customProperty: {type: 'string'},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
trackChanges: true,
|
|
|
|
additionalChangeModelProperties: {customProperty: {type: 'string'}},
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2016-11-21 08:13:16 +00:00
|
|
|
|
|
|
|
var ChangeModelForTarget = TargetModel.Change;
|
|
|
|
ChangeModelForTarget.Checkpoint = loopback.Checkpoint.extend('TargetCheckpoint');
|
|
|
|
ChangeModelForTarget.Checkpoint.attachTo(dataSource);
|
|
|
|
|
|
|
|
TargetModel.attachTo(dataSource);
|
|
|
|
|
|
|
|
startingCheckpoint = -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model._defineChangeModel()', function() {
|
|
|
|
it('defines change model with custom properties', function() {
|
|
|
|
var changeModel = SourceModel.getChangeModel();
|
|
|
|
var changeModelProperties = changeModel.definition.properties;
|
|
|
|
|
|
|
|
expect(changeModelProperties).to.have.property('customProperty');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.changes(since, filter, callback)', function() {
|
|
|
|
beforeEach(givenSomeSourceModelInstances);
|
|
|
|
|
|
|
|
it('queries changes using customized filter', function(done) {
|
|
|
|
var filterUsed = mockChangeFind(this.SourceModel);
|
|
|
|
|
|
|
|
SourceModel.changes(
|
|
|
|
startingCheckpoint,
|
|
|
|
{where: {customProperty: '123'}},
|
|
|
|
function(err, changes) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(filterUsed[0]).to.eql({
|
|
|
|
where: {
|
|
|
|
checkpoint: {gte: -1},
|
|
|
|
modelName: SourceModel.modelName,
|
|
|
|
customProperty: '123',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
done();
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2016-11-21 08:13:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('query returns the matching changes', function(done) {
|
|
|
|
SourceModel.changes(
|
|
|
|
startingCheckpoint,
|
|
|
|
{where: {customProperty: '123'}},
|
|
|
|
function(err, changes) {
|
|
|
|
expect(changes).to.have.length(1);
|
|
|
|
expect(changes[0]).to.have.property('customProperty', '123');
|
|
|
|
done();
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2016-11-21 08:13:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function givenSomeSourceModelInstances(done) {
|
|
|
|
const data = [
|
|
|
|
{name: 'foo', customProperty: '123'},
|
|
|
|
{name: 'foo', customPropertyValue: '456'},
|
|
|
|
];
|
|
|
|
this.SourceModel.create(data, done);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function mockChangeFind(Model) {
|
|
|
|
var filterUsed = [];
|
|
|
|
|
|
|
|
Model.getChangeModel().find = function(filter, cb) {
|
|
|
|
filterUsed.push(filter);
|
|
|
|
if (cb) {
|
|
|
|
process.nextTick(cb);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return filterUsed;
|
|
|
|
}
|
|
|
|
});
|