Skip to content

Commit 89ded47

Browse files
committed
add new test case "nw_fork"
1 parent 6ec2f46 commit 89ded47

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

tests/automation/nw_fork/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>nw_fork test</title>
6+
</head>
7+
<body>
8+
<p>nw fork test</p>
9+
10+
<script src="../res/mocha_util.js"></script>
11+
<script src="mocha_test.js"></script>
12+
13+
</body>
14+
15+
</html>
16+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
process.on("message",function(m){
2+
process.send(m);
3+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf8">
5+
<title>test</title>
6+
</head>
7+
<body>
8+
<h1>it works!</h1>
9+
<script type="text/javascript">
10+
var child_process = require('child_process');
11+
var fork = child_process.fork;
12+
var node_name = "node";
13+
if (process.platform == "win32"){
14+
node_name += ".exe";
15+
}
16+
17+
var path = require('path');
18+
var fs = require('fs');
19+
var modulePath = path.join(fs.realpathSync('.'), 'fork_module.js');
20+
21+
var child = fork(modulePath,[],{"execPath":node_name});
22+
var message = "hello world";
23+
var result = false;
24+
var gui = require('nw.gui');
25+
var port = gui.App.argv[0] || 13013;
26+
27+
child.on("message",function(m){
28+
console.log(m);
29+
if (m == message){
30+
result = true;
31+
}
32+
33+
var socket = require('net').connect({port: 13013});
34+
socket.setEncoding('utf8');
35+
socket.end(result.toString());
36+
setTimeout(function(){
37+
gui.App.quit();
38+
},200);
39+
40+
41+
});
42+
43+
child.send(message);
44+
setTimeout(function(){
45+
gui.App.quit();
46+
},2000);
47+
</script>
48+
</body>
49+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name":"test",
3+
"main":"index.html",
4+
"dependencies":{}
5+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var path = require('path');
2+
var assert = require('assert');
3+
var fs = require('fs-extra');
4+
var curDir = fs.realpathSync('.');
5+
6+
var result = false;
7+
describe('fork()',function(){
8+
9+
var child, server, result;
10+
11+
before(function(done){
12+
this.timeout(0);
13+
server = createTCPServer(13013);
14+
child = spawnChildProcess(path.join(curDir, 'internal'));
15+
16+
server.on('connection', function(socket) {
17+
socket.setEncoding('utf8');
18+
socket.on('data', function(data) {
19+
result = JSON.parse(data+'');
20+
});
21+
});
22+
23+
child.on('exit',function(){
24+
done();
25+
});
26+
27+
28+
});
29+
30+
after(function(done){
31+
server.close();
32+
child.kill();
33+
done();
34+
});
35+
36+
37+
38+
it("child_process.fork() should have a workaround solution",function(done){
39+
assert.equal(result,true);
40+
done();
41+
});
42+
43+
});
44+

tests/automation/nw_fork/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name":"nwfork_wrapper",
3+
"main":"index.html"
4+
}

0 commit comments

Comments
 (0)