safe id and build numbers generation

This commit is contained in:
oleg 2015-06-29 21:57:34 +03:00
parent cec79bdc75
commit dc51811eda
4 changed files with 95 additions and 2 deletions

View File

@ -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

10
db.js
View File

@ -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
);

View File

@ -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);
});
});
});
});

View File

@ -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;
};