loopback/test/checkpoint.test.js

101 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

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
'use strict';
var async = require('async');
var loopback = require('../');
var expect = require('./helpers/expect');
var Checkpoint = loopback.Checkpoint.extend('TestCheckpoint');
describe('Checkpoint', function() {
2015-12-17 17:02:35 +00:00
describe('bumpLastSeq() and current()', function() {
beforeEach(function() {
var memory = loopback.createDataSource({
connector: loopback.Memory,
2015-12-17 17:02:35 +00:00
});
Checkpoint.attachTo(memory);
});
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),
function(next) {
Checkpoint.current(function(err, seq) {
if (err) next(err);
expect(seq).to.equal(3);
next();
});
},
], 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); },
function(next) { Checkpoint.current(next); },
2015-12-17 17:02:35 +00:00
], function(err, list) {
if (err) return done(err);
2015-12-17 17:02:35 +00:00
Checkpoint.find(function(err, data) {
if (err) return done(err);
2015-12-17 17:02:35 +00:00
expect(data).to.have.length(1);
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); },
function(next) { Checkpoint.bumpLastSeq(next); },
2015-12-17 17:02:35 +00:00
], function(err, list) {
if (err) return done(err);
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.
expect(list.map(function(it) { return it.seq; }))
2015-12-17 17:02:35 +00:00
.to.eql([2, 2]);
2015-12-17 17:02:35 +00:00
done();
});
});
});
it('Checkpoint.current() for non existing checkpoint should initialize checkpoint',
function(done) {
2015-12-17 17:02:35 +00:00
Checkpoint.current(function(err, seq) {
expect(seq).to.equal(1);
2015-12-17 17:02:35 +00:00
done(err);
});
});
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);
2015-12-17 17:02:35 +00:00
done(err);
});
});
});
});