Files
action_core/test/noop_action_handler_test.cpp
2026-08-10 10:52:16 +07:00

117 lines
3.7 KiB
C++

/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Kiểm hành vi của handler mặc định qua đúng đường nạp plugin.
*
* `NoopActionHandler` là ví dụ tham chiếu cho contract, nên hành vi của nó phải được khoá lại —
* đặc biệt là các guard cấu hình, vì chúng là thứ ngăn một file YAML gõ nhầm biến thành action
* chạy vĩnh viễn trên robot.
*
* Author: DuongTD
*********************************************************************/
#include <gtest/gtest.h>
#include <cstdlib>
#include <string>
#include <robot/node_handle.h>
#include <action_core/action_registry.h>
namespace
{
using action_core::ActionHandler;
using action_core::ActionRegistry;
using action_core::ActionStatus;
robot_protocol_msgs::Action makeAction(const std::string& type)
{
robot_protocol_msgs::Action action;
action.actionType = type;
action.actionId = type + "-id";
return action;
}
ActionHandler* load(ActionRegistry& registry, robot::NodeHandle& nh, const std::string& type)
{
if (!registry.loadFromConfig(nh, "actions"))
{
return nullptr;
}
return registry.find(type);
}
TEST(NoopActionHandler, ZeroDurationFinishesOnTheFirstTick)
{
robot::NodeHandle nh;
ActionRegistry registry;
ActionHandler* handler = load(registry, nh, "wait");
ASSERT_NE(handler, nullptr);
const robot::Time now = robot::Time::now();
ASSERT_TRUE(handler->start(makeAction("wait"), now));
EXPECT_EQ(handler->update(now).status, ActionStatus::kSucceeded);
}
TEST(NoopActionHandler, MessageNamesTheActionId)
{
robot::NodeHandle nh;
ActionRegistry registry;
ActionHandler* handler = load(registry, nh, "wait");
ASSERT_NE(handler, nullptr);
const robot::Time now = robot::Time::now();
ASSERT_TRUE(handler->start(makeAction("wait"), now));
// Message chỉ để log, nhưng phải chỉ đúng action nào — log không truy được về action nào thì vô
// dụng khi một chặng có nhiều action.
const std::string message = handler->update(now).message;
EXPECT_NE(message.find("wait-id"), std::string::npos) << message;
}
TEST(NoopActionHandler, CancelDoesNotThrowAndCanBeCalledWithoutStart)
{
robot::NodeHandle nh;
ActionRegistry registry;
ActionHandler* handler = load(registry, nh, "wait");
ASSERT_NE(handler, nullptr);
// Caller huỷ một action chưa kịp start là chuyện bình thường (cancel tới trong cùng cycle).
EXPECT_NO_THROW(handler->cancel());
}
TEST(NoopActionHandler, RestartResetsTheClock)
{
robot::NodeHandle nh;
ActionRegistry registry;
ActionHandler* handler = load(registry, nh, "charge");
ASSERT_NE(handler, nullptr);
const robot::Time now = robot::Time::now();
ASSERT_TRUE(handler->start(makeAction("charge"), now));
EXPECT_EQ(handler->update(now + robot::Duration(10.0)).status, ActionStatus::kFailed);
// Handler được dùng lại cho action sau: mốc thời gian phải được gieo lại, nếu không action thứ
// hai hỏng ngay lập tức vì thừa hưởng đồng hồ của action trước.
const robot::Time later = now + robot::Duration(20.0);
ASSERT_TRUE(handler->start(makeAction("charge"), later));
EXPECT_EQ(handler->update(later).status, ActionStatus::kRunning);
EXPECT_EQ(handler->update(later + robot::Duration(1.0)).status, ActionStatus::kSucceeded);
}
} // namespace
int main(int argc, char** argv)
{
#ifdef ACTION_CORE_TEST_CONFIG_DIR
setenv("PNKX_NAV_CORE_CONFIG_DIR", ACTION_CORE_TEST_CONFIG_DIR, 0);
#endif
#ifdef ACTION_CORE_TEST_LIBRARY_DIR
setenv("PNKX_NAV_CORE_LIBRARY_PATH", ACTION_CORE_TEST_LIBRARY_DIR, 0);
#endif
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}