|
| 1 | +// Copyright (c) 2017 Intel Corporation. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/* eslint-disable camelcase */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +const assert = require('assert'); |
| 20 | +const rclnodejs = require('../index.js'); |
| 21 | + |
| 22 | +describe('Testing message files generated from an action file', function() { |
| 23 | + // let testRootDir = __dirname; |
| 24 | + // let testActionDir = path.join(testRootDir, 'ros1_actions'); |
| 25 | + // let pkgRootDir = path.dirname(testRootDir); |
| 26 | + // let msgGenRootDir = path.join(pkgRootDir, 'generated'); |
| 27 | + |
| 28 | + this.timeout(60 * 1000); |
| 29 | + |
| 30 | + beforeEach(function() { |
| 31 | + return rclnodejs.init(); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(function() { |
| 35 | + rclnodejs.shutdown(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('ActionServer start', function() { |
| 39 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 40 | + type: 'ros1_actions/msg/DoDishes', |
| 41 | + actionServer: 'dishes1', |
| 42 | + rclnodejs: rclnodejs |
| 43 | + }); |
| 44 | + |
| 45 | + as.start(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('GoalStatus requiring', function() { |
| 49 | + rclnodejs.require('actionlib_msgs/msg/GoalStatus'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('ActionClient sendGoal', function() { |
| 53 | + let ac = new rclnodejs.ActionClientInterface({ |
| 54 | + type: 'ros1_actions/msg/DoDishes', |
| 55 | + actionServer: 'dishes3', |
| 56 | + rclnodejs: rclnodejs |
| 57 | + }); |
| 58 | + |
| 59 | + ac.sendGoal({ goal: {dishwasher_id: 3}}); |
| 60 | + }); |
| 61 | + |
| 62 | + it('ActionLib server goal event triggered', function(done) { |
| 63 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 64 | + type: 'ros1_actions/msg/DoDishes', |
| 65 | + actionServer: 'dishes4', |
| 66 | + rclnodejs: rclnodejs |
| 67 | + }); |
| 68 | + as.on('goal', function(goal) { |
| 69 | + done(); |
| 70 | + }); |
| 71 | + as.start(); |
| 72 | + |
| 73 | + let ac = new rclnodejs.ActionClientInterface({ |
| 74 | + type: 'ros1_actions/msg/DoDishes', |
| 75 | + actionServer: 'dishes4', |
| 76 | + rclnodejs: rclnodejs |
| 77 | + }); |
| 78 | + ac.sendGoal({ goal: {dishwasher_id: 4}}); |
| 79 | + }); |
| 80 | + |
| 81 | + it('ActionLib client feedback accepted', function(done) { |
| 82 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 83 | + type: 'ros1_actions/msg/DoDishes', |
| 84 | + actionServer: 'dishes5', |
| 85 | + rclnodejs: rclnodejs |
| 86 | + }); |
| 87 | + as.on('goal', function(goal) { |
| 88 | + goal.setAccepted('goal accepted'); |
| 89 | + let feedback = { |
| 90 | + percent_complete: 70 |
| 91 | + }; |
| 92 | + |
| 93 | + goal.publishFeedback(feedback); |
| 94 | + }); |
| 95 | + as.start(); |
| 96 | + |
| 97 | + let ac = new rclnodejs.ActionClientInterface({ |
| 98 | + type: 'ros1_actions/msg/DoDishes', |
| 99 | + actionServer: 'dishes5', |
| 100 | + rclnodejs: rclnodejs |
| 101 | + }); |
| 102 | + ac.on('feedback', function(feedback) { |
| 103 | + assert.strictEqual(feedback.percent_complete, 70); |
| 104 | + done(); |
| 105 | + }); |
| 106 | + ac.sendGoal({ goal: {dishwasher_id: 5}}); |
| 107 | + }); |
| 108 | + |
| 109 | + it('ActionLib server goal cancelled', function(done) { |
| 110 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 111 | + type: 'ros1_actions/msg/DoDishes', |
| 112 | + actionServer: 'dishes6', |
| 113 | + rclnodejs: rclnodejs |
| 114 | + }); |
| 115 | + as.on('goal', function(goal) { |
| 116 | + goal.setCancelRequested(); |
| 117 | + }); |
| 118 | + as.start(); |
| 119 | + |
| 120 | + let ac = new rclnodejs.ActionClientInterface({ |
| 121 | + type: 'ros1_actions/msg/DoDishes', |
| 122 | + actionServer: 'dishes6', |
| 123 | + rclnodejs: rclnodejs |
| 124 | + }); |
| 125 | + ac.on('feedback', function(feedback) { |
| 126 | + assert.throws(() => { |
| 127 | + throw new Error('should not be here'); |
| 128 | + }, Error); |
| 129 | + }); |
| 130 | + ac.on('status', function(status) { |
| 131 | + done(); |
| 132 | + }); |
| 133 | + ac.sendGoal({ goal: {dishwasher_id: 6}}); |
| 134 | + }); |
| 135 | + |
| 136 | + it('ActionLib server goal setCancelled', function(done) { |
| 137 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 138 | + type: 'ros1_actions/msg/DoDishes', |
| 139 | + actionServer: 'dishes7', |
| 140 | + rclnodejs: rclnodejs |
| 141 | + }); |
| 142 | + as.on('goal', function(goal) { |
| 143 | + goal.setAccepted('goal accepted'); |
| 144 | + |
| 145 | + ac.cancel(goal.id); |
| 146 | + |
| 147 | + as.on('cancel', function(goalHandle) { |
| 148 | + goal.setCancelled({total_dishes_cleaned: 10}, 'canceled'); |
| 149 | + done(); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + as.start(); |
| 154 | + |
| 155 | + let ac = new rclnodejs.ActionClientInterface({ |
| 156 | + type: 'ros1_actions/msg/DoDishes', |
| 157 | + actionServer: 'dishes7', |
| 158 | + rclnodejs: rclnodejs |
| 159 | + }); |
| 160 | + ac.sendGoal({ goal: {dishwasher_id: 7}}); |
| 161 | + }); |
| 162 | + |
| 163 | + it('ActionLib server goal setRejected', function(done) { |
| 164 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 165 | + type: 'ros1_actions/msg/DoDishes', |
| 166 | + actionServer: 'dishes8', |
| 167 | + rclnodejs: rclnodejs |
| 168 | + }); |
| 169 | + as.on('goal', function(goal) { |
| 170 | + goal.setRejected({total_dishes_cleaned: 0}, 'rejected'); |
| 171 | + }); |
| 172 | + as.start(); |
| 173 | + |
| 174 | + let ac = new rclnodejs.ActionClientInterface({ |
| 175 | + type: 'ros1_actions/msg/DoDishes', |
| 176 | + actionServer: 'dishes8', |
| 177 | + rclnodejs: rclnodejs |
| 178 | + }); |
| 179 | + |
| 180 | + let count = 0; |
| 181 | + ac.on('status', function(status) { |
| 182 | + if (count++ == 1) { |
| 183 | + assert.strictEqual(status.status_list[0].text, 'rejected'); |
| 184 | + done(); |
| 185 | + } |
| 186 | + }); |
| 187 | + ac.sendGoal({ goal: {dishwasher_id: 8}}); |
| 188 | + }); |
| 189 | + |
| 190 | + it('ActionLib server goal setAborted', function(done) { |
| 191 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 192 | + type: 'ros1_actions/msg/DoDishes', |
| 193 | + actionServer: 'dishes9', |
| 194 | + rclnodejs: rclnodejs |
| 195 | + }); |
| 196 | + as.on('goal', function(goal) { |
| 197 | + goal.setAccepted('goal accepted'); |
| 198 | + goal.setAborted({total_dishes_cleaned: 0}, 'aborted'); |
| 199 | + }); |
| 200 | + |
| 201 | + as.start(); |
| 202 | + |
| 203 | + let ac = new rclnodejs.ActionClientInterface({ |
| 204 | + type: 'ros1_actions/msg/DoDishes', |
| 205 | + actionServer: 'dishes9', |
| 206 | + rclnodejs: rclnodejs |
| 207 | + }); |
| 208 | + ac.on('result', function(result) { |
| 209 | + assert.strictEqual(result.total_dishes_cleaned, 0); |
| 210 | + |
| 211 | + done(); |
| 212 | + }); |
| 213 | + ac.sendGoal({ goal: {dishwasher_id: 9}}); |
| 214 | + }); |
| 215 | + |
| 216 | + it('ActionLib sever goal setSucceeded', function(done) { |
| 217 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 218 | + type: 'ros1_actions/msg/DoDishes', |
| 219 | + actionServer: 'dishes10', |
| 220 | + rclnodejs: rclnodejs |
| 221 | + }); |
| 222 | + as.on('goal', function(goal) { |
| 223 | + goal.setAccepted('goal accepted'); |
| 224 | + goal.setSucceeded({total_dishes_cleaned: 100}, 'done'); |
| 225 | + }); |
| 226 | + as.start(); |
| 227 | + |
| 228 | + let ac = new rclnodejs.ActionClientInterface({ |
| 229 | + type: 'ros1_actions/msg/DoDishes', |
| 230 | + actionServer: 'dishes10', |
| 231 | + rclnodejs: rclnodejs |
| 232 | + }); |
| 233 | + ac.on('result', function(result) { |
| 234 | + assert.strictEqual(result.total_dishes_cleaned, 100); |
| 235 | + done(); |
| 236 | + }); |
| 237 | + |
| 238 | + ac.sendGoal({ goal: {dishwasher_id: 9}}); |
| 239 | + }); |
| 240 | + |
| 241 | + it('ActionLib complete process', function(done) { |
| 242 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 243 | + type: 'ros1_actions/msg/DoDishes', |
| 244 | + actionServer: 'dishes11', |
| 245 | + rclnodejs: rclnodejs |
| 246 | + }); |
| 247 | + as.on('goal', function(goal) { |
| 248 | + goal.setAccepted('goal accepted'); |
| 249 | + let feedback = { |
| 250 | + percent_complete: 90 |
| 251 | + }; |
| 252 | + |
| 253 | + goal.publishFeedback(feedback); |
| 254 | + goal.setSucceeded({total_dishes_cleaned: 100}, 'done'); |
| 255 | + }); |
| 256 | + as.start(); |
| 257 | + |
| 258 | + let ac = new rclnodejs.ActionClientInterface({ |
| 259 | + type: 'ros1_actions/msg/DoDishes', |
| 260 | + actionServer: 'dishes11', |
| 261 | + rclnodejs: rclnodejs |
| 262 | + }); |
| 263 | + ac.on('feedback', function(feedback) { |
| 264 | + assert.strictEqual(feedback.percent_complete, 90); |
| 265 | + }); |
| 266 | + ac.on('result', function(result) { |
| 267 | + assert.strictEqual(result.total_dishes_cleaned, 100); |
| 268 | + done(); |
| 269 | + }); |
| 270 | + |
| 271 | + ac.sendGoal({ goal: {dishwasher_id: 11}}); |
| 272 | + }); |
| 273 | + |
| 274 | + it('ActionClient cancel', function(done) { |
| 275 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 276 | + type: 'ros1_actions/msg/DoDishes', |
| 277 | + actionServer: 'dishes12', |
| 278 | + rclnodejs: rclnodejs |
| 279 | + }); |
| 280 | + as.on('goal', function(goal) { |
| 281 | + goal.setAccepted('goal accepted'); |
| 282 | + ac.cancel(gGoal.goal_id.id); |
| 283 | + }); |
| 284 | + as.on('cancel', function(goalHandle) { |
| 285 | + done(); |
| 286 | + }); |
| 287 | + as.start(); |
| 288 | + |
| 289 | + let ac = new rclnodejs.ActionClientInterface({ |
| 290 | + type: 'ros1_actions/msg/DoDishes', |
| 291 | + actionServer: 'dishes12', |
| 292 | + rclnodejs: rclnodejs |
| 293 | + }); |
| 294 | + let gGoal = ac.sendGoal({ goal: {dishwasher_id: 12}}); |
| 295 | + }); |
| 296 | + |
| 297 | + it('ActionClient shutdown', function() { |
| 298 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 299 | + type: 'ros1_actions/msg/DoDishes', |
| 300 | + actionServer: 'dishes13', |
| 301 | + rclnodejs: rclnodejs |
| 302 | + }); |
| 303 | + as.on('goal', function(goal) { |
| 304 | + goal.setAccepted('goal accepted'); |
| 305 | + }); |
| 306 | + as.start(); |
| 307 | + |
| 308 | + let ac = new rclnodejs.ActionClientInterface({ |
| 309 | + type: 'ros1_actions/msg/DoDishes', |
| 310 | + actionServer: 'dishes13', |
| 311 | + rclnodejs: rclnodejs |
| 312 | + }); |
| 313 | + ac.sendGoal({ goal: {dishwasher_id: 12}}); |
| 314 | + ac.shutdown(); |
| 315 | + }); |
| 316 | + |
| 317 | + it('ActionClient status succeed', function(done) { |
| 318 | + const GoalStatus = rclnodejs.require('actionlib_msgs/msg/GoalStatus'); |
| 319 | + let as = new rclnodejs.ActionLib.ActionServer({ |
| 320 | + type: 'ros1_actions/msg/DoDishes', |
| 321 | + actionServer: 'dishes14', |
| 322 | + rclnodejs: rclnodejs |
| 323 | + }); |
| 324 | + as.on('goal', function(goal) { |
| 325 | + goal.setAccepted('goal accepted'); |
| 326 | + goal.setSucceeded({total_dishes_cleaned: 100}, 'done'); |
| 327 | + }); |
| 328 | + as.start(); |
| 329 | + |
| 330 | + let count = 0; |
| 331 | + let ac = new rclnodejs.ActionClientInterface({ |
| 332 | + type: 'ros1_actions/msg/DoDishes', |
| 333 | + actionServer: 'dishes14', |
| 334 | + rclnodejs: rclnodejs |
| 335 | + }); |
| 336 | + ac.on('status', function(status) { |
| 337 | + status.status_list.forEach((s) =>{ |
| 338 | + if (count++ == 1 && s.goal_id.id === goal.goal_id.id && |
| 339 | + s.status === GoalStatus.SUCCEEDED) { |
| 340 | + done(); |
| 341 | + } |
| 342 | + }); |
| 343 | + }); |
| 344 | + |
| 345 | + let goal = ac.sendGoal({ goal: {dishwasher_id: 12}}); |
| 346 | + }); |
| 347 | +}); |
0 commit comments