Files
move_base2/test/watchdog_test.cpp
duongtd 051e18f989 feat(move_base2): direction zone delivery and costmap readiness wait
- SensorGateway::pushStaticMap also dispatches to DIRECTIONAL_LAYER on
  the global costmap, before STATIC_LAYER: with a same-geometry map the
  directional layer goes PENDING before StaticLayer can report current,
  so a plan cannot pair the new map with the old zones.
- Static maps are replayed in arrival order instead of topic order, so a
  direction grid is never replayed before its static map.
- PlannerRunner checks every enabled global layer under the same lock as
  the snapshot. While a layer is not current it keeps the goal, reports
  waiting_for_costmap, logs the stale layers (throttled) and spends no
  planner_patience or watchdog budget. After costmap_ready_timeout [s]
  (default 10, 0 = legacy behaviour) the attempt fails as before. The
  key is added to config/runtime/move_base_common_params.yaml.
- Test fixture destroys the server before its fake ports (the control
  thread could call a destroyed port).

Full history: branch feature/direction-zone.
2026-11-04 13:24:22 +07:00

210 lines
5.8 KiB
C++

/*********************************************************************
*
* Software License Agreement (BSD License)
*
* move_base2 — test watchdog: kết luận treo từ bảng trạng thái, trên thread riêng.
*
* Author: DuongTD
*********************************************************************/
#include <gtest/gtest.h>
#include <chrono>
#include <limits>
#include <string>
#include <thread>
#include <move_base2/core/component_status.h>
#include <move_base2/io/watchdog.h>
using move_base2::Component;
using move_base2::ComponentPhase;
using move_base2::ComponentStatusBoard;
using move_base2::Watchdog;
using move_base2::WatchdogConfig;
namespace
{
WatchdogConfig fastConfig()
{
WatchdogConfig c;
c.period = 0.01; // [s]
c.planner_timeout = 0.05; // [s]
c.local_planner_call_budget = 0.05;
c.recovery_stall_budget = 0.05;
c.action_stall_budget = 0.05;
c.control_loop_stall_budget = 0.05;
return c;
}
void sleepMs(int ms)
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
} // namespace
TEST(WatchdogConfig, RejectsNegativePeriodAndNonFiniteBudgets)
{
WatchdogConfig c;
std::string error;
c.period = -0.1;
EXPECT_FALSE(c.validate(error));
c = WatchdogConfig();
c.planner_timeout = std::numeric_limits<double>::infinity();
EXPECT_FALSE(c.validate(error));
c = WatchdogConfig();
c.period = 0.0; // tắt = hợp lệ
EXPECT_TRUE(c.validate(error)) << error;
EXPECT_NE(c.describe().find("planner_timeout"), std::string::npos);
}
TEST(Watchdog, IdleRowsAreNeverHung)
{
ComponentStatusBoard board;
Watchdog dog;
std::string error;
WatchdogConfig c = fastConfig();
c.period = 0.0; // không thread — checkOnce() tay
ASSERT_TRUE(dog.start(&board, c, error)) << error;
board.enter(Component::kGlobalPlanner, ComponentPhase::kIdle);
sleepMs(80);
dog.checkOnce();
EXPECT_FALSE(dog.anyHung()) << "idle for a long time is not 'hung'";
}
TEST(Watchdog, WaitingForCostmapDoesNotConsumePlannerBudget)
{
ComponentStatusBoard board;
Watchdog dog;
std::string error;
WatchdogConfig c = fastConfig();
c.period = 0.0;
ASSERT_TRUE(dog.start(&board, c, error)) << error;
board.enter(Component::kGlobalPlanner, ComponentPhase::kWaitingForCostmap);
sleepMs(80);
dog.checkOnce();
EXPECT_FALSE(dog.hung(Component::kGlobalPlanner));
}
TEST(Watchdog, BusyWithoutProgressBecomesHungAndRecoversOnProgress)
{
ComponentStatusBoard board;
Watchdog dog;
std::string error;
WatchdogConfig c = fastConfig();
c.period = 0.0;
ASSERT_TRUE(dog.start(&board, c, error)) << error;
board.enter(Component::kGlobalPlanner, ComponentPhase::kBusy);
dog.checkOnce();
EXPECT_FALSE(dog.hung(Component::kGlobalPlanner)) << "just started";
sleepMs(30);
board.progress(Component::kGlobalPlanner); // vẫn nhích → không treo dù bận lâu
sleepMs(30);
dog.checkOnce();
EXPECT_FALSE(dog.hung(Component::kGlobalPlanner)) << "busy but progressing";
sleepMs(80); // > 0.05 s không nhích
dog.checkOnce();
EXPECT_TRUE(dog.hung(Component::kGlobalPlanner));
EXPECT_TRUE(dog.anyHung());
EXPECT_NE(dog.report().find("global_planner"), std::string::npos);
board.progress(Component::kGlobalPlanner);
dog.checkOnce();
EXPECT_FALSE(dog.hung(Component::kGlobalPlanner)) << "recovered";
}
TEST(Watchdog, StoppingCountsAsWatchedButPausedDoesNot)
{
ComponentStatusBoard board;
Watchdog dog;
std::string error;
WatchdogConfig c = fastConfig();
c.period = 0.0;
ASSERT_TRUE(dog.start(&board, c, error)) << error;
board.enter(Component::kRecovery, ComponentPhase::kStopping);
board.enter(Component::kAction, ComponentPhase::kPaused);
sleepMs(80);
dog.checkOnce();
EXPECT_TRUE(dog.hung(Component::kRecovery)) << "told to stop and never returned = hung";
EXPECT_FALSE(dog.hung(Component::kAction)) << "a paused handler is not expected to progress";
}
TEST(Watchdog, ZeroBudgetDisablesThatRow)
{
ComponentStatusBoard board;
Watchdog dog;
std::string error;
WatchdogConfig c = fastConfig();
c.period = 0.0;
c.action_stall_budget = 0.0;
ASSERT_TRUE(dog.start(&board, c, error)) << error;
board.enter(Component::kAction, ComponentPhase::kBusy);
sleepMs(80);
dog.checkOnce();
EXPECT_FALSE(dog.hung(Component::kAction));
}
TEST(Watchdog, ControlLoopHeartbeatStallIsDetectedFromTheWatchdogThread)
{
// Mô hình thật: control thread heartbeat cuối mỗi step(); ngừng heartbeat = treo. Watchdog chạy
// thread riêng và phải tự thấy — đây là thứ control thread không tự làm được.
ComponentStatusBoard board;
Watchdog dog;
std::string error;
ASSERT_TRUE(dog.start(&board, fastConfig(), error)) << error;
ASSERT_TRUE(dog.running());
for (int i = 0; i < 5; ++i)
{
board.progress(Component::kControlLoop);
sleepMs(10);
}
EXPECT_FALSE(dog.controlLoopHung());
sleepMs(120); // không heartbeat
EXPECT_TRUE(dog.controlLoopHung());
board.progress(Component::kControlLoop);
sleepMs(40);
EXPECT_FALSE(dog.controlLoopHung());
dog.stop();
EXPECT_FALSE(dog.running());
}
TEST(Watchdog, RowThatNeverProgressedIsNotHung)
{
// Runtime mới lên: control loop chưa chạy cycle nào, last_progress = 0 — không được báo treo ngay.
ComponentStatusBoard board;
Watchdog dog;
std::string error;
WatchdogConfig c = fastConfig();
c.period = 0.0;
ASSERT_TRUE(dog.start(&board, c, error)) << error;
sleepMs(80);
dog.checkOnce();
EXPECT_FALSE(dog.controlLoopHung());
}
TEST(Watchdog, StartRequiresABoardAndRefusesDoubleStart)
{
Watchdog dog;
std::string error;
EXPECT_FALSE(dog.start(nullptr, fastConfig(), error));
ComponentStatusBoard board;
ASSERT_TRUE(dog.start(&board, fastConfig(), error)) << error;
EXPECT_FALSE(dog.start(&board, fastConfig(), error)) << "already running";
dog.stop();
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}