Files
move_base2/test/component_status_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

145 lines
5.2 KiB
C++

/*********************************************************************
*
* Software License Agreement (BSD License)
*
* move_base2 — test bảng trạng thái thành phần: nguồn sự thật cho quiescence và watchdog.
*
* Author: DuongTD
*********************************************************************/
#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <string>
#include <thread>
#include <move_base2/core/component_status.h>
using move_base2::Component;
using move_base2::ComponentPhase;
using move_base2::ComponentStatusBoard;
TEST(ComponentStatusBoard, StartsIdleEverywhere)
{
ComponentStatusBoard board;
for (std::size_t i = 0; i < ComponentStatusBoard::kRows; ++i)
{
const Component c = static_cast<Component>(i);
EXPECT_TRUE(board.isIdle(c)) << move_base2::toString(c);
EXPECT_EQ(board.at(c).progress_ticks.load(), 0u);
}
EXPECT_FALSE(board.anyHung());
EXPECT_TRUE(board.allIdle({Component::kGlobalPlanner, Component::kRecovery, Component::kAction}));
}
TEST(ComponentStatusBoard, EnterChangesPhaseAndCountsAsProgress)
{
ComponentStatusBoard board;
board.enter(Component::kGlobalPlanner, ComponentPhase::kBusy);
EXPECT_EQ(board.phase(Component::kGlobalPlanner), ComponentPhase::kBusy);
EXPECT_FALSE(board.isIdle(Component::kGlobalPlanner));
EXPECT_FALSE(board.allIdle({Component::kGlobalPlanner, Component::kRecovery}));
EXPECT_EQ(board.at(Component::kGlobalPlanner).progress_ticks.load(), 1u);
EXPECT_GT(board.at(Component::kGlobalPlanner).phase_since_ns.load(), 0);
board.enter(Component::kGlobalPlanner, ComponentPhase::kStopping);
EXPECT_EQ(board.phase(Component::kGlobalPlanner), ComponentPhase::kStopping);
board.enter(Component::kGlobalPlanner, ComponentPhase::kIdle);
EXPECT_TRUE(board.isIdle(Component::kGlobalPlanner));
EXPECT_EQ(board.at(Component::kGlobalPlanner).progress_ticks.load(), 3u);
}
TEST(ComponentStatusBoard, ProgressDoesNotChangePhase)
{
ComponentStatusBoard board;
board.enter(Component::kRecovery, ComponentPhase::kBusy);
const std::int64_t since = board.at(Component::kRecovery).phase_since_ns.load();
board.progress(Component::kRecovery);
board.progress(Component::kRecovery);
EXPECT_EQ(board.phase(Component::kRecovery), ComponentPhase::kBusy);
EXPECT_EQ(board.at(Component::kRecovery).phase_since_ns.load(), since)
<< "progress must not restart the phase clock";
EXPECT_EQ(board.at(Component::kRecovery).progress_ticks.load(), 3u);
EXPECT_GE(board.at(Component::kRecovery).last_progress_ns.load(), since);
}
TEST(ComponentStatusBoard, SecondsInPhaseAndSinceProgressGrowWithWallTime)
{
ComponentStatusBoard board;
board.enter(Component::kControlLoop, ComponentPhase::kBusy);
std::this_thread::sleep_for(std::chrono::milliseconds(20));
EXPECT_GE(board.secondsInPhase(Component::kControlLoop), 0.015);
EXPECT_GE(board.secondsSinceProgress(Component::kControlLoop), 0.015);
board.progress(Component::kControlLoop);
EXPECT_LT(board.secondsSinceProgress(Component::kControlLoop), 0.015);
EXPECT_GE(board.secondsInPhase(Component::kControlLoop), 0.015) << "phase clock untouched";
}
TEST(ComponentStatusBoard, AnyHungSeesASingleHungRow)
{
ComponentStatusBoard board;
board.enter(Component::kCostmapLocal, ComponentPhase::kHung);
EXPECT_TRUE(board.anyHung());
board.enter(Component::kCostmapLocal, ComponentPhase::kIdle);
EXPECT_FALSE(board.anyHung());
}
TEST(ComponentStatusBoard, EveryComponentAndPhaseHasAName)
{
for (std::size_t i = 0; i < ComponentStatusBoard::kRows; ++i)
{
const std::string name = move_base2::toString(static_cast<Component>(i));
EXPECT_FALSE(name.empty());
EXPECT_NE(name, "unknown");
}
for (const ComponentPhase p : {ComponentPhase::kIdle, ComponentPhase::kBusy, ComponentPhase::kStopping,
ComponentPhase::kPaused, ComponentPhase::kHung,
ComponentPhase::kWaitingForCostmap})
{
const std::string name = move_base2::toString(p);
EXPECT_FALSE(name.empty());
EXPECT_NE(name, "unknown");
}
}
TEST(ComponentStatusBoard, ConcurrentWriterAndReaderDoNotTearOrCrash)
{
// Mô hình thật: thread planner ghi hàng của nó, control thread và watchdog đọc. Không lock — mọi
// field atomic, đọc phải luôn thấy một phase hợp lệ.
ComponentStatusBoard board;
std::atomic<bool> stop{false};
std::atomic<std::uint64_t> bad_reads{0};
std::thread writer([&] {
for (int i = 0; i < 200000; ++i)
{
board.enter(Component::kGlobalPlanner, (i & 1) ? ComponentPhase::kBusy : ComponentPhase::kIdle);
board.progress(Component::kGlobalPlanner);
}
stop = true;
});
std::thread reader([&] {
while (!stop)
{
const ComponentPhase p = board.phase(Component::kGlobalPlanner);
if (p != ComponentPhase::kBusy && p != ComponentPhase::kIdle)
{
++bad_reads;
}
(void)board.secondsInPhase(Component::kGlobalPlanner);
(void)board.allIdle({Component::kGlobalPlanner, Component::kRecovery});
}
});
writer.join();
reader.join();
EXPECT_EQ(bad_reads.load(), 0u);
EXPECT_EQ(board.at(Component::kGlobalPlanner).progress_ticks.load(), 400000u);
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}