From 151228b4047def0a17ffd83b9cca33092bdca912 Mon Sep 17 00:00:00 2001 From: oleg Date: Tue, 15 Dec 2015 23:26:59 +0300 Subject: [PATCH] fix id generation on builds update - new hope to fix builds loss --- db.js | 4 +++- test/db/concurrent.js | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/db.js b/db.js index e2873fd..f0a0b88 100644 --- a/db.js +++ b/db.js @@ -210,7 +210,9 @@ nlevel.DocsSection.prototype._beforePut = function(docs, callback) { // update createDate before put to provide latest date for last id // it's rquired for correct generateIds function _(docs).each(function(doc) { - doc.createDate = Date.now(); + if (!doc.id) { + doc.createDate = Date.now(); + } }); self.beforePut(docs, callback); diff --git a/test/db/concurrent.js b/test/db/concurrent.js index eb63556..d4f74b1 100644 --- a/test/db/concurrent.js +++ b/test/db/concurrent.js @@ -37,7 +37,7 @@ describe('Db concurrency', function() { ); }); - describe('prallel builds put should produce different ids', function() { + describe('prallel builds add should produce different ids', function() { var expectedIds = []; var builds = _(100).chain().range().map(function(number) { @@ -45,7 +45,7 @@ describe('Db concurrency', function() { return makeBuild({project: {name: 'project' + number}}); }).value(); - it('put two builds in parallel without errors', function(done) { + it('put builds in parallel without errors', function(done) { Steppy( function() { var putGroup = this.makeGroup(); @@ -68,6 +68,43 @@ describe('Db concurrency', function() { }); }); + describe('prallel builds add/update should produce different ids', function() { + + var expectedIds = []; + var builds = _(200).chain().range().map(function(number) { + expectedIds.push(number + 1); + return makeBuild({project: {name: 'project' + number}}); + }).value(); + + it('put builds in parallel without errors', function(done) { + Steppy( + function() { + var putGroup = this.makeGroup(); + _(builds.slice(0, 190)).each(function(build) { + db.builds.put(build, putGroup.slot()); + }); + }, + function() { + var putGroup = this.makeGroup(); + _(builds).each(function(build) { + db.builds.put(build, putGroup.slot()); + }); + }, + done + ); + }); + + it('shoud have all ' + expectedIds.length +' ids ', function() { + expect(_(builds).chain().pluck('id').sortBy().value()).eql( + expectedIds + ); + }); + + after(function(done) { + db.builds.del(expectedIds, done); + }); + }); + describe('prallel builds put should produce different numbers', function() { var expectedIds = []; @@ -79,7 +116,7 @@ describe('Db concurrency', function() { }); }).value(); - it('put three builds in parallel without errors', function(done) { + it('put builds in parallel without errors', function(done) { Steppy( function() { var putGroup = this.makeGroup();