summaryrefslogtreecommitdiff
path: root/test/timeout.js
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2018-06-02 18:35:23 -0700
committerAlexander Early <alexander.early@gmail.com>2018-06-02 18:35:32 -0700
commitbd86f42a7d71552d9a502b50235ffc090a1b4a98 (patch)
treef5170115927ad4097f1a393d34b8a52f5c9bbfeb /test/timeout.js
parentb149f7dcc7daba4d7bac5a313bdf6d1a85210cb1 (diff)
downloadasync-bd86f42a7d71552d9a502b50235ffc090a1b4a98.tar.gz
move mocha_tests/ to test/
Diffstat (limited to 'test/timeout.js')
-rw-r--r--test/timeout.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/test/timeout.js b/test/timeout.js
new file mode 100644
index 0000000..cd4a751
--- /dev/null
+++ b/test/timeout.js
@@ -0,0 +1,108 @@
+var async = require('../lib');
+var expect = require('chai').expect;
+
+describe('timeout', function () {
+
+ it('timeout with series', function(done){
+ async.series([
+ async.timeout(function asyncFn(callback) {
+ setTimeout(function() {
+ callback(null, 'I didn\'t time out');
+ }, 25);
+ }, 50),
+ async.timeout(function asyncFn(callback) {
+ setTimeout(function() {
+ callback(null, 'I will time out');
+ }, 75);
+ }, 50)
+ ],
+ function(err, results) {
+ expect(err.message).to.equal('Callback function "asyncFn" timed out.');
+ expect(err.code).to.equal('ETIMEDOUT');
+ expect(err.info).to.equal(undefined);
+ expect(results[0]).to.equal('I didn\'t time out');
+ done();
+ });
+ });
+
+ it('timeout with series and info', function (done) {
+ var info = { custom: 'info about callback' };
+ async.series([
+ async.timeout(function asyncFn(callback) {
+ setTimeout(function() {
+ callback(null, 'I didn\'t time out');
+ }, 25);
+ }, 50),
+ async.timeout(function asyncFn(callback) {
+ setTimeout(function() {
+ callback(null, 'I will time out');
+ }, 75);
+ }, 50, info)
+ ],
+ function(err, results) {
+ expect(err.message).to.equal('Callback function "asyncFn" timed out.');
+ expect(err.code).to.equal('ETIMEDOUT');
+ expect(err.info).to.equal(info);
+ expect(results[0]).to.equal('I didn\'t time out');
+ done();
+ });
+ });
+
+ it('timeout with parallel', function(done){
+ async.parallel([
+ async.timeout(function asyncFn(callback) {
+ setTimeout(function() {
+ callback(null, 'I didn\'t time out');
+ }, 25);
+ }, 50),
+ async.timeout(function asyncFn(callback) {
+ setTimeout(function() {
+ callback(null, 'I will time out');
+ }, 75);
+ }, 50)
+ ],
+ function(err, results) {
+ expect(err.message).to.equal('Callback function "asyncFn" timed out.');
+ expect(err.code).to.equal('ETIMEDOUT');
+ expect(err.info).to.equal(undefined);
+ expect(results[0]).to.equal('I didn\'t time out');
+ done();
+ });
+ });
+
+ it('timeout with multiple calls (#1418)', function(done) {
+ var timeout = async.timeout(function asyncFn(n, callback) {
+ if (n < 1) {
+ setTimeout(function() {
+ callback(null, 'I will time out');
+ }, 75);
+ } else {
+ async.setImmediate(function() {
+ callback(null, 'I didn\'t time out');
+ })
+ }
+ }, 50);
+
+ async.series([
+ function(cb) {
+ timeout(0, function(err, result) {
+ expect(err.message).to.equal('Callback function "asyncFn" timed out.');
+ expect(err.code).to.equal('ETIMEDOUT');
+ expect(err.info).to.equal(undefined);
+ expect(result).to.equal(undefined);
+ cb();
+ });
+ },
+ function(cb) {
+ timeout(1, function(err, result) {
+ expect(err).to.equal(null);
+ expect(result).to.equal('I didn\'t time out');
+ cb();
+ });
+ }
+ ], function(err) {
+ expect(err).to.equal(null);
+ done();
+ });
+ })
+});