0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-30 22:34:10 -05:00

transaction stub

This commit is contained in:
Alex Kocharin 2013-10-26 15:43:42 +04:00
parent 3f8166fa19
commit 5da3c8ba58
3 changed files with 120 additions and 16 deletions

View file

@ -1,16 +0,0 @@
/*
module.exports = function() {
local_store()
for each uplink
status = uplink_store()
if (status == 201) {
succeess.push(uplink)
} else {
bail()
}
bail:
local_revert()
uplink_revert()
};
*/

46
lib/transaction.js Normal file
View file

@ -0,0 +1,46 @@
var async = require('async')
//
// Function performs a certain task on a multiple uplinks
// and reverts changes if something fails
//
// uplinks - list of uplinks not counting local
// localAction, localRollback - function(cb)
// remoteAction, remoteRollback - function(uplink, cb)
//
module.exports = function(uplinks, localAction, localRollback, remoteAction, remoteRollback, callback) {
var uplink_ids = uplinks.map(function(_, i) {
return i
})
localAction(function(err) {
if (err) return callback(err)
async.map(uplink_ids, function(i, cb) {
remoteAction(uplinks[i], function(err) {
cb(null, err)
})
}, function(err, res) {
var return_err = err
// let err be first non-null element in the array
for (var i=0; i<res.length; i++) {
if (return_err) break
return_err = res[i]
}
if (!return_err) return callback()
async.map(uplink_ids, function(i, cb) {
if (res[i]) return cb()
remoteRollback(uplinks[i], function() {
cb()
})
}, function(err) {
localRollback(function() {
callback(return_err)
})
})
})
})
}

74
test/transaction.js Normal file
View file

@ -0,0 +1,74 @@
var transaction = require('../lib/transaction')
var assert = require('assert')
function call_back(cb, value) {
setTimeout(function() {
cb(value)
}, Math.random()*30)
}
function test(uplinks, cb) {
var calls = []
var local = uplinks.shift()
transaction(
uplinks.map(
function(x, i) {return [i, x]}
),
function localAction(cb) {
calls.push('l')
call_back(cb, !local ? 'l' : null)
},
function localRollback(cb) {
calls.push('lb')
call_back(cb, true)
},
function remoteAction(remote, cb) {
calls.push('r'+remote[0])
call_back(cb, !remote[1] ? 'r'+remote[0] : null)
},
function remoteRollback(remote, cb) {
calls.push('rb'+remote[0])
call_back(cb, true)
},
function callback(err) {
cb(err, calls)
}
)
}
// everything is fine
test([true, true, true, true, true], function(err, calls) {
assert.deepEqual(err, undefined)
assert.deepEqual(calls, [ 'l', 'r0', 'r1', 'r2', 'r3' ])
})
// local throws errors - don't call remotes
test([false, true, true, true, true], function(err, calls) {
assert.deepEqual(err, 'l')
assert.deepEqual(calls, ['l'])
})
// remote fails, call all rollbacks
test([true, true, true, false, true], function(err, calls) {
assert.deepEqual(err, 'r2')
assert.deepEqual(calls, [ 'l', 'r0', 'r1', 'r2', 'r3', 'rb0', 'rb1', 'rb3', 'lb' ])
})
// no remotes
test([true], function(err, calls) {
assert.deepEqual(err, undefined)
assert.deepEqual(calls, [ 'l' ])
})
// all remotes fail
test([true, false, false, false, false], function(err, calls) {
assert.deepEqual(err, 'r0')
assert.deepEqual(calls, [ 'l', 'r0', 'r1', 'r2', 'r3', 'lb' ])
})
// mix
test([true, true, false, true, false], function(err, calls) {
assert.deepEqual(err, 'r1')
assert.deepEqual(calls, [ 'l', 'r0', 'r1', 'r2', 'r3', 'rb0', 'rb2', 'lb' ])
})