Skip to content

Commit c04a812

Browse files
committed
Added test cases for --file-descriptor-limit argument
1 parent fbefe51 commit c04a812

File tree

8 files changed

+194
-0
lines changed

8 files changed

+194
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testfiles
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div xmlns="http://www.w3.org/1999/xhtml" id="mocha"><ul id="mocha-stats"><li class="progress"><canvas width="40" height="40"></canvas></li><li class="passes"><a href="#">passes:</a> <em>2</em></li><li class="failures"><a href="#">failures:</a> <em>0</em></li><li class="duration">duration: <em>3.20</em>s</li></ul><ul id="mocha-report"><li class="suite"><h1><a href="?grep=fd-limit">fd-limit</a></h1><ul><li class="suite"><h1><a href="?grep=fd-limit file descriptor limit">file descriptor limit</a></h1><ul><li class="test pass slow"><h2>fetches 2k files via XHR should succeed with --file-descriptor-limit=8192<span class="duration">1579ms</span> <a href="?grep=fd-limit file descriptor limit fetches 2k files via XHR should succeed with --file-descriptor-limit=8192" class="replay">‣</a></h2><pre style="display: none;"><code>this.timeout(5000);
2+
nw.spawn(['test_app', '--file-descriptor-limit=8192'], function(err, data) {
3+
if (err) throw err;
4+
assert.equal(data.ok, true);
5+
done();
6+
});</code></pre></li><li class="test pass slow"><h2>fetches 2k files via XHR should fail with --file-descriptor-limit=100<span class="duration">679ms</span> <a href="?grep=fd-limit file descriptor limit fetches 2k files via XHR should fail with --file-descriptor-limit=100" class="replay">‣</a></h2><pre style="display: none;"><code>this.timeout(5000);
7+
nw.spawn(['test_app', '--file-descriptor-limit=100'], function(err, data) {
8+
if (err) throw err;
9+
assert.equal(data.ok, false);
10+
done();
11+
});</code></pre></li></ul></li></ul></li></ul></div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>fd-limit</title>
7+
<link href="../res/mocha.css" rel="stylesheet"/>
8+
</head>
9+
<body>
10+
<script src="../res/mocha_util.js"></script>
11+
<script src="mocha_test.js"></script>
12+
</body>
13+
</html>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var assert = require('assert');
2+
var fs = require('fs');
3+
var path = require('path');
4+
var nw = require('./nw');
5+
6+
describe('fd-limit', function() {
7+
8+
describe('file descriptor limit', function() {
9+
10+
before(function(done) {
11+
this.timeout(5000);
12+
fs.mkdir('testfiles', function(err) {
13+
if (err) throw err;
14+
var n = 0;
15+
(function next() {
16+
fs.writeFile('testfiles/' + n + '.json', JSON.stringify({i: n}), function(err) {
17+
if (err) throw err;
18+
19+
if (++n === 2000) {
20+
done();
21+
} else {
22+
next();
23+
}
24+
});
25+
}());
26+
});
27+
});
28+
29+
after(function(done) {
30+
this.timeout(5000);
31+
var n = 0;
32+
(function next() {
33+
fs.unlink('testfiles/' + n + '.json', function(err) {
34+
if (err) throw err;
35+
36+
if (++n === 2000) {
37+
fs.rmdir('testfiles', function(err){
38+
if (err) throw err;
39+
done();
40+
});
41+
} else {
42+
next();
43+
}
44+
});
45+
}());
46+
});
47+
48+
it('fetches 2k files via XHR should succeed with --file-descriptor-limit=8192', function(done) {
49+
this.timeout(5000);
50+
51+
nw.spawn(['test_app', '--file-descriptor-limit=8192'], function(err, data) {
52+
if (err) throw err;
53+
assert.equal(data.ok, true);
54+
done();
55+
});
56+
57+
});
58+
59+
it('fetches 2k files via XHR should fail with --file-descriptor-limit=100', function(done) {
60+
this.timeout(5000);
61+
62+
nw.spawn(['test_app', '--file-descriptor-limit=100'], function(err, data) {
63+
if (err) throw err;
64+
assert.equal(data.ok, false);
65+
done();
66+
});
67+
68+
});
69+
70+
});
71+
72+
});

tests/automation/fd-limit/nw.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var spawn = require('child_process').spawn;
2+
var net = require('net');
3+
4+
exports.port = 13030;
5+
6+
exports.spawn = function(args, cb) {
7+
var shutdown = function (err, data) {
8+
shutdown = function(){};
9+
server.close(function(){
10+
if (err) {
11+
cb(err);
12+
} else {
13+
cb(null, JSON.parse(data));
14+
}
15+
});
16+
};
17+
18+
var server = net.createServer();
19+
server.on('connection', function(con) {
20+
con.on('data', function(data) {
21+
shutdown(null, data);
22+
});
23+
con.on('error', function(err){
24+
shutdown(err);
25+
});
26+
con.on('close', function(){
27+
shutdown(new Error('not-receive'));
28+
});
29+
});
30+
server.listen(exports.port);
31+
32+
return spawn(process.execPath, args);
33+
34+
};
35+
36+
exports.send = function(data) {
37+
var client = net.connect(exports.port);
38+
client.end(JSON.stringify(data));
39+
require('nw.gui').App.quit();
40+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "fd-limit",
3+
"main": "index.html",
4+
"single-instance": false
5+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>fd-limit</title>
7+
</head>
8+
<body>
9+
<script>
10+
var gui = require('nw.gui');
11+
var nw = require('../nw');
12+
13+
get2k();
14+
15+
function get2k() {
16+
var pass = false;
17+
var count = 2000;
18+
for(var i = 0; i < count; i++) {
19+
getFile('../testfiles/'+i+'.json', done);
20+
}
21+
22+
function done(err) {
23+
if (pass) return;
24+
if (err) {
25+
pass = true;
26+
reportDone(false);
27+
} else if (--count === 0) {
28+
reportDone(true);
29+
}
30+
}
31+
}
32+
33+
function getFile(file, done) {
34+
var xhr = new XMLHttpRequest();
35+
xhr.onload = function(){done();};
36+
xhr.onerror = function(){done(true);};
37+
xhr.open('GET', file, true);
38+
xhr.send();
39+
}
40+
41+
function reportDone(result) {
42+
console.log(result);
43+
nw.send({ok: result});
44+
}
45+
</script>
46+
</body>
47+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "fd-limit-test-app",
3+
"main": "index.html",
4+
"single-instance": false
5+
}

0 commit comments

Comments
 (0)