Skip to content

Commit 30a0c25

Browse files
committed
[test] Update test cases
Update a lot of test cases: fix some errors and refactor some cases if necessary to help node quit in a better way. Fix: #108
1 parent 60a6e94 commit 30a0c25

17 files changed

+174
-148
lines changed

test/client_setup.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ rclnodejs.init().then(function() {
2727
process.stdout.write(response.sum.toString());
2828
});
2929
rclnodejs.spin(node);
30-
setTimeout(function() {
30+
31+
process.on('SIGINT', function() {
3132
node.destroy();
3233
rclnodejs.shutdown();
33-
}, 1 * 1000);
34+
process.exit(0);
35+
});
3436
}).catch(function(err) {
3537
console.log(err);
3638
});

test/publisher_array_setup.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
const rclnodejs = require('../index.js');
1818

1919
rclnodejs.init().then(function() {
20-
const node = rclnodejs.createNode('array_message_publisher');
20+
var node = rclnodejs.createNode('array_message_publisher');
2121

22-
let Header = rclnodejs.require('std_msgs').msg.Header;
23-
let Time = rclnodejs.require('builtin_interfaces').msg.Time;
24-
let JointState = rclnodejs.require('sensor_msgs').msg.JointState;
22+
const Header = rclnodejs.require('std_msgs').msg.Header;
23+
const Time = rclnodejs.require('builtin_interfaces').msg.Time;
24+
const JointState = rclnodejs.require('sensor_msgs').msg.JointState;
2525

26-
const publisher = node.createPublisher(JointState, 'JointState');
26+
var publisher = node.createPublisher(JointState, 'JointState');
2727

2828
let time = new Time();
2929
time.sec = 123456;
@@ -44,10 +44,11 @@ rclnodejs.init().then(function() {
4444
publisher.publish(state);
4545
rclnodejs.spin(node);
4646

47-
setTimeout(() => {
47+
process.on('SIGINT', function() {
4848
node.destroy();
4949
rclnodejs.shutdown();
50-
}, 100);
50+
process.exit(0);
51+
});
5152
}).catch(function(err) {
5253
console.log(err);
5354
});

test/publisher_msg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ rclnodejs.init().then(() => {
3131
}, 100);
3232

3333
rclnodejs.spin(node);
34-
process.on('message', (m) => {
34+
process.on('SIGINT', (m) => {
3535
clearInterval(timer);
3636
node.destroy();
3737
rclnodejs.shutdown();

test/publisher_setup.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ const rclnodejs = require('../index.js');
1818

1919
rclnodejs.init().then(function() {
2020
var node = rclnodejs.createNode('publisher');
21-
var RclString = rclnodejs.require('std_msgs').msg.String;
22-
var publisher = node.createPublisher(RclString, 'topic');
23-
var msg = new RclString();
21+
const RclString = rclnodejs.require('std_msgs').msg.String;
22+
23+
let msg = new RclString();
2424
msg.data = 'Greeting from publisher';
25+
26+
var publisher = node.createPublisher(RclString, 'topic');
2527
publisher.publish(msg);
2628
rclnodejs.spin(node);
2729

28-
setTimeout(function() {
30+
process.on('SIGINT', function() {
2931
node.destroy();
3032
rclnodejs.shutdown();
31-
}, 1 * 1000);
33+
process.exit(0);
34+
});
3235
}).catch(function(err) {
3336
console.log(err);
3437
});

test/py/listener.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ def handler(signum, frame):
3030
cleanup()
3131
sys.exit(0)
3232

33-
signal.signal(signal.SIGINT, handler)
34-
3533
def callback(msg):
3634
sys.stdout.write(msg.data)
3735
sys.stdout.flush()
3836

3937
def main():
4038
global node
39+
signal.signal(signal.SIGINT, handler)
40+
4141
rclpy.init()
4242
node = rclpy.create_node('py_listener')
43-
publisher = node.create_subscription(String, 'js_py_chatter', callback)
43+
subscription = node.create_subscription(String, 'js_py_chatter', callback)
4444
while rclpy.ok():
4545
rclpy.spin_once(node)
4646

test/test-array.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ const childProcess = require('child_process');
1919
const rclnodejs = require('../index.js');
2020

2121
describe('rclnodejs message communication', function() {
22+
this.timeout(60 * 1000);
23+
2224
before(function() {
23-
this.timeout(60 * 1000);
2425
return rclnodejs.init();
2526
});
2627

@@ -29,9 +30,10 @@ describe('rclnodejs message communication', function() {
2930
});
3031

3132
it('should support array type', function(done) {
32-
this.timeout(10 * 1000);
3333
var node = rclnodejs.createNode('array_message_subscription');
34-
let JointState = rclnodejs.require('sensor_msgs').msg.JointState;
34+
const JointState = rclnodejs.require('sensor_msgs').msg.JointState;
35+
var publisher = childProcess.fork(`${__dirname}/publisher_array_setup.js`);
36+
var destroy = false;
3537
var subscription = node.createSubscription(JointState, 'JointState', (state) => {
3638
assert.deepStrictEqual(state.header.stamp.sec, 123456);
3739
assert.deepStrictEqual(state.header.stamp.nanosec, 789);
@@ -41,11 +43,13 @@ describe('rclnodejs message communication', function() {
4143
assert.deepStrictEqual(state.velocity, [2, 3]);
4244
assert.deepStrictEqual(state.effort, [4, 5, 6]);
4345

44-
node.destroy();
45-
done();
46+
if (!destroy) {
47+
publisher.kill('SIGINT');
48+
node.destroy();
49+
destroy = true;
50+
done();
51+
}
4652
});
4753
rclnodejs.spin(node);
48-
49-
childProcess.spawn('node', ['publisher_array_setup.js'], {cwd: __dirname});
5054
});
5155
});

test/test-cross-lang.js

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,23 @@ const assert = require('assert');
1818
const path = require('path');
1919
const childProcess = require('child_process');
2020
const rclnodejs = require('../index.js');
21+
const utils = require('./utils.js');
2122

2223
describe('Cross-language interaction', function() {
23-
describe('Node.js Subcription', function() {
24-
this.timeout(60 * 1000);
24+
this.timeout(60 * 1000);
2525

26-
before(function() {
27-
return rclnodejs.init();
28-
});
26+
before(function() {
27+
return rclnodejs.init();
28+
});
2929

30-
after(function() {
31-
rclnodejs.shutdown();
32-
});
30+
after(function() {
31+
rclnodejs.shutdown();
32+
});
3333

34+
describe('Node.js Subcription', function() {
3435
it('Node.js subscription should receive msg from C++ publisher', (done) => {
3536
var node = rclnodejs.createNode('cpp_pub_js_sub');
36-
var rclString = rclnodejs.require('std_msgs').msg.String;
37+
const rclString = rclnodejs.require('std_msgs').msg.String;
3738
var destroy = false;
3839
var cppTalkPath = path.join(process.env['AMENT_PREFIX_PATH'], 'lib', 'demo_nodes_cpp', 'talker');
3940
var cppTalker = childProcess.spawn(cppTalkPath);
@@ -42,45 +43,35 @@ describe('Cross-language interaction', function() {
4243
if (!destroy) {
4344
node.destroy();
4445
cppTalker.kill('SIGINT');
45-
destroy = true;
46+
destroy = true;
47+
done();
4648
}
47-
done();
4849
});
4950
rclnodejs.spin(node);
5051
});
5152

5253
it('Node.js subscription should receive msg from Python publisher', (done) => {
5354
var node = rclnodejs.createNode('cpp_pub_py_sub');
54-
var rclString = rclnodejs.require('std_msgs').msg.String;
55+
const rclString = rclnodejs.require('std_msgs').msg.String;
5556
var destroy = false;
56-
var pyTalker = childProcess.spawn('python3', [`${__dirname}/py/talker.py`]);
57+
var pyTalker = utils.launchPythonProcess([`${__dirname}/py/talker.py`]);
5758
var subscription = node.createSubscription(rclString, 'py_js_chatter', (msg) => {
5859
assert.ok(/Hello World/.test(msg.data));
5960
if (!destroy) {
6061
node.destroy();
6162
pyTalker.kill('SIGINT');
62-
destroy = true;
63+
destroy = true;
64+
done();
6365
}
64-
done();
6566
});
6667
rclnodejs.spin(node);
6768
});
6869
});
6970

7071
describe('Node.js publisher', function() {
71-
this.timeout(60 * 1000);
72-
73-
before(function() {
74-
return rclnodejs.init();
75-
});
76-
77-
after(function() {
78-
rclnodejs.shutdown();
79-
});
80-
8172
it('Cpp subscription should receive msg from Node.js publisher', (done) => {
8273
var node = rclnodejs.createNode('js_pub_cpp_sub');
83-
var rclString = rclnodejs.require('std_msgs').msg.String;
74+
const rclString = rclnodejs.require('std_msgs').msg.String;
8475
var destroy = false;
8576

8677
let text = 'Greeting from Node.js publisher';
@@ -95,41 +86,41 @@ describe('Cross-language interaction', function() {
9586

9687
cppListener.stdout.on('data', (data) => {
9788
if (!destroy) {
98-
clearInterval(timer);
9989
assert.ok(new RegExp(text).test(data.toString()));
100-
done();
90+
clearInterval(timer);
10191
node.destroy();
10292
cppListener.kill('SIGINT');
10393
destroy = true;
94+
done();
10495
}
10596
});
10697
rclnodejs.spin(node);
10798
});
10899

109100
it('Python subscription should receive msg from Node.js publisher', function(done) {
110101
var node = rclnodejs.createNode('js_pub_py_sub');
111-
var rclString = rclnodejs.require('std_msgs').msg.String;
102+
const rclString = rclnodejs.require('std_msgs').msg.String;
112103
var destroy = false;
113104

114-
let text = 'Greeting from Node.js publisher';
115-
var pyListener = childProcess.spawn('python3', [`${__dirname}/py/listener.py`]);
105+
let text = 'Greeting from Node.js publisher to Python subscription';
106+
var pyListener = utils.launchPythonProcess([`${__dirname}/py/listener.py`]);
116107
var publisher = node.createPublisher(rclString, 'js_py_chatter');
117108
var msg = new rclString();
118109
msg.data = text;
119110

111+
var timer = setInterval(() => {
112+
publisher.publish(msg);
113+
}, 100);
120114
pyListener.stdout.on('data', (data) => {
121115
if (!destroy) {
122-
clearInterval(timer);
123116
assert.ok(new RegExp(text).test(data.toString()));
124-
done();
117+
clearInterval(timer);
125118
node.destroy();
126119
pyListener.kill('SIGINT');
127120
destroy = true;
121+
done();
128122
}
129123
});
130-
var timer = setInterval(() => {
131-
publisher.publish(msg);
132-
}, 100);
133124
rclnodejs.spin(node);
134125
});
135126
});

test/test-destruction.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const assert = require('assert');
1818
const rclnodejs = require('../index.js');
1919

2020
describe('Node destroy testing', function() {
21+
this.timeout(60 * 1000);
22+
2123
before(function() {
22-
this.timeout(60 * 1000);
2324
return rclnodejs.init();
2425
});
2526

test/test-existance.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ describe('rclnodejs module existance testing', function() {
4949
});
5050

5151
describe('rclnodejs class existance testing', function() {
52+
this.timeout(60 * 1000);
53+
5254
before(function() {
53-
this.timeout(60 * 1000);
5455
return rclnodejs.init();
5556
});
5657

test/test-init-shutdown.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ const assert = require('assert');
1818
const rclnodejs = require('../index.js');
1919

2020
describe('Node destroy testing', function() {
21-
before(function() {
22-
this.timeout(60 * 1000);
23-
});
21+
this.timeout(60 * 1000);
2422

2523
it('rclnodejs.init()', function(done) {
2624
rclnodejs.init().then(function() {

0 commit comments

Comments
 (0)