fix before put concurrency - should fix builds loss

This commit is contained in:
oleg 2015-12-12 00:00:31 +03:00
parent 001469509f
commit 66bab3949e
2 changed files with 35 additions and 19 deletions

21
db.js
View File

@ -198,22 +198,23 @@ nlevel.DocsSection.prototype._beforePut = function(docs, callback) {
return callback(); return callback();
} }
Steppy(
function() {
if (self._beforePutInProgress) { if (self._beforePutInProgress) {
return setTimeout(function() { setTimeout(function() {
nlevel.DocsSection.prototype._beforePut.call( nlevel.DocsSection.prototype._beforePut.call(
self, docs, callback self, docs, callback
); );
}, 5); }, 1);
} } else {
self._beforePutInProgress = true; self._beforePutInProgress = true;
self.beforePut(docs, this.slot()); // update createDate before put to provide latest date for last id
}, // it's rquired for correct generateIds function
callback _(docs).each(function(doc) {
); doc.createDate = Date.now();
});
self.beforePut(docs, callback);
}
}; };
nlevel.DocsSection.prototype._afterPut = function(docs, callback) { nlevel.DocsSection.prototype._afterPut = function(docs, callback) {

View File

@ -19,13 +19,28 @@ describe('Db concurrency', function() {
}; };
before(function(done) { before(function(done) {
db = helpers.initDb(done); Steppy(
function() {
db = helpers.initDb(this.slot());
},
function() {
db.builds.find({}, this.slot());
},
function(err, builds) {
if (builds.length) {
db.builds.del(builds, this.slot());
} else {
this.pass(null);
}
},
done
);
}); });
describe('prallel builds put should produce different ids', function() { describe('prallel builds put should produce different ids', function() {
var expectedIds = []; var expectedIds = [];
var builds = _(2).chain().range().map(function(number) { var builds = _(100).chain().range().map(function(number) {
expectedIds.push(number + 1); expectedIds.push(number + 1);
return makeBuild({project: {name: 'project' + number}}); return makeBuild({project: {name: 'project' + number}});
}).value(); }).value();
@ -42,7 +57,7 @@ describe('Db concurrency', function() {
); );
}); });
it('shoud have ids ' + expectedIds.join(', '), function() { it('shoud have all ' + expectedIds.length +' ids ', function() {
expect(_(builds).chain().pluck('id').sortBy().value()).eql( expect(_(builds).chain().pluck('id').sortBy().value()).eql(
expectedIds expectedIds
); );
@ -56,7 +71,7 @@ describe('Db concurrency', function() {
describe('prallel builds put should produce different numbers', function() { describe('prallel builds put should produce different numbers', function() {
var expectedIds = []; var expectedIds = [];
var builds = _(3).chain().range().map(function(number) { var builds = _(100).chain().range().map(function(number) {
expectedIds.push(number + 1); expectedIds.push(number + 1);
return makeBuild({ return makeBuild({
project: {name: 'project1'}, project: {name: 'project1'},
@ -76,7 +91,7 @@ describe('Db concurrency', function() {
); );
}); });
it('shoud have ids ' + expectedIds.join(', '), function() { it('shoud have all ' + expectedIds.length +' ids ', function() {
expect(_(builds).chain().pluck('id').sortBy().value()).eql( expect(_(builds).chain().pluck('id').sortBy().value()).eql(
expectedIds expectedIds
); );