Skip to content

Commit b00c5c1

Browse files
author
Minggang Wang
authored
Merge pull request #142 from qiuzhong/catch-m3
Add test cases for M3 legacy features
2 parents cc60673 + 7d8e3d5 commit b00c5c1

10 files changed

+245
-22
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/publisher_msg.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ def main():
9797
publisher = node.create_publisher(Float64, 'Float64_py_js_channel')
9898
msg = Float64()
9999
msg.data = 3.14
100+
elif rclType == 'Array':
101+
node = rclpy.create_node('py_array_publisher')
102+
publisher = node.create_publisher(ByteMultiArray, 'Array_py_js_channel');
103+
104+
lengthDim = MultiArrayDimension()
105+
lengthDim.label = 'length'
106+
lengthDim.size = 1;
107+
lengthDim.stride = 3;
108+
109+
layout = MultiArrayLayout()
110+
layout.dim = [lengthDim]
111+
layout.data_offset = 0;
112+
113+
msg = ByteMultiArray()
114+
msg.layout = layout
115+
msg.data = [b'\x41', b'\x42', b'\x43']
100116
elif rclType == 'ColorRGBA':
101117
node = rclpy.create_node('py_colorrgba_publisher')
102118
publisher = node.create_publisher(ColorRGBA, 'ColorRGBA_py_js_channel')

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/py/subscription_msg.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def callback(msg):
3636
sys.stdout.write(str(msg.data))
3737
sys.stdout.flush()
3838

39+
def callback_array(msg):
40+
sys.stdout.write(''.join([r.decode('utf-8') for r in msg.data]))
41+
sys.stdout.flush()
42+
3943
def callback_colorrgba(msg):
4044
sys.stdout.write('(' + str(msg.r) + ','
4145
+ str(msg.g) + ','
@@ -109,6 +113,9 @@ def main():
109113
elif rclType == 'Float64':
110114
node = rclpy.create_node('py_float64_subscription')
111115
subscription = node.create_subscription(Float64, 'Float64_js_py_channel', callback)
116+
elif rclType == 'Array':
117+
node = rclpy.create_node('py_array_subscription')
118+
subscription = node.create_subscription(ByteMultiArray, 'Array_js_py_channel', callback_array)
112119
elif rclType == 'ColorRGBA':
113120
node = rclpy.create_node('py_colorrgba_subscription')
114121
subscription = node.create_subscription(ColorRGBA, 'ColorRGBA_js_py_channel', callback_colorrgba)

test/test-compound-msg-type-check.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,6 @@ describe('Compound types', function() {
3838
assert.deepStrictEqual(typeof msg.g, 'number');
3939
assert.deepStrictEqual(typeof msg.b, 'number');
4040
assert.deepStrictEqual(typeof msg.a, 'number');
41-
42-
// assertThrowsError(() => {
43-
// msg.r = -1;
44-
// msg.r = 256;
45-
// }, TypeError, 'out of bounds', 'Invalid RGBA value');
46-
// assertThrowsError(() => {
47-
// msg.g = -1;
48-
// msg.g = 256;
49-
// }, TypeError, 'out of bounds', 'Invalid RGBA value');
50-
// assertThrowsError(() => {
51-
// msg.b = -1;
52-
// msg.b = 256;
53-
// }, TypeError, 'out of bounds', 'Invalid RGBA value');
54-
// assertThrowsError(() => {
55-
// msg.a = -0.1;
56-
// msg.a = 1.1;
57-
// }, TypeError, 'out of bounds', 'Invalid RGBA value');
5841
});
5942

6043
it('Array', function() {

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
});

test/test-msg-type-py-node.js

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,23 @@ describe('Rclnodejs - Python message type testing', function() {
294294
rclnodejs.spin(node);
295295
});
296296

297-
// it('Array', function(done) {
298-
// });
297+
it('Array', function(done) {
298+
var node = rclnodejs.createNode('array_js_subscription');
299+
const ByteMultiArray = rclnodejs.require('std_msgs').msg.ByteMultiArray;
300+
var destroy = false;
301+
var publisher = utils.launchPythonProcess([`${__dirname}/py/publisher_msg.py`, 'Array']);
302+
var subscription = node.createSubscription(ByteMultiArray, 'Array_py_js_channel', (msg) => {
303+
assert.deepStrictEqual(msg.data, [65, 66, 67]);
304+
305+
if (!destroy) {
306+
node.destroy();
307+
publisher.kill('SIGINT');
308+
destroy = true;
309+
}
310+
done();
311+
});
312+
rclnodejs.spin(node);
313+
});
299314

300315
it('Header', function(done) {
301316
var node = rclnodejs.createNode('header_js_publisher');
@@ -733,6 +748,47 @@ describe('Rclnodejs - Python message type testing', function() {
733748
describe('Rclnodejs publisher - Python subscription: compound msg types', function() {
734749
this.timeout(60 * 1000);
735750

751+
it('Array', function(done) {
752+
var node = rclnodejs.createNode('multiarray_js_publisher');
753+
const Byte = rclnodejs.require('std_msgs').msg.Byte;
754+
const MultiArrayDimension = rclnodejs.require('std_msgs').msg.MultiArrayDimension;
755+
const MultiArrayLayout = rclnodejs.require('std_msgs').msg.MultiArrayLayout;
756+
const ByteMultiArray = rclnodejs.require('std_msgs').msg.ByteMultiArray;
757+
758+
let lengthDim = new MultiArrayDimension();
759+
lengthDim.label = 'length';
760+
lengthDim.size = 1;
761+
lengthDim.stride = 3;
762+
763+
let layout = new MultiArrayLayout();
764+
layout.dim.fill([lengthDim]);
765+
// eslint-disable-next-line
766+
layout.data_offset = 0;
767+
768+
let byteArray = new ByteMultiArray();
769+
byteArray.layout = layout;
770+
byteArray.data = [65, 66, 67];
771+
772+
var destroy = false;
773+
var subscription = utils.launchPythonProcess([`${__dirname}/py/subscription_msg.py`, 'Array']);
774+
var publisher = node.createPublisher(ByteMultiArray, 'Array_js_py_channel');
775+
const expected = 'ABC';
776+
subscription.stdout.on('data', (data) => {
777+
if (!destroy) {
778+
clearInterval(timer);
779+
assert.deepStrictEqual(data.toString(), expected);
780+
done();
781+
node.destroy();
782+
subscription.kill('SIGINT');
783+
destroy = true;
784+
}
785+
});
786+
var timer = setInterval(() => {
787+
publisher.publish(byteArray);
788+
}, 100);
789+
rclnodejs.spin(node);
790+
});
791+
736792
it('ColorRGBA', function(done) {
737793
var node = rclnodejs.createNode('colorrgba_js_publisher');
738794
const ColorRGBA = rclnodejs.require('std_msgs').msg.ColorRGBA;

0 commit comments

Comments
 (0)