Skip to content

Commit 5037348

Browse files
author
Minggang Wang
authored
Merge pull request #159 from qiuzhong/multi-nodes
[test]Add 6 test cases for rclnodejs-C++ nodes testing.
2 parents 76320df + e943cfe commit 5037348

File tree

8 files changed

+309
-18
lines changed

8 files changed

+309
-18
lines changed

test/py/client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# you may not use this file except in compliance with the License.
66
# You may obtain a copy of the License at
77
#
8-
# http:#www.apache.org/licenses/LICENSE-2.0
8+
# http://www.apache.org/licenses/LICENSE-2.0
99
#
1010
# Unless required by applicable law or agreed to in writing, software
1111
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -38,9 +38,13 @@ def main():
3838
global node
3939
signal.signal(signal.SIGINT, handler)
4040

41+
service = 'py_js_add_two_ints'
42+
if len(sys.argv) > 1:
43+
service = sys.argv[1]
44+
4145
rclpy.init()
4246
node = rclpy.create_node('add_client')
43-
client = node.create_client(AddTwoInts, 'js_py_add_service')
47+
client = node.create_client(AddTwoInts, service)
4448
request = AddTwoInts.Request()
4549
request.a = 1
4650
request.b = 2

test/py/listener.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# you may not use this file except in compliance with the License.
66
# You may obtain a copy of the License at
77
#
8-
# http:#www.apache.org/licenses/LICENSE-2.0
8+
# http://www.apache.org/licenses/LICENSE-2.0
99
#
1010
# Unless required by applicable law or agreed to in writing, software
1111
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -38,9 +38,12 @@ def main():
3838
global node
3939
signal.signal(signal.SIGINT, handler)
4040

41+
topic = 'js_py_chatter'
42+
if len(sys.argv) > 1:
43+
topic = sys.argv[1]
4144
rclpy.init()
4245
node = rclpy.create_node('py_listener')
43-
subscription = node.create_subscription(String, 'js_py_chatter', callback)
46+
subscription = node.create_subscription(String, topic, callback)
4447
while rclpy.ok():
4548
rclpy.spin_once(node)
4649

test/py/publisher_msg.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
#!/usr/bin/env python3
2+
# Copyright (c) 2017 Intel Corporation. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
215

316
import sys
417
import signal

test/py/service.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# you may not use this file except in compliance with the License.
66
# You may obtain a copy of the License at
77
#
8-
# http:#www.apache.org/licenses/LICENSE-2.0
8+
# http://www.apache.org/licenses/LICENSE-2.0
99
#
1010
# Unless required by applicable law or agreed to in writing, software
1111
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -39,9 +39,13 @@ def main():
3939
global node
4040
signal.signal(signal.SIGINT, handler)
4141

42+
service = 'js_py_add_two_ints'
43+
if len(sys.argv) > 1:
44+
service = sys.argv[1]
45+
4246
rclpy.init()
4347
node = rclpy.create_node('add_service')
44-
service = node.create_service(AddTwoInts, 'py_js_add_service', callback)
48+
service = node.create_service(AddTwoInts, service, callback)
4549
while rclpy.ok():
4650
rclpy.spin_once(node)
4751

test/py/subscription_msg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# you may not use this file except in compliance with the License.
66
# You may obtain a copy of the License at
77
#
8-
# http:#www.apache.org/licenses/LICENSE-2.0
8+
# http://www.apache.org/licenses/LICENSE-2.0
99
#
1010
# Unless required by applicable law or agreed to in writing, software
1111
# distributed under the License is distributed on an "AS IS" BASIS,

test/py/talker.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# you may not use this file except in compliance with the License.
66
# You may obtain a copy of the License at
77
#
8-
# http:#www.apache.org/licenses/LICENSE-2.0
8+
# http://www.apache.org/licenses/LICENSE-2.0
99
#
1010
# Unless required by applicable law or agreed to in writing, software
1111
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -35,9 +35,13 @@ def main():
3535
global node
3636
signal.signal(signal.SIGINT, handler)
3737

38+
topic = 'py_js_chatter'
39+
if len(sys.argv) > 1:
40+
topic = sys.argv[1]
41+
3842
rclpy.init()
3943
node = rclpy.create_node('py_talker')
40-
publisher = node.create_publisher(String, 'py_js_chatter')
44+
publisher = node.create_publisher(String, topic)
4145

4246
msg = String()
4347
msg.data = 'Hello World'

test/test-cross-lang.js

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ describe('Cross-language interaction', function() {
3737
const rclString = rclnodejs.require('std_msgs').msg.String;
3838
var destroy = false;
3939
var cppTalkPath = path.join(process.env['AMENT_PREFIX_PATH'], 'lib', 'demo_nodes_cpp', 'talker');
40-
var cppTalker = childProcess.spawn(cppTalkPath);
41-
var subscription = node.createSubscription(rclString, 'chatter', (msg) => {
40+
var cppTalker = childProcess.spawn(cppTalkPath, ['-t', 'cpp_js_chatter']);
41+
var subscription = node.createSubscription(rclString, 'cpp_js_chatter', (msg) => {
4242
assert.ok(/Hello World:/.test(msg.data));
4343
if (!destroy) {
4444
node.destroy();
@@ -76,8 +76,8 @@ describe('Cross-language interaction', function() {
7676

7777
let text = 'Greeting from Node.js publisher';
7878
let cppListenerPath = path.join(process.env['AMENT_PREFIX_PATH'], 'lib', 'demo_nodes_cpp', 'listener');
79-
var cppListener = childProcess.spawn(cppListenerPath);
80-
var publisher = node.createPublisher(rclString, 'chatter');
79+
var cppListener = childProcess.spawn(cppListenerPath, ['-t', 'js_cpp_chatter']);
80+
var publisher = node.createPublisher(rclString, 'js_cpp_chatter');
8181
var msg = new rclString();
8282
msg.data = text;
8383
var timer = setInterval(() => {
@@ -127,12 +127,12 @@ describe('Cross-language interaction', function() {
127127

128128
describe('Node.js client', function() {
129129
it('Node.js client should work with Python service', function(done) {
130-
var node = rclnodejs.createNode('js_add_client');
130+
var node = rclnodejs.createNode('js_py_add_client');
131131
const AddTwoInts = rclnodejs.require('example_interfaces').srv.AddTwoInts;
132132
var destroy = false;
133133

134134
var pyService = utils.launchPythonProcess([`${__dirname}/py/service.py`]);
135-
var client = node.createClient(AddTwoInts, 'py_js_add_service');
135+
var client = node.createClient(AddTwoInts, 'js_py_add_two_ints');
136136
let request = new AddTwoInts.Request();
137137
request.a = 1;
138138
request.b = 2;
@@ -152,15 +152,44 @@ describe('Cross-language interaction', function() {
152152

153153
rclnodejs.spin(node);
154154
});
155+
156+
it('Node.js client should work with C++ service', function(done) {
157+
var node = rclnodejs.createNode('js_cpp_add_client');
158+
const AddTwoInts = rclnodejs.require('example_interfaces').srv.AddTwoInts;
159+
var client = node.createClient(AddTwoInts, 'js_cpp_add_two_ints');
160+
let request = new AddTwoInts.Request();
161+
request.a = 1;
162+
request.b = 2;
163+
164+
var destroy = false;
165+
var cppServicePath = path.join(process.env['AMENT_PREFIX_PATH'],
166+
'lib',
167+
'demo_nodes_cpp',
168+
'add_two_ints_server');
169+
var cppService = childProcess.spawn(cppServicePath, ['-s', 'js_cpp_add_two_ints']);
170+
var timer = node.createTimer(100, () => {
171+
client.sendRequest(request, (response) => {
172+
if (!destroy) {
173+
timer.cancel();
174+
assert.deepStrictEqual(response.sum, 3);
175+
node.destroy();
176+
cppService.kill('SIGINT');
177+
destroy = true;
178+
done();
179+
}
180+
});
181+
});
182+
rclnodejs.spin(node);
183+
});
155184
});
156-
185+
157186
describe('Node.js service', function() {
158187
it('Node.js service should work with Python client', function(done) {
159-
var node = rclnodejs.createNode('js_add_service');
188+
var node = rclnodejs.createNode('py_js_add_service');
160189
const AddTwoInts = rclnodejs.require('example_interfaces').srv.AddTwoInts;
161190
var destroy = false;
162191

163-
var service = node.createService(AddTwoInts, 'js_py_add_service', (request, response) => {
192+
var service = node.createService(AddTwoInts, 'py_js_add_two_ints', (request, response) => {
164193
assert.deepStrictEqual(typeof request.a, 'number');
165194
assert.deepStrictEqual(typeof request.b, 'number');
166195
response.sum = request.a + request.b;
@@ -178,5 +207,30 @@ describe('Cross-language interaction', function() {
178207
}
179208
});
180209
});
210+
211+
it('Node.js service should work with C++ client', function(done) {
212+
var node = rclnodejs.createNode('cpp_js_add_service');
213+
const AddTwoInts = rclnodejs.require('example_interfaces').srv.AddTwoInts;
214+
var destroy = false;
215+
216+
var service = node.createService(AddTwoInts, 'cpp_js_add_two_ints', (request, response) => {
217+
assert.deepStrictEqual(typeof request.a, 'number');
218+
assert.deepStrictEqual(typeof request.b, 'number');
219+
response.sum = request.a + request.b;
220+
return response;
221+
});
222+
rclnodejs.spin(node);
223+
224+
var cppClientPath = path.join(process.env['AMENT_PREFIX_PATH'],
225+
'lib',
226+
'demo_nodes_cpp',
227+
'add_two_ints_client');
228+
var cppClient = childProcess.spawn(cppClientPath, ['-s', 'cpp_js_add_two_ints']);
229+
cppClient.stdout.on('data', function(data) {
230+
assert.deepStrictEqual(data.toString().trim(), 'Result of add_two_ints: 5');
231+
node.destroy();
232+
done();
233+
});
234+
});
181235
});
182236
});

0 commit comments

Comments
 (0)