#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace robot_costmap_2d; namespace { class LayerTest : public ::testing::Test { protected: std::string directory_; std::string old_env_; bool had_env_ = false; tf3::BufferCore tf_{tf3::Duration(10.0)}; void SetUp() override { const char* old = std::getenv("PNKX_NAV_CORE_CONFIG_DIR"); if (old) { had_env_ = true; old_env_ = old; } char pattern[] = "/tmp/direction-layer-test-XXXXXX"; char* path = mkdtemp(pattern); ASSERT_NE(path, nullptr); directory_ = path; std::filesystem::create_directory(directory_ + "/config"); ASSERT_EQ(setenv("PNKX_NAV_CORE_CONFIG_DIR", directory_.c_str(), 1), 0); config(true); } void TearDown() override { if (had_env_) setenv("PNKX_NAV_CORE_CONFIG_DIR", old_env_.c_str(), 1); else unsetenv("PNKX_NAV_CORE_CONFIG_DIR"); std::filesystem::remove_all(directory_); } void config(bool enabled) { std::ofstream out(directory_ + "/config/directional_layer_params.yaml"); out << "directional_layer:\n enabled: " << (enabled ? "true" : "false") << "\n map_topic: map\n direction_topic: direction_map\n debug_state: false\n"; } robot_nav_msgs::OccupancyGrid grid() { robot_nav_msgs::OccupancyGrid g; g.header.frame_id = "map"; g.info.width = 4; g.info.height = 4; g.info.resolution = 1; g.data.assign(16, 0); return g; } }; TEST_F(LayerTest, LifecycleAndProvider) { LayeredCostmap master("map", false, false); master.resizeMap(4, 4, 1, 0, 0); EXPECT_EQ(master.getDirectionFieldSnapshot().state, DirectionFieldState::kDisabled); auto layer = boost::make_shared(); layer->initialize(&master, "directional", &tf_); master.addPlugin(layer); auto g = grid(); auto s = master.getDirectionFieldSnapshot(); EXPECT_EQ(s.state, DirectionFieldState::kPending); EXPECT_FALSE(layer->isCurrent()); layer->dataCallBack(g, "direction_map"); EXPECT_EQ(layer->directionSnapshot().generation, s.generation); double min_x = 1; double min_y = 2; double max_x = 3; double max_y = 4; layer->updateBounds(0, 0, 0, &min_x, &min_y, &max_x, &max_y); EXPECT_DOUBLE_EQ(min_x, 1); EXPECT_DOUBLE_EQ(min_y, 2); EXPECT_DOUBLE_EQ(max_x, 3); EXPECT_DOUBLE_EQ(max_y, 4); auto& costs = *master.getCostmap(); costs.setCost(0, 0, 42); layer->updateCosts(costs, 0, 0, 4, 4); EXPECT_EQ(costs.getCost(0, 0), 42); layer->dataCallBack(7, "map"); layer->dataCallBack(g, "other_topic"); EXPECT_EQ(layer->directionSnapshot().generation, s.generation); layer->dataCallBack(g, "/map"); s = layer->directionSnapshot(); EXPECT_EQ(s.state, DirectionFieldState::kPending); g.data[0] = 1; layer->dataCallBack(g, "/direction_map"); auto ready = master.getDirectionFieldSnapshot(); EXPECT_EQ(ready.state, DirectionFieldState::kReady); ASSERT_TRUE(ready.field); EXPECT_TRUE(layer->isCurrent()); EXPECT_GT(ready.generation, s.generation); g.data[0] = 100; layer->dataCallBack(g, "direction_map"); s = layer->directionSnapshot(); EXPECT_EQ(s.generation, ready.generation); EXPECT_EQ(s.field, ready.field); layer->reset(); EXPECT_EQ(layer->directionSnapshot().field, ready.field); g.data.clear(); layer->dataCallBack(g, "direction_map"); s = layer->directionSnapshot(); ASSERT_TRUE(s.field); EXPECT_FALSE(s.field->hasConstraints()); EXPECT_GT(s.generation, ready.generation); layer->dataCallBack(g, "map"); s = layer->directionSnapshot(); EXPECT_EQ(s.state, DirectionFieldState::kPending); EXPECT_FALSE(s.field); EXPECT_FALSE(layer->isCurrent()); layer->dataCallBack(g, "direction_map"); layer->matchSize(); EXPECT_EQ(layer->directionSnapshot().state, DirectionFieldState::kPending); layer->dataCallBack(g, "direction_map"); master.getCostmap()->resizeMap(3, 3, 1, 0, 0); layer->reset(); EXPECT_EQ(layer->directionSnapshot().state, DirectionFieldState::kPending); master.resizeMap(4, 4, 1, 0, 0); layer->dataCallBack(g, "direction_map"); master.resizeMap(3, 3, 1, 0, 0); EXPECT_EQ(layer->directionSnapshot().state, DirectionFieldState::kPending); } TEST_F(LayerTest, DisabledAndMultipleProviders) { config(false); LayeredCostmap master("map", false, false); master.resizeMap(4, 4, 1, 0, 0); auto disabled = boost::make_shared(); disabled->initialize(&master, "directional", &tf_); master.addPlugin(disabled); EXPECT_TRUE(disabled->isCurrent()); EXPECT_EQ(master.getDirectionFieldSnapshot().state, DirectionFieldState::kDisabled); config(true); auto a = boost::make_shared(); a->initialize(&master, "first", &tf_); master.addPlugin(a); auto b = boost::make_shared(); b->initialize(&master, "second", &tf_); master.addPlugin(b); EXPECT_EQ(master.getDirectionFieldSnapshot().state, DirectionFieldState::kPending); } TEST_F(LayerTest, RollingCostmapStaysPending) { LayeredCostmap master("map", true, false); master.resizeMap(4, 4, 1, 0, 0); DirectionalLayer layer; layer.initialize(&master, "directional", &tf_); auto g = grid(); layer.dataCallBack(g, "map"); layer.dataCallBack(g, "direction_map"); EXPECT_EQ(layer.directionSnapshot().state, DirectionFieldState::kPending); EXPECT_FALSE(layer.isCurrent()); } TEST_F(LayerTest, StaticMapCallbackDoesNotWaitForMasterMutex) { LayeredCostmap master("map", false, false); master.resizeMap(4, 4, 1, 0, 0); DirectionalLayer layer; layer.initialize(&master, "directional", &tf_); auto g = grid(); std::mutex completion_mutex; std::condition_variable completion; bool done = false; bool completed_while_locked = false; std::thread callback; { boost::unique_lock master_lock(*master.getCostmap()->getMutex()); callback = std::thread([&] { layer.dataCallBack(g, "map"); { std::lock_guard lock(completion_mutex); done = true; } completion.notify_one(); }); std::unique_lock lock(completion_mutex); completed_while_locked = completion.wait_for(lock, std::chrono::seconds(2), [&] { return done; }); } callback.join(); EXPECT_TRUE(completed_while_locked); EXPECT_EQ(layer.directionSnapshot().state, DirectionFieldState::kPending); } TEST_F(LayerTest, OldSnapshotSurvivesConcurrentReplacement) { LayeredCostmap master("map", false, false); master.resizeMap(4, 4, 1, 0, 0); auto layer = boost::make_shared(); layer->initialize(&master, "directional", &tf_); master.addPlugin(layer); auto g = grid(); g.data[0] = 1; layer->dataCallBack(g, "map"); layer->dataCallBack(g, "direction_map"); auto old = master.getDirectionFieldSnapshot(); ASSERT_TRUE(old.field); std::atomic start{false}; std::atomic ok{true}; std::thread reader([&] { while (!start.load()) { } for (int i = 0; i < 1000; ++i) { auto s = master.getDirectionFieldSnapshot(); if (!s.field || old.field->code(0, 0) != 1) ok = false; } }); std::thread updater([&] { while (!start.load()) { } for (int i = 0; i < 1000; ++i) layer->updateBounds(0, 0, 0, nullptr, nullptr, nullptr, nullptr); }); start = true; g.data[0] = 3; layer->dataCallBack(g, "direction_map"); reader.join(); updater.join(); EXPECT_TRUE(ok.load()); EXPECT_EQ(old.field->code(0, 0), 1); EXPECT_EQ(master.getDirectionFieldSnapshot().field->code(0, 0), 3); } } // namespace