Skip to content

Instantly share code, notes, and snippets.

@SevInf
Last active December 25, 2015 17:09
Show Gist options
  • Select an option

  • Save SevInf/7010893 to your computer and use it in GitHub Desktop.

Select an option

Save SevInf/7010893 to your computer and use it in GitHub Desktop.
test for APW
var assert = require('assert'),
Q = require('Q'),
APW = require('./index.js');
var defers = ['A', 'B', 'C*', 'D*', 'E*', 'F*', 'G*'].reduce(function (res, key) {
res[key] = Q.defer();
return res;
}, {});
function createNode(id, run) {
return {
getId: function() {
return id;
},
run: function() {
return defers[id].promise.then(run);
}
};
}
function shuffle(array) {
var i = array.length, t;
while (i > 0) {
var idx = Math.floor(Math.random() * i--);
t = array[idx];
array[idx] = array[i];
array[i] = t;
}
console.log(array);
return array;
}
exports.getArch = function () {
var array = [],
sum = 0,
arch = new APW.Arch();
arch.setNode(createNode('A', function() {
console.log('A');
console.log(array);
assert.deepEqual(array, [1, 2, 12]);
}));
arch.setNode(createNode('B', function() {
console.log('B');
array.push(1);
var result = arch.withLock(function() {
arch.setNode(createNode('C*', function() {
console.log('C*');
array.push(sum);
}), 'A');
arch.setNode(createNode('D*', function() {
sum += 3;
console.log('D*');
}), 'C*');
arch.setNode(createNode('E*', function() {
sum += 4;
console.log('E*');
}), 'C*');
arch.setNode(createNode('F*', function() {
sum += 5;
console.log('F*');
}), 'C*');
arch.setNode(createNode('G*', function() {
console.log('G*');
array.push(2);
}), ['D*', 'E*', 'F*']);
});
}), 'A');
shuffle(Object.keys(defers)).forEach(function(name) {
defers[name].resolve();
});
return arch;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment