Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
986 views
in Technique[技术] by (71.8m points)

amazon web services - Node.js Mocha async test doesn't return from callbacks

I'm utterly confused on how to wrap nested async callbacks in a Mocha test. Here is the offending code sample: It's calling Amazon S3 to check that files exist:

var should = require('should');
var appConfig = require("../config.js");
var fs = require('fs');
var async = require('async');
var s3 = require('aws2js').load('s3', appConfig.awsAccessKeyId, appConfig.awsSecretAccessKey);
s3.setBucket(appConfig.awsBucketName);

var test_user_id = 590170571;
var user_file_code = test_user_id * 2;
var output_file_template = ['wordcloud_r', 'polarity_r', 'emot_cat_r'];

describe('Should show uploaded  files to amazon s3', function () {
    it.only('should upload three local graphics files to Amazon S3 bucket', function (done) {
        async.each(output_file_template, function (test_file, cb) {
            console.log(test_file);
            s3.head('reports/dsfsdf.dff', function (err, res) {
                if (err) {
                    console.log(err)
                }
                console.log(res)
                cb(null);
              // done(); //nope  
            });
              // done(); //nope
        });
        // done(); //nope
    });
});

Either code hangs waiting to complete (if I omit done() ) - or, the code completes without callbacks, or, node complains that done() was called multiple times.

With the help below, I sort of got it working, but it looks like asynchronous voodoo stew

 it.only('should upload three local graphics files to Amazon S3 bucket', function (done) {
        async.series([function (callback) {
            async.each(output_file_template, function (test_file, cb) {
                console.log(test_file);
                s3.head('reports/dsfsdf.dff', function (err, res) {
                    if (err) {
                        console.log(err)
                    }
                    console.log(res)
                    cb();
                    callback();
                });

            });

        }, function (callback) {
            done();
            callback();
        }]);

    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use the asynchronous support in mocha. Try adding done to the following line:

describe('Should show uploaded  files to amazon s3', function (done) {

and you need to add done() below the console.log(res).

Documentation is here: http://visionmedia.github.io/mocha/#asynchronous-code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...