Skip to content

Commit 7d8e3d5

Browse files
committed
[test]Add test for Python - Node.js client/service interaction
1 parent 3dcad9d commit 7d8e3d5

File tree

6 files changed

+164
-3
lines changed

6 files changed

+164
-3
lines changed

test/publisher_msg_colorrgba.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_msg_header.js

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

3636
rclnodejs.spin(node);
37-
process.on('message', (m) => {
37+
process.on('SIGINT', (m) => {
3838
clearInterval(timer);
3939
node.destroy();
4040
rclnodejs.shutdown();

test/publisher_msg_jointstate.js

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

4646
rclnodejs.spin(node);
47-
process.on('message', (m) => {
47+
process.on('SIGINT', (m) => {
4848
clearInterval(timer);
4949
node.destroy();
5050
rclnodejs.shutdown();

test/py/client.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/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.
15+
16+
import sys
17+
import rclpy
18+
from time import sleep
19+
from std_msgs.msg import String
20+
from example_interfaces.srv import AddTwoInts
21+
import signal
22+
23+
node = None
24+
25+
def cleanup():
26+
global node
27+
node.destroy_node()
28+
rclpy.shutdown()
29+
30+
def handler(signum, frame):
31+
cleanup()
32+
sys.exit(0)
33+
34+
def callback(response):
35+
print(response.sum)
36+
37+
def main():
38+
global node
39+
signal.signal(signal.SIGINT, handler)
40+
41+
rclpy.init()
42+
node = rclpy.create_node('add_client')
43+
client = node.create_client(AddTwoInts, 'js_py_add_service')
44+
request = AddTwoInts.Request()
45+
request.a = 1
46+
request.b = 2
47+
48+
client.call(request)
49+
client.wait_for_future()
50+
print(client.response.sum)
51+
52+
cleanup()
53+
54+
if __name__ == '__main__':
55+
main()

test/py/service.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/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.
15+
16+
import sys
17+
import rclpy
18+
from time import sleep
19+
from std_msgs.msg import String
20+
from example_interfaces.srv import AddTwoInts
21+
import signal
22+
23+
node = None
24+
25+
def cleanup():
26+
global node
27+
node.destroy_node()
28+
rclpy.shutdown()
29+
30+
def handler(signum, frame):
31+
cleanup()
32+
sys.exit(0)
33+
34+
def callback(request, response):
35+
response.sum = request.a + request.b
36+
return response
37+
38+
def main():
39+
global node
40+
signal.signal(signal.SIGINT, handler)
41+
42+
rclpy.init()
43+
node = rclpy.create_node('add_service')
44+
service = node.create_service(AddTwoInts, 'py_js_add_service', callback)
45+
while rclpy.ok():
46+
rclpy.spin_once(node)
47+
48+
cleanup()
49+
50+
if __name__ == '__main__':
51+
main()

test/test-cross-lang.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,59 @@ describe('Cross-language interaction', function() {
124124
rclnodejs.spin(node);
125125
});
126126
});
127+
128+
describe('Node.js client', function() {
129+
it('Node.js client should work with Python service', function(done) {
130+
var node = rclnodejs.createNode('js_add_client');
131+
const AddTwoInts = rclnodejs.require('example_interfaces').srv.AddTwoInts;
132+
var destroy = false;
133+
134+
var pyService = utils.launchPythonProcess([`${__dirname}/py/service.py`]);
135+
var client = node.createClient(AddTwoInts, 'py_js_add_service');
136+
let request = new AddTwoInts.Request();
137+
request.a = 1;
138+
request.b = 2;
139+
140+
var timer = setInterval(() => {
141+
client.sendRequest(request, (response) => {
142+
if (!destroy) {
143+
assert.deepStrictEqual(response.sum, 3);
144+
clearInterval(timer);
145+
node.destroy();
146+
pyService.kill('SIGINT');
147+
destroy = true;
148+
done();
149+
}
150+
});
151+
}, 100);
152+
153+
rclnodejs.spin(node);
154+
});
155+
});
156+
157+
describe('Node.js service', function() {
158+
it('Node.js service should work with Python client', function(done) {
159+
var node = rclnodejs.createNode('js_add_service');
160+
const AddTwoInts = rclnodejs.require('example_interfaces').srv.AddTwoInts;
161+
var destroy = false;
162+
163+
var service = node.createService(AddTwoInts, 'js_py_add_service', (request, response) => {
164+
assert.deepStrictEqual(typeof request.a, 'number');
165+
assert.deepStrictEqual(typeof request.b, 'number');
166+
response.sum = request.a + request.b;
167+
return response;
168+
});
169+
rclnodejs.spin(node);
170+
171+
var pyClient = utils.launchPythonProcess([`${__dirname}/py/client.py`]);
172+
pyClient.stdout.on('data', function(data) {
173+
assert.deepEqual(parseInt(data, 10), 3);
174+
if (!destroy) {
175+
node.destroy();
176+
destroy = true;
177+
done();
178+
}
179+
});
180+
});
181+
});
127182
});

0 commit comments

Comments
 (0)