|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 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 | +#include <gz/msgs/int32.pb.h> |
| 18 | + |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +#include <gz/transport/Node.hh> |
| 22 | + |
| 23 | +TEST(CallbackScope, CleanupCorrectly) |
| 24 | +{ |
| 25 | + // test cleanup for serialized msgs |
| 26 | + gz::transport::Node node; |
| 27 | + auto publisher = node.Advertise<gz::msgs::Int32>("/my_topic"); |
| 28 | + gz::msgs::Int32 msg; |
| 29 | + { |
| 30 | + auto msg2 = std::make_unique<gz::msgs::Int32>(); |
| 31 | + |
| 32 | + std::function<void(const gz::msgs::Int32&)> callback = |
| 33 | + [&msg2](const gz::msgs::Int32 &) { |
| 34 | + |
| 35 | + if (nullptr == msg2) |
| 36 | + { |
| 37 | + FAIL(); |
| 38 | + } |
| 39 | + return; |
| 40 | + }; |
| 41 | + |
| 42 | + node.Subscribe("/my_topic", callback); |
| 43 | + publisher.Publish(msg); |
| 44 | + publisher.Publish(msg); |
| 45 | + node.Unsubscribe("/my_topic"); |
| 46 | + |
| 47 | + // Clear msg2 |
| 48 | + msg2.reset(); |
| 49 | + } |
| 50 | + |
| 51 | + // test cleanup for raw msgs |
| 52 | + gz::transport::Node nodeRaw; |
| 53 | + auto publisherRaw = nodeRaw.Advertise<gz::msgs::Int32>("/my_topic_raw"); |
| 54 | + gz::msgs::Int32 msgRaw; |
| 55 | + { |
| 56 | + auto msg2Raw = std::make_unique<gz::msgs::Int32>(); |
| 57 | + std::function<void(const char *_msg, const size_t, |
| 58 | + const gz::transport::MessageInfo &)> rawCallback = |
| 59 | + [&msg2Raw](const char *, const size_t, |
| 60 | + const gz::transport::MessageInfo &) { |
| 61 | + |
| 62 | + if (nullptr == msg2Raw) |
| 63 | + { |
| 64 | + FAIL(); |
| 65 | + } |
| 66 | + return; |
| 67 | + }; |
| 68 | + |
| 69 | + nodeRaw.SubscribeRaw("/my_topic_raw", rawCallback); |
| 70 | + // use Publish intead of PublishRaw so the msgs end up in the pub queue |
| 71 | + publisherRaw.Publish(msgRaw); |
| 72 | + publisherRaw.Publish(msgRaw); |
| 73 | + nodeRaw.Unsubscribe("/my_topic_raw"); |
| 74 | + |
| 75 | + msg2Raw.reset(); |
| 76 | + } |
| 77 | +} |
0 commit comments