From dc51811edaafcf19771ef251970ebfcb62b2896d Mon Sep 17 00:00:00 2001 From: oleg Date: Mon, 29 Jun 2015 21:57:34 +0300 Subject: [PATCH] safe id and build numbers generation --- README.md | 2 +- db.js | 10 ++++++ test/db/concurrent.js | 74 +++++++++++++++++++++++++++++++++++++++++++ test/helpers.js | 11 ++++++- 4 files changed, 95 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3b83a04..04e5778 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ work in progress... * ~~Rename notification strategies according to statuses~~ * Work with git * ~~Build every commit, commit with tag, etc~~ -* Safe id and build numbers generation +* ~~Safe id and build numbers generation~~ * Scheduler * ~~Better tests coverage~~ * Semantic versioning and plugins diff --git a/db.js b/db.js index f21a599..eb5a827 100644 --- a/db.js +++ b/db.js @@ -32,6 +32,14 @@ exports.init = function(dbPath, params, callback) { Steppy( function() { + if (self._beforePutInProgress) { + return setTimeout(function() { + exports.builds._beforePut.call(self, builds, callback); + }, 5); + } + + self._beforePutInProgress = true; + if (builds.length > 1) { throw new Error('Build put hooks work only with single build'); } @@ -60,6 +68,8 @@ exports.init = function(dbPath, params, callback) { } this.pass(null); + + self._beforePutInProgress = false; }, callback ); diff --git a/test/db/concurrent.js b/test/db/concurrent.js index 6cf7aaf..3e0a9ca 100644 --- a/test/db/concurrent.js +++ b/test/db/concurrent.js @@ -1,5 +1,79 @@ 'use strict'; +var expect = require('expect.js'), + helpers = require('../helpers'), + Steppy = require('twostep').Steppy, + _ = require('underscore'); + describe('Db concurrency', function() { + var db; + + var madeBuildIndex = 0; + var makeBuild = function(build) { + return _({ + // to increase build numbers + createDate: Date.now() + madeBuildIndex++, + project: _({}).extend(build && build.project) + }).extend(build); + }; + + before(function(done) { + db = helpers.initDb(done); + }); + + describe('prallel builds put should produce different ids', function() { + + var firstBuild = makeBuild({project: {name: 'project1'}}), + secondBuild = makeBuild({project: {name: 'project2'}}); + + it('put two builds in parallel without errors', function(done) { + Steppy( + function() { + db.builds.put(firstBuild, this.slot()); + db.builds.put(secondBuild, this.slot()); + }, + done + ); + }); + + it('first build should have id 1', function() { + expect(firstBuild.id).equal(1); + }); + + it('secondBuild build should have id 2', function() { + expect(secondBuild.id).equal(2); + }); + + }); + + describe('prallel builds put should produce different numbers', function() { + + var builds = _(3).chain().range().map(function() { + return makeBuild({ + project: {name: 'project1'}, + status: 'in-progress' + }); + }).value(); + + it('put three builds in parallel without errors', function(done) { + Steppy( + function() { + var step = this; + _(builds).each(function(build) { + db.builds.put(build, step.slot()); + }); + }, + done + ); + }); + + _(builds).each(function(build, index) { + var number = (index + 1); + it('build ' + number + ' should have number ' + number, function() { + expect(build.number).equal(number); + }); + }); + + }); }); diff --git a/test/helpers.js b/test/helpers.js index 7d9c083..8eed480 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -1,7 +1,8 @@ 'use strict'; var SpawnCommand = require('../lib/command/spawn').Command, - fs = require('fs'); + fs = require('fs'), + db = require('../db'); exports.removeDir = function(dir, callback) { @@ -42,3 +43,11 @@ exports.mercurialRevs = [{ date: new Date('Sun Jun 28 10:54:22 2015 +0300').getTime(), comment: 'add tags' }]; + +exports.initDb = function(callback) { + db.init('path/to/db/ignored/for/memdown', { + db: require('memdown'), + valueEncoding: 'json' + }, callback); + return db; +};