Skip to content

Commit 4cff06a

Browse files
authored
Support get_type_description service via parameter (#1162)
This PR adds support for starting the type description service via a configurable parameter and includes a new test case to verify that configuration. - Added a new test case in test/test-type-description-service.js to check the parameter configuration. - Updated lib/type_description_service.js to declare and retrieve the "start_type_description_service" parameter and conditionally start the service. Fix: #1161
1 parent b3cf4a7 commit 4cff06a

File tree

3 files changed

+97
-13
lines changed

3 files changed

+97
-13
lines changed

lib/type_description_service.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ const loader = require('./interface_loader.js');
1818
const rclnodejs = require('bindings')('rclnodejs');
1919
const Service = require('./service.js');
2020

21+
const {
22+
ParameterType,
23+
Parameter,
24+
ParameterDescriptor,
25+
} = require('../lib/parameter.js');
26+
2127
// This class is used to create a TypeDescriptionService which can be used to
2228
// retrieve information about types used by the node’s publishers, subscribers,
2329
// services or actions.
@@ -32,10 +38,30 @@ class TypeDescriptionService {
3238
'type_description_interfaces/srv/GetTypeDescription'
3339
);
3440
this._typeDescriptionService = null;
41+
42+
this._enabled = false;
43+
const startTypeDescriptionServiceParam = 'start_type_description_service';
44+
if (!node.hasParameter(startTypeDescriptionServiceParam)) {
45+
node.declareParameter(
46+
new Parameter(
47+
startTypeDescriptionServiceParam,
48+
ParameterType.PARAMETER_BOOL,
49+
true
50+
),
51+
new ParameterDescriptor(
52+
startTypeDescriptionServiceParam,
53+
ParameterType.PARAMETER_BOOL,
54+
'If enabled, start the ~/get_type_description service.',
55+
true
56+
)
57+
);
58+
}
59+
const param = node.getParameter(startTypeDescriptionServiceParam);
60+
this._enabled = param.value;
3561
}
3662

3763
start() {
38-
if (this._typeDescriptionService) {
64+
if (!this._enabled || this._typeDescriptionService) {
3965
return;
4066
}
4167

test/test-parameter-service.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const assert = require('assert');
1818

1919
const rclnodejs = require('../index.js');
2020
const assertUtils = require('./utils.js');
21-
const assertThrowsError = assertUtils.assertThrowsError;
21+
const DistroUtils = require('../lib/distro.js');
2222

2323
const {
2424
ParameterType,
@@ -120,7 +120,11 @@ describe('Parameter_server tests', function () {
120120
let success = false;
121121
client.sendRequest(request, (response) => {
122122
const result = response.result;
123-
assert.equal(result.names.length, 3); // account for use_sim_time parameter
123+
if (DistroUtils.getDistroId() >= DistroUtils.getDistroId('jazzy')) {
124+
assert.equal(result.names.length, 4); // account for use_sim_time and start_type_description_service parameter
125+
} else {
126+
assert.equal(result.names.length, 3); // account for use_sim_time parameter
127+
}
124128
assert.ok(result.names.includes('p1'));
125129
assert.ok(result.names.includes('p2'));
126130
success = true;

test/test-type-description-service.js

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const assertUtils = require('./utils.js');
1919
const DistroUtils = require('../lib/distro.js');
2020
const rclnodejs = require('../index.js');
2121
const TypeDescriptionService = require('../lib/type_description_service.js');
22+
const { exec } = require('child_process');
2223

2324
describe('type description service test suite', function () {
2425
this.timeout(60 * 1000);
@@ -35,14 +36,13 @@ describe('type description service test suite', function () {
3536
const nodeName = 'test_type_description_service';
3637
node = rclnodejs.createNode(nodeName);
3738
rclnodejs.spin(node);
38-
await assertUtils.createDelay(1000);
3939
});
4040

4141
afterEach(function () {
4242
rclnodejs.shutdown();
4343
});
4444

45-
it('Test type description service', function (done) {
45+
it('Test type description service', async function () {
4646
// Create a publisher
4747
const topic = 'test_get_type_description_publisher';
4848
const topicType = 'std_msgs/msg/String';
@@ -63,14 +63,68 @@ describe('type description service test suite', function () {
6363
const GetTypeDescription =
6464
'type_description_interfaces/srv/GetTypeDescription';
6565
const client = node.createClient(GetTypeDescription, serviceName);
66-
client.sendRequest(request, (response) => {
67-
assert.strictEqual(response.successful, true);
68-
assert.strictEqual(
69-
response.type_description.type_description.type_name,
70-
topicType
71-
);
72-
assert.notStrictEqual(response.type_sources.length, 0);
73-
done();
66+
return client.waitForService(60 * 1000).then((result) => {
67+
if (!result) {
68+
throw new Error('Service not available');
69+
}
70+
return new Promise((resolve) => {
71+
client.sendRequest(request, (response) => {
72+
assert.strictEqual(response.successful, true);
73+
assert.strictEqual(
74+
response.type_description.type_description.type_name,
75+
topicType
76+
);
77+
assert.notStrictEqual(response.type_sources.length, 0);
78+
resolve();
79+
});
80+
});
7481
});
7582
});
83+
84+
it('Test type description service configured by parameter', function (done) {
85+
exec(
86+
'ros2 param list /test_type_description_service',
87+
(error, stdout, stderr) => {
88+
if (error || stderr) {
89+
done(
90+
new Error(
91+
'Test type description service configured by parameter failed.'
92+
)
93+
);
94+
}
95+
if (stdout.includes('start_type_description_service')) {
96+
done();
97+
} else {
98+
done(
99+
new Error("'start_type_description_service' not found in stdout.")
100+
);
101+
}
102+
}
103+
);
104+
});
105+
106+
it('Test start_type_description_service parameter value', function (done) {
107+
exec(
108+
'ros2 param get /test_type_description_service start_type_description_service',
109+
(error, stdout, stderr) => {
110+
if (error || stderr) {
111+
done(
112+
new Error(
113+
'Test type description service configured by parameter failed.'
114+
)
115+
);
116+
}
117+
if (stdout.includes('Boolean value is: True')) {
118+
done();
119+
} else {
120+
console.log(stdout);
121+
done(
122+
new Error(
123+
"'start_type_description_service param value' not found in stdout."
124+
)
125+
);
126+
}
127+
}
128+
);
129+
});
76130
});

0 commit comments

Comments
 (0)