- RecoveryBehavior::cancel() now calls a new virtual onCancelRequested() synchronously, from whichever thread calls cancel(). Documents onCancel() as effectively dead in move_base2: it only ran on the next update() tick, and CANCELLING forces tick_recovery=false, so that tick never came. Every existing behavior's onCancel() was unreachable code. Velocity-only behaviors (wait/rotate/back_up/clear_costmap) still stop correctly via the arbiter zeroing cmd_vel; this only matters for a behavior that owns a long-running operation of its own (see Test/sbpl_recovery, separate commit). - recovery_abi.h (new) + RECOVERY_CORE_EXPORT_BEHAVIOR(factory, symbol) macro: exports an extra <symbol>_abi version symbol alongside the existing BOOST_DLL_ALIAS factory. recovery_registry.cpp probes it before calling the factory and refuses to load a .so whose version doesn't match (or is missing) instead of dlsym+reinterpret_cast blindly onto a vtable that just grew a virtual — the ABI hazard boost::dll plugins don't otherwise catch at load time. - All 4 in-tree behaviors switched to the export macro. - test/plugins/stale_abi_recovery.cpp (new): a plugin built against the old ABI, used to verify the registry actually refuses it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
245 lines
7.6 KiB
C++
245 lines
7.6 KiB
C++
/*********************************************************************
|
|
*
|
|
* Kiểm đường nạp plugin thật: YAML -> library_path -> Boost.DLL -> configure.
|
|
*
|
|
* Bản test cũ của gói KHÔNG kiểm được gì: nó `return` khi không nạp được plugin nào và `main` trả 0,
|
|
* nên "không plugin nào chạy" cũng in [PASS]. Test này phải fail được.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cstdlib>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include <robot/node_handle.h>
|
|
|
|
#include <recovery_core/recovery_registry.h>
|
|
|
|
#include "recovery_test_utils.h"
|
|
|
|
namespace
|
|
{
|
|
using recovery_core::RecoveryOutputType;
|
|
using recovery_test::MockBehavior;
|
|
using recovery_test::VelocityRig;
|
|
|
|
TEST(Registry, LoadsDeclaredBehaviorsInOrder)
|
|
{
|
|
VelocityRig rig;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
ASSERT_TRUE(registry.loadFromConfig(nh, "recovery", rig.ctx));
|
|
|
|
// Thứ tự CHÍNH LÀ hành vi: caller thử behavior 0 trước.
|
|
ASSERT_EQ(registry.size(), 3u);
|
|
EXPECT_EQ(registry.nameAt(0), "wait");
|
|
EXPECT_EQ(registry.nameAt(1), "rotate");
|
|
EXPECT_EQ(registry.nameAt(2), "back_up");
|
|
}
|
|
|
|
TEST(Registry, LoadedBehaviorsReportCorrectOutputKind)
|
|
{
|
|
VelocityRig rig;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
ASSERT_TRUE(registry.loadFromConfig(nh, "recovery", rig.ctx));
|
|
ASSERT_EQ(registry.size(), 3u);
|
|
|
|
EXPECT_EQ(registry.at(0)->outputKind(), RecoveryOutputType::kNone); // wait
|
|
EXPECT_EQ(registry.at(1)->outputKind(), RecoveryOutputType::kVelocity); // rotate
|
|
EXPECT_EQ(registry.at(2)->outputKind(), RecoveryOutputType::kVelocity); // back_up
|
|
}
|
|
|
|
TEST(Registry, PerInstanceParamsComeFromItsOwnNamespace)
|
|
{
|
|
VelocityRig rig;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
ASSERT_TRUE(registry.loadFromConfig(nh, "recovery", rig.ctx));
|
|
|
|
// `recovery/rotate/timeout: 20.0`, `recovery/back_up/timeout: 15.0`, `recovery/wait` không khai.
|
|
EXPECT_DOUBLE_EQ(recovery_test::findBehavior(registry, "rotate")->timeout(), 20.0);
|
|
EXPECT_DOUBLE_EQ(recovery_test::findBehavior(registry, "back_up")->timeout(), 15.0);
|
|
EXPECT_DOUBLE_EQ(recovery_test::findBehavior(registry, "wait")->timeout(), 0.0);
|
|
}
|
|
|
|
TEST(Registry, MissingLibraryPathIsReportedAndFails)
|
|
{
|
|
VelocityRig rig;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
// `GhostRecovery` được khai trong danh sách nhưng không có khoá library_path.
|
|
EXPECT_FALSE(registry.loadFromConfig(nh, "recovery_missing_library", rig.ctx));
|
|
EXPECT_EQ(registry.size(), 0u);
|
|
}
|
|
|
|
TEST(Registry, OneBadBehaviorDoesNotDropTheGoodOnes)
|
|
{
|
|
VelocityRig rig;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
// Một đường phục hồi hỏng không nên xoá sạch các đường còn lại.
|
|
EXPECT_FALSE(registry.loadFromConfig(nh, "recovery_partial", rig.ctx));
|
|
ASSERT_EQ(registry.size(), 1u);
|
|
EXPECT_EQ(registry.nameAt(0), "wait");
|
|
}
|
|
|
|
TEST(Registry, RefusesPluginsBuiltForAnotherAbiVersionButKeepsTheRest)
|
|
{
|
|
// 2026-09-11: RecoveryBehavior thêm virtual onCancelRequested(); plugin build với header cũ có
|
|
// vtable ngắn hơn và crash đúng lúc cancel. Registry tra `<Type>_abi` trước factory: lệch hoặc
|
|
// thiếu đều bị từ chối, log lỗi, behavior tốt cùng danh sách vẫn được nạp.
|
|
VelocityRig rig;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
EXPECT_FALSE(registry.loadFromConfig(nh, "recovery_stale_abi", rig.ctx));
|
|
ASSERT_EQ(registry.size(), 1u) << "only the ABI-matching plugin may be registered";
|
|
EXPECT_EQ(registry.nameAt(0), "wait");
|
|
}
|
|
|
|
TEST(RecoveryLifecycle, CancelCallsOnCancelRequestedSynchronously)
|
|
{
|
|
// Hook đồng bộ (2026-09-11): chạy NGAY trong cancel(), không cần tick — chỗ duy nhất một behavior
|
|
// có việc chạy dài biết nó bị huỷ khi caller không tick nữa (CANCELLING không tick recovery).
|
|
class HookProbe final : public recovery_core::RecoveryBehavior
|
|
{
|
|
public:
|
|
recovery_core::RecoveryOutputType outputKind() const override
|
|
{
|
|
return recovery_core::RecoveryOutputType::kNone;
|
|
}
|
|
int hook_calls = 0;
|
|
int update_calls = 0;
|
|
|
|
protected:
|
|
bool onConfigure(robot::NodeHandle&) override
|
|
{
|
|
return true;
|
|
}
|
|
bool onStart(const recovery_core::RecoveryGoal&) override
|
|
{
|
|
return true;
|
|
}
|
|
recovery_core::RecoveryResult onUpdate(const robot::Time&, double) override
|
|
{
|
|
++update_calls;
|
|
return recovery_core::RecoveryResult::Running();
|
|
}
|
|
void onCancelRequested() override
|
|
{
|
|
++hook_calls;
|
|
}
|
|
};
|
|
|
|
HookProbe probe;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryContext ctx;
|
|
ASSERT_TRUE(probe.configure("probe", ctx, nh));
|
|
ASSERT_TRUE(probe.start(recovery_core::RecoveryGoal(), robot::Time(1000.0)));
|
|
ASSERT_EQ(probe.update(robot::Time(1000.05)).status, recovery_core::RecoveryStatus::kRunning);
|
|
|
|
probe.cancel();
|
|
EXPECT_EQ(probe.hook_calls, 1) << "onCancelRequested must run inside cancel(), before any tick";
|
|
EXPECT_EQ(probe.update_calls, 1) << "no tick was needed for the hook";
|
|
|
|
probe.cancel();
|
|
EXPECT_EQ(probe.hook_calls, 2) << "every cancel() call reaches the hook";
|
|
}
|
|
|
|
TEST(Registry, MissingBehaviorListFails)
|
|
{
|
|
VelocityRig rig;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
EXPECT_FALSE(registry.loadFromConfig(nh, "namespace_khong_ton_tai", rig.ctx));
|
|
EXPECT_EQ(registry.size(), 0u);
|
|
}
|
|
|
|
TEST(Registry, IndexOutOfRangeIsSafe)
|
|
{
|
|
VelocityRig rig;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
ASSERT_TRUE(registry.loadFromConfig(nh, "recovery", rig.ctx));
|
|
|
|
EXPECT_EQ(registry.at(99), nullptr);
|
|
EXPECT_TRUE(registry.nameAt(99).empty());
|
|
}
|
|
|
|
TEST(Registry, RegisterBehaviorRejectsNull)
|
|
{
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
EXPECT_FALSE(registry.registerBehavior(nullptr));
|
|
EXPECT_EQ(registry.size(), 0u);
|
|
}
|
|
|
|
TEST(Registry, RegisterBehaviorAppendsInOrder)
|
|
{
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
auto first = std::make_shared<MockBehavior>(RecoveryOutputType::kNone);
|
|
auto second = std::make_shared<MockBehavior>(RecoveryOutputType::kNone);
|
|
|
|
ASSERT_TRUE(registry.registerBehavior(first));
|
|
ASSERT_TRUE(registry.registerBehavior(second));
|
|
|
|
ASSERT_EQ(registry.size(), 2u);
|
|
EXPECT_EQ(registry.at(0), first.get());
|
|
EXPECT_EQ(registry.at(1), second.get());
|
|
}
|
|
|
|
TEST(Registry, ClearReleasesBehaviors)
|
|
{
|
|
VelocityRig rig;
|
|
robot::NodeHandle nh;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
ASSERT_TRUE(registry.loadFromConfig(nh, "recovery", rig.ctx));
|
|
ASSERT_GT(registry.size(), 0u);
|
|
|
|
registry.clear();
|
|
EXPECT_EQ(registry.size(), 0u);
|
|
}
|
|
|
|
TEST(Registry, BehaviorsStayUsableAfterLoaderScopeEnds)
|
|
{
|
|
VelocityRig rig;
|
|
recovery_core::RecoveryRegistry registry;
|
|
|
|
{
|
|
// NodeHandle chết trước registry: behavior vẫn phải sống, vì registry mới là thứ giữ .so.
|
|
robot::NodeHandle scoped_nh;
|
|
ASSERT_TRUE(registry.loadFromConfig(scoped_nh, "recovery", rig.ctx));
|
|
}
|
|
|
|
auto* wait = recovery_test::findBehavior(registry, "wait");
|
|
ASSERT_NE(wait, nullptr);
|
|
ASSERT_TRUE(wait->start(recovery_core::RecoveryGoal(), robot::Time(1000.0)));
|
|
EXPECT_EQ(wait->update(robot::Time(1003.0)).status, recovery_core::RecoveryStatus::kSucceeded);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
#ifdef RECOVERY_CORE_TEST_CONFIG_DIR
|
|
setenv("PNKX_NAV_CORE_CONFIG_DIR", RECOVERY_CORE_TEST_CONFIG_DIR, 0);
|
|
#endif
|
|
#ifdef RECOVERY_CORE_TEST_LIBRARY_DIR
|
|
setenv("PNKX_NAV_CORE_LIBRARY_PATH", RECOVERY_CORE_TEST_LIBRARY_DIR, 0);
|
|
#endif
|
|
testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|