|
| 1 | +// Copyright 2023 Sony Group Corporation. |
| 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 | +#include <gtest/gtest.h> |
| 15 | + |
| 16 | +#include "rclcpp/context.hpp" |
| 17 | +#include "rclcpp/rclcpp.hpp" |
| 18 | + |
| 19 | +TEST(TestContext, check_pre_shutdown_callback_order) { |
| 20 | + auto context = std::make_shared<rclcpp::Context>(); |
| 21 | + context->init(0, nullptr); |
| 22 | + |
| 23 | + int result[4] = {0, 0, 0, 0}; |
| 24 | + int index = 0; |
| 25 | + |
| 26 | + auto callback1 = [&result, &index]() { |
| 27 | + result[index] = 1; |
| 28 | + index++; |
| 29 | + }; |
| 30 | + auto callback2 = [&result, &index]() { |
| 31 | + result[index] = 2; |
| 32 | + index++; |
| 33 | + }; |
| 34 | + auto callback3 = [&result, &index]() { |
| 35 | + result[index] = 3; |
| 36 | + index++; |
| 37 | + }; |
| 38 | + auto callback4 = [&result, &index]() { |
| 39 | + result[index] = 4; |
| 40 | + index++; |
| 41 | + }; |
| 42 | + |
| 43 | + context->add_pre_shutdown_callback(callback1); |
| 44 | + context->add_pre_shutdown_callback(callback2); |
| 45 | + context->add_pre_shutdown_callback(callback3); |
| 46 | + context->add_pre_shutdown_callback(callback4); |
| 47 | + |
| 48 | + context->shutdown("for test"); |
| 49 | + |
| 50 | + EXPECT_TRUE(result[0] == 1 && result[1] == 2 && result[2] == 3 && result[3] == 4); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(TestContext, check_on_shutdown_callback_order) { |
| 54 | + auto context = std::make_shared<rclcpp::Context>(); |
| 55 | + context->init(0, nullptr); |
| 56 | + |
| 57 | + int result[4] = {0, 0, 0, 0}; |
| 58 | + int index = 0; |
| 59 | + |
| 60 | + auto callback1 = [&result, &index]() { |
| 61 | + result[index] = 1; |
| 62 | + index++; |
| 63 | + }; |
| 64 | + auto callback2 = [&result, &index]() { |
| 65 | + result[index] = 2; |
| 66 | + index++; |
| 67 | + }; |
| 68 | + auto callback3 = [&result, &index]() { |
| 69 | + result[index] = 3; |
| 70 | + index++; |
| 71 | + }; |
| 72 | + auto callback4 = [&result, &index]() { |
| 73 | + result[index] = 4; |
| 74 | + index++; |
| 75 | + }; |
| 76 | + |
| 77 | + context->add_on_shutdown_callback(callback1); |
| 78 | + context->add_on_shutdown_callback(callback2); |
| 79 | + context->add_on_shutdown_callback(callback3); |
| 80 | + context->add_on_shutdown_callback(callback4); |
| 81 | + |
| 82 | + context->shutdown("for test"); |
| 83 | + |
| 84 | + EXPECT_TRUE(result[0] == 1 && result[1] == 2 && result[2] == 3 && result[3] == 4); |
| 85 | +} |
| 86 | + |
| 87 | +TEST(TestContext, check_mixed_register_shutdown_callback_order) { |
| 88 | + auto context = std::make_shared<rclcpp::Context>(); |
| 89 | + context->init(0, nullptr); |
| 90 | + |
| 91 | + int result[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 92 | + int index = 0; |
| 93 | + |
| 94 | + auto callback1 = [&result, &index]() { |
| 95 | + result[index] = 1; |
| 96 | + index++; |
| 97 | + }; |
| 98 | + auto callback2 = [&result, &index]() { |
| 99 | + result[index] = 2; |
| 100 | + index++; |
| 101 | + }; |
| 102 | + auto callback3 = [&result, &index]() { |
| 103 | + result[index] = 3; |
| 104 | + index++; |
| 105 | + }; |
| 106 | + auto callback4 = [&result, &index]() { |
| 107 | + result[index] = 4; |
| 108 | + index++; |
| 109 | + }; |
| 110 | + auto callback5 = [&result, &index]() { |
| 111 | + result[index] = 5; |
| 112 | + index++; |
| 113 | + }; |
| 114 | + auto callback6 = [&result, &index]() { |
| 115 | + result[index] = 6; |
| 116 | + index++; |
| 117 | + }; |
| 118 | + auto callback7 = [&result, &index]() { |
| 119 | + result[index] = 7; |
| 120 | + index++; |
| 121 | + }; |
| 122 | + auto callback8 = [&result, &index]() { |
| 123 | + result[index] = 8; |
| 124 | + index++; |
| 125 | + }; |
| 126 | + |
| 127 | + // Mixed register |
| 128 | + context->add_pre_shutdown_callback(callback1); |
| 129 | + context->add_on_shutdown_callback(callback5); |
| 130 | + context->add_pre_shutdown_callback(callback2); |
| 131 | + context->add_on_shutdown_callback(callback6); |
| 132 | + context->add_pre_shutdown_callback(callback3); |
| 133 | + context->add_on_shutdown_callback(callback7); |
| 134 | + context->add_pre_shutdown_callback(callback4); |
| 135 | + context->add_on_shutdown_callback(callback8); |
| 136 | + |
| 137 | + context->shutdown("for test"); |
| 138 | + |
| 139 | + EXPECT_TRUE( |
| 140 | + result[0] == 1 && result[1] == 2 && result[2] == 3 && result[3] == 4 && |
| 141 | + result[4] == 5 && result[5] == 6 && result[6] == 7 && result[7] == 8); |
| 142 | +} |
| 143 | + |
| 144 | +TEST(TestContext, check_pre_shutdown_callback_order_after_del) { |
| 145 | + auto context = std::make_shared<rclcpp::Context>(); |
| 146 | + context->init(0, nullptr); |
| 147 | + |
| 148 | + int result[4] = {0, 0, 0, 0}; |
| 149 | + int index = 0; |
| 150 | + |
| 151 | + auto callback1 = [&result, &index]() { |
| 152 | + result[index] = 1; |
| 153 | + index++; |
| 154 | + }; |
| 155 | + auto callback2 = [&result, &index]() { |
| 156 | + result[index] = 2; |
| 157 | + index++; |
| 158 | + }; |
| 159 | + auto callback3 = [&result, &index]() { |
| 160 | + result[index] = 3; |
| 161 | + index++; |
| 162 | + }; |
| 163 | + auto callback4 = [&result, &index]() { |
| 164 | + result[index] = 4; |
| 165 | + index++; |
| 166 | + }; |
| 167 | + |
| 168 | + context->add_pre_shutdown_callback(callback1); |
| 169 | + auto callback_handle = context->add_pre_shutdown_callback(callback2); |
| 170 | + context->add_pre_shutdown_callback(callback3); |
| 171 | + context->add_pre_shutdown_callback(callback4); |
| 172 | + |
| 173 | + EXPECT_TRUE(context->remove_pre_shutdown_callback(callback_handle)); |
| 174 | + EXPECT_FALSE(context->remove_pre_shutdown_callback(callback_handle)); |
| 175 | + |
| 176 | + context->shutdown("for test"); |
| 177 | + |
| 178 | + EXPECT_TRUE(result[0] == 1 && result[1] == 3 && result[2] == 4 && result[3] == 0); |
| 179 | +} |
| 180 | + |
| 181 | +TEST(TestContext, check_on_shutdown_callback_order_after_del) { |
| 182 | + auto context = std::make_shared<rclcpp::Context>(); |
| 183 | + context->init(0, nullptr); |
| 184 | + |
| 185 | + int result[4] = {0, 0, 0, 0}; |
| 186 | + int index = 0; |
| 187 | + |
| 188 | + auto callback1 = [&result, &index]() { |
| 189 | + result[index] = 1; |
| 190 | + index++; |
| 191 | + }; |
| 192 | + auto callback2 = [&result, &index]() { |
| 193 | + result[index] = 2; |
| 194 | + index++; |
| 195 | + }; |
| 196 | + auto callback3 = [&result, &index]() { |
| 197 | + result[index] = 3; |
| 198 | + index++; |
| 199 | + }; |
| 200 | + auto callback4 = [&result, &index]() { |
| 201 | + result[index] = 4; |
| 202 | + index++; |
| 203 | + }; |
| 204 | + |
| 205 | + context->add_on_shutdown_callback(callback1); |
| 206 | + auto callback_handle = context->add_on_shutdown_callback(callback2); |
| 207 | + context->add_on_shutdown_callback(callback3); |
| 208 | + context->add_on_shutdown_callback(callback4); |
| 209 | + |
| 210 | + EXPECT_TRUE(context->remove_on_shutdown_callback(callback_handle)); |
| 211 | + EXPECT_FALSE(context->remove_on_shutdown_callback(callback_handle)); |
| 212 | + |
| 213 | + context->shutdown("for test"); |
| 214 | + |
| 215 | + EXPECT_TRUE(result[0] == 1 && result[1] == 3 && result[2] == 4 && result[3] == 0); |
| 216 | +} |
0 commit comments