arnaucube
arnaucube/comunicationLeapPublic Self-hosted repository
comunicationLeap/node_modules/mkdirp/test/race.js
node_modules/mkdirp/test/race.jsRaw
 1var mkdirp = require('../').mkdirp;
 2var path = require('path');
 3var fs = require('fs');
 4var test = require('tap').test;
 5
 6test('race', function (t) {
 7    t.plan(4);
 8    var ps = [ '', 'tmp' ];
 9    
10    for (var i = 0; i < 25; i++) {
11        var dir = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
12        ps.push(dir);
13    }
14    var file = ps.join('/');
15    
16    var res = 2;
17    mk(file, function () {
18        if (--res === 0) t.end();
19    });
20    
21    mk(file, function () {
22        if (--res === 0) t.end();
23    });
24    
25    function mk (file, cb) {
26        mkdirp(file, 0755, function (err) {
27            if (err) t.fail(err);
28            else path.exists(file, function (ex) {
29                if (!ex) t.fail('file not created')
30                else fs.stat(file, function (err, stat) {
31                    if (err) t.fail(err)
32                    else {
33                        t.equal(stat.mode & 0777, 0755);
34                        t.ok(stat.isDirectory(), 'target not a directory');
35                        if (cb) cb();
36                    }
37                })
38            })
39        });
40    }
41});