diff options
author | Mickael van der Beek <mickael@sagacify.com> | 2014-11-18 21:20:56 +0100 |
---|---|---|
committer | Mickael van der Beek <mickael@sagacify.com> | 2014-11-18 21:20:56 +0100 |
commit | 2410077e26d5429ac45533438f008706dcb989f7 (patch) | |
tree | 7f256954934f46a8efbebeb0f456b399f0d96ea4 | |
parent | 232121251f601c9be7209f6382ee32180df676f1 (diff) | |
download | async-2410077e26d5429ac45533438f008706dcb989f7.tar.gz |
Fixes caolan/async#263 and other dependency issues by detecting dead-locks and throwing an Error.
-rwxr-xr-x | lib/async.js | 11 | ||||
-rwxr-xr-x | test/test-async.js | 27 |
2 files changed, 38 insertions, 0 deletions
diff --git a/lib/async.js b/lib/async.js index a13f835..50f7a28 100755 --- a/lib/async.js +++ b/lib/async.js @@ -478,6 +478,17 @@ } }; var requires = task.slice(0, Math.abs(task.length - 1)) || []; + // prevent dead-locks + var len = requires.length; + var dep; + while (len--) { + if (!(dep = tasks[requires[len]])) { + throw new Error('Has inexistant dependency'); + } + if (_isArray(dep) && !!~dep.indexOf(k)) { + throw new Error('Has cyclic dependencies'); + } + } var ready = function () { return _reduce(requires, function (a, x) { return (a && results.hasOwnProperty(x)); diff --git a/test/test-async.js b/test/test-async.js index 6a35606..8ee2bd9 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -580,6 +580,33 @@ exports['auto modifying results causes final callback to run early'] = function( }); }; +// Issue 263 on github: https://github.com/caolan/async/issues/263 +exports['auto prevent dead-locks due to inexistant dependencies'] = function(test) { + test.throws(function () { + async.auto({ + task1: ['noexist', function(callback, results){ + callback(null, 'task1'); + }] + }); + }, Error); + test.done(); +}; + +// Issue 263 on github: https://github.com/caolan/async/issues/263 +exports['auto prevent dead-locks due to cyclic dependencies'] = function(test) { + test.throws(function () { + async.auto({ + task3: ['task4', function(callback, results){ + callback(null, 'task3'); + }], + task4: ['task3', function(callback, results){ + callback(null, 'task4'); + }] + }); + }, Error); + test.done(); +}; + // Issue 306 on github: https://github.com/caolan/async/issues/306 exports['retry when attempt succeeds'] = function(test) { var failed = 3 |