2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// 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';
|
2014-07-11 15:07:22 +00:00
|
|
|
var async = require('async');
|
|
|
|
var loopback = require('../');
|
2016-11-22 14:30:04 +00:00
|
|
|
var expect = require('./helpers/expect');
|
2014-07-11 15:07:22 +00:00
|
|
|
|
2014-10-09 15:46:36 +00:00
|
|
|
var Checkpoint = loopback.Checkpoint.extend('TestCheckpoint');
|
2015-03-06 13:47:43 +00:00
|
|
|
|
2014-07-11 15:07:22 +00:00
|
|
|
describe('Checkpoint', function() {
|
2015-12-17 17:02:35 +00:00
|
|
|
describe('bumpLastSeq() and current()', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
var memory = loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2015-12-17 17:02:35 +00:00
|
|
|
});
|
|
|
|
Checkpoint.attachTo(memory);
|
|
|
|
});
|
|
|
|
|
2014-07-11 15:07:22 +00:00
|
|
|
it('returns the highest `seq` value', function(done) {
|
|
|
|
async.series([
|
2015-12-17 17:02:35 +00:00
|
|
|
Checkpoint.bumpLastSeq.bind(Checkpoint),
|
|
|
|
Checkpoint.bumpLastSeq.bind(Checkpoint),
|
2014-07-11 15:07:22 +00:00
|
|
|
function(next) {
|
|
|
|
Checkpoint.current(function(err, seq) {
|
|
|
|
if (err) next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-03 13:53:49 +00:00
|
|
|
expect(seq).to.equal(3);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-11 15:07:22 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2014-07-11 15:07:22 +00:00
|
|
|
], done);
|
|
|
|
});
|
2015-12-17 17:02:35 +00:00
|
|
|
|
|
|
|
it('Should be no race condition for current() when calling in parallel', function(done) {
|
|
|
|
async.parallel([
|
|
|
|
function(next) { Checkpoint.current(next); },
|
2016-04-01 09:14:26 +00:00
|
|
|
function(next) { Checkpoint.current(next); },
|
2015-12-17 17:02:35 +00:00
|
|
|
], function(err, list) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-12-17 17:02:35 +00:00
|
|
|
Checkpoint.find(function(err, data) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-12-17 17:02:35 +00:00
|
|
|
expect(data).to.have.length(1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-12-17 17:02:35 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should be no race condition for bumpLastSeq() when calling in parallel', function(done) {
|
|
|
|
async.parallel([
|
|
|
|
function(next) { Checkpoint.bumpLastSeq(next); },
|
2016-04-01 09:14:26 +00:00
|
|
|
function(next) { Checkpoint.bumpLastSeq(next); },
|
2015-12-17 17:02:35 +00:00
|
|
|
], function(err, list) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-12-17 17:02:35 +00:00
|
|
|
Checkpoint.find(function(err, data) {
|
|
|
|
if (err) return done(err);
|
|
|
|
// The invariant "we have at most 1 checkpoint instance" is preserved
|
|
|
|
// even when multiple calls are made in parallel
|
|
|
|
expect(data).to.have.length(1);
|
|
|
|
// There is a race condition here, we could end up with both 2 or 3 as the "seq".
|
|
|
|
// The current implementation of the memory connector always yields 2 though.
|
|
|
|
expect(data[0].seq).to.equal(2);
|
|
|
|
// In this particular case, since the new last seq is always 2, both results
|
|
|
|
// should be 2.
|
2016-04-01 09:14:26 +00:00
|
|
|
expect(list.map(function(it) { return it.seq; }))
|
2015-12-17 17:02:35 +00:00
|
|
|
.to.eql([2, 2]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-12-17 17:02:35 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
it('Checkpoint.current() for non existing checkpoint should initialize checkpoint',
|
2017-12-12 08:33:15 +00:00
|
|
|
function(done) {
|
|
|
|
Checkpoint.current(function(err, seq) {
|
|
|
|
expect(seq).to.equal(1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done(err);
|
|
|
|
});
|
2015-12-17 17:02:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('bumpLastSeq() works when singleton instance does not exists yet', function(done) {
|
|
|
|
Checkpoint.bumpLastSeq(function(err, cp) {
|
|
|
|
// We expect `seq` to be 2 since `checkpoint` does not exist and
|
|
|
|
// `bumpLastSeq` for the first time not only initializes it to one,
|
|
|
|
// but also increments the initialized value by one.
|
|
|
|
expect(cp.seq).to.equal(2);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-12-17 17:02:35 +00:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2014-07-11 15:07:22 +00:00
|
|
|
});
|
|
|
|
});
|