Skip to content

Commit 0796d42

Browse files
committed
Merge pull request nwjs#2851 from ghostoy/fd-limit-automation
Added test cases for --file-descriptor-limit argument
2 parents 29f2896 + 238b936 commit 0796d42

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
testfiles
2+
fd-limit.xml
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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
if (process.platform === 'win32') {
50+
assert.equal(true, true);
51+
done();
52+
}
53+
54+
this.timeout(5000);
55+
56+
nw.spawn(['test_app', '--file-descriptor-limit=8192'], function(err, data) {
57+
if (err) throw err;
58+
assert.equal(data.ok, true);
59+
done();
60+
});
61+
62+
});
63+
64+
it('fetches 2k files via XHR should fail with --file-descriptor-limit=100', function(done) {
65+
if (process.platform === 'win32') {
66+
assert.equal(true, true);
67+
done();
68+
}
69+
70+
this.timeout(5000);
71+
72+
nw.spawn(['test_app', '--file-descriptor-limit=100'], function(err, data) {
73+
if (err) throw err;
74+
assert.equal(data.ok, false);
75+
done();
76+
});
77+
78+
});
79+
80+
});
81+
82+
});

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)