2013-12-15 21:54:50 +01:00
|
|
|
var transaction = require('../../lib/transaction')
|
2013-10-26 13:43:42 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:11:54 +01:00
|
|
|
describe('Transaction', function() {
|
|
|
|
it('everything is fine', function(cb) {
|
|
|
|
test([true, true, true, true, true], function(err, calls) {
|
|
|
|
assert.deepEqual(err, undefined)
|
|
|
|
assert.deepEqual(calls, [ 'l', 'r0', 'r1', 'r2', 'r3' ])
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2013-10-26 13:43:42 +02:00
|
|
|
|
2013-12-19 16:11:54 +01:00
|
|
|
it("local throws errors - don't call remotes", function(cb) {
|
|
|
|
test([false, true, true, true, true], function(err, calls) {
|
|
|
|
assert.deepEqual(err, 'l')
|
|
|
|
assert.deepEqual(calls, ['l'])
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2013-10-26 13:43:42 +02:00
|
|
|
|
2013-12-19 16:11:54 +01:00
|
|
|
it('remote fails, call all rollbacks', function(cb) {
|
|
|
|
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' ])
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2013-10-26 13:43:42 +02:00
|
|
|
|
2013-12-19 16:11:54 +01:00
|
|
|
it('no remotes', function(cb) {
|
|
|
|
test([true], function(err, calls) {
|
|
|
|
assert.deepEqual(err, undefined)
|
|
|
|
assert.deepEqual(calls, [ 'l' ])
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2013-10-26 13:43:42 +02:00
|
|
|
|
2013-12-19 16:11:54 +01:00
|
|
|
it('all remotes fail', function(cb) {
|
|
|
|
test([true, false, false, false, false], function(err, calls) {
|
|
|
|
assert.deepEqual(err, 'r0')
|
|
|
|
assert.deepEqual(calls, [ 'l', 'r0', 'r1', 'r2', 'r3', 'lb' ])
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2013-10-26 13:43:42 +02:00
|
|
|
|
2013-12-19 16:11:54 +01:00
|
|
|
it('mix', function(cb) {
|
|
|
|
test([true, true, false, true, false], function(err, calls) {
|
|
|
|
assert.deepEqual(err, 'r1')
|
|
|
|
assert.deepEqual(calls, [ 'l', 'r0', 'r1', 'r2', 'r3', 'rb0', 'rb2', 'lb' ])
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2013-10-26 13:43:42 +02:00
|
|
|
})
|
|
|
|
|