|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Open Source Robotics Foundation |
| 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 | + */ |
| 17 | + |
| 18 | +/* eslint-disable camelcase */ |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +const assert = require('assert'); |
| 22 | +const rclnodejs = require('../index.js'); |
| 23 | + |
| 24 | +/** |
| 25 | + * These tests need to be run with `--expose-gc` flags. |
| 26 | + */ |
| 27 | +describe('Test promise wrapper with garbage collection', function () { |
| 28 | + this.timeout(60 * 1000); |
| 29 | + |
| 30 | + before(function () { |
| 31 | + return rclnodejs.init(); |
| 32 | + }); |
| 33 | + |
| 34 | + after(function () { |
| 35 | + rclnodejs.shutdown(); |
| 36 | + }); |
| 37 | + |
| 38 | + [ |
| 39 | + { |
| 40 | + typeClass: 'std_msgs/msg/String', |
| 41 | + msg: { |
| 42 | + data: 'Hello World!', |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + typeClass: 'std_msgs/msg/UInt8MultiArray', |
| 47 | + msg: { |
| 48 | + layout: { dim: [{ label: 'test', size: 10, stride: 10 }], data_offset: 0 }, |
| 49 | + data: Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + typeClass: 'sensor_msgs/msg/JointState', |
| 54 | + msg: { |
| 55 | + header: { |
| 56 | + stamp: { |
| 57 | + sec: 123456, |
| 58 | + nanosec: 789, |
| 59 | + }, |
| 60 | + frame_id: 'main frame', |
| 61 | + }, |
| 62 | + name: ['Tom', 'Jerry'], |
| 63 | + position: Float64Array.from([1, 2]), |
| 64 | + velocity: Float64Array.from([2, 3]), |
| 65 | + effort: Float64Array.from([4, 5, 6]), |
| 66 | + }, |
| 67 | + }, |
| 68 | + ].forEach(({ typeClass, msg }, i) => |
| 69 | + it(`${typeClass} message does not get garbage collected`, async function () { |
| 70 | + const node = rclnodejs.createNode(`promise_gc_${i}`); |
| 71 | + rclnodejs.spin(node); |
| 72 | + |
| 73 | + const topic = `promise_gc_channel_${i}`; |
| 74 | + |
| 75 | + const publisher = node.createPublisher(typeClass, topic); |
| 76 | + const timer = node.createTimer(100, () => publisher.publish(msg)); |
| 77 | + |
| 78 | + const result = await new Promise((res) => |
| 79 | + node.createSubscription(typeClass, topic, (msg) => { |
| 80 | + timer.cancel(); |
| 81 | + node.destroy(); |
| 82 | + res(msg); |
| 83 | + }) |
| 84 | + ); |
| 85 | + global.gc(); |
| 86 | + assert.deepEqual(result, msg); |
| 87 | + }) |
| 88 | + ); |
| 89 | +}); |
0 commit comments