NavigationServer::emergency() phan anh ngay tren thread goi (getTwist tra 0, getFeedback bao EMERGENCY) truoc khi ham tra ve, du control thread dang block trong plugin nao; don dep that (planner/recovery/ action/mission layer) chay o chu ky ke cua control thread. State NavigationState::kEmergency la state GIU, khong terminal: pause/resume/ cancel/goal moi deu bi bo qua, chi clearEmergency() moi thoat, ve kIdle. MissionLayer: emergency() -> drain hang doi, goi cancel toi navigation dung 1 lan, chuyen MissionState::EMERGENCY; clearEmergency() ve CLEAR_EMERGENCY, khong tu chay lai gi, cho order ke tiep. Test moi cho ca 2 lop (navigation_server_test, mission_layer_test, state_machine_test, walking_skeleton_test) phu: force-zero-truoc-cycle, tu choi moveTo/instantActions khi dang chot, huy goal con pending, mo chot roi nhan goal moi.
785 lines
29 KiB
C++
785 lines
29 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* move_base2 — test MissionLayer lắp với MissionAdapterBridge thật.
|
|
*
|
|
* Đây là chỗ kiểm thứ mà `mission_adapter_bridge_test` không kiểm được: bridge có được nối vào một
|
|
* mission layer ĐANG CHẠY hay không, và một yêu cầu nhiều chặng có thật sự đi hết chặng này tới
|
|
* chặng kia hay không. Bridge đúng mà layer không được dựng thì mọi test bridge vẫn xanh trong khi
|
|
* robot chỉ chạy được chặng đầu — đúng trạng thái của gói trước lần sửa này.
|
|
*
|
|
* Nguồn mission ở đây được đăng ký thẳng vào registry (không qua Boost.DLL): đường nạp `.so` đã có
|
|
* `plugin_registry_test` của `mission_adapters` lo, còn thứ cần khoá tại đây là chuỗi sự kiện.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <chrono>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include <mission_adapters/mission_request.h>
|
|
#include <mission_adapters/types.h>
|
|
|
|
#include <move_base2/bridges/mission_layer.h>
|
|
|
|
namespace
|
|
{
|
|
using mission_adapters::ConversionResult;
|
|
using mission_adapters::Mission;
|
|
using mission_adapters::MissionRequest;
|
|
using mission_adapters::MissionSourceAdapter;
|
|
using mission_adapters::MissionState;
|
|
using mission_adapters::SubmitMode;
|
|
using move_base2::MissionAdapterBridge;
|
|
using move_base2::MissionLayer;
|
|
using move_base2::NavigationOutcome;
|
|
using move_base2::NavigationRequest;
|
|
|
|
constexpr auto kTimeout = std::chrono::seconds(2);
|
|
constexpr auto kPollStep = std::chrono::milliseconds(2);
|
|
|
|
/**
|
|
* @brief Nguồn giả cắt một pose thành @c legs chặng, mô phỏng đúng hình dạng của một VDA5050 order
|
|
* nhiều node có action.
|
|
*/
|
|
class SplittingAdapter : public MissionSourceAdapter
|
|
{
|
|
public:
|
|
explicit SplittingAdapter(std::size_t legs, SubmitMode mode = SubmitMode::kReplace)
|
|
: legs_(legs), mode_(mode)
|
|
{
|
|
}
|
|
|
|
bool configure(const std::string&, robot::NodeHandle&) override
|
|
{
|
|
return true;
|
|
}
|
|
|
|
std::string schema() const override
|
|
{
|
|
return mission_adapters::schema::kPoseStamped;
|
|
}
|
|
|
|
bool validate(const MissionRequest& request, std::string& reason) const override
|
|
{
|
|
if (!request.pose)
|
|
{
|
|
reason = "request carries no pose";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
ConversionResult convert(const MissionRequest& request) override
|
|
{
|
|
ConversionResult result;
|
|
result.mode = mode_;
|
|
for (std::size_t i = 0; i < legs_; ++i)
|
|
{
|
|
auto mission = std::make_shared<Mission>();
|
|
mission->has_goal = true;
|
|
mission->goal = *request.pose;
|
|
// Mỗi chặng xa hơn chặng trước một mét — đủ để test phân biệt được chặng nào đang chạy.
|
|
mission->goal.pose.position.x = static_cast<double>(i + 1); // [m]
|
|
result.missions.push_back(mission);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
std::size_t legs_;
|
|
SubmitMode mode_;
|
|
};
|
|
|
|
robot_geometry_msgs::PoseStamped makeGoal()
|
|
{
|
|
robot_geometry_msgs::PoseStamped goal;
|
|
goal.header.frame_id = "map";
|
|
goal.pose.orientation.w = 1.0;
|
|
return goal;
|
|
}
|
|
|
|
/// @brief Layer + bridge đã nối, cộng chỗ nhận yêu cầu như control thread thật.
|
|
class Fixture
|
|
{
|
|
public:
|
|
explicit Fixture(std::size_t legs, SubmitMode mode = SubmitMode::kReplace)
|
|
{
|
|
adapter_ = std::make_shared<SplittingAdapter>(legs, mode);
|
|
EXPECT_TRUE(layer_.registry().registerAdapter(adapter_));
|
|
layer_.markActiveForTesting();
|
|
layer_.attach(bridge_);
|
|
|
|
bridge_.setRequestCallback([this](const NavigationRequest& request) {
|
|
received_.push_back(request);
|
|
});
|
|
bridge_.setCancelCallback([this]() { ++cancel_calls_; });
|
|
|
|
bridge_.start();
|
|
layer_.start();
|
|
}
|
|
|
|
~Fixture()
|
|
{
|
|
layer_.stop();
|
|
bridge_.stop();
|
|
}
|
|
|
|
/**
|
|
* @brief Quay control thread cho tới khi một chặng được đẩy xuống, hoặc hết thời gian chờ.
|
|
* @return false nếu không có chặng nào tới — dùng để khẳng định "KHÔNG được có chặng mới".
|
|
*/
|
|
bool pumpUntilRequest()
|
|
{
|
|
const auto deadline = std::chrono::steady_clock::now() + kTimeout;
|
|
while (std::chrono::steady_clock::now() < deadline)
|
|
{
|
|
if (bridge_.pumpPendingRequest())
|
|
{
|
|
return true;
|
|
}
|
|
std::this_thread::sleep_for(kPollStep);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// @brief Chờ mission layer đạt tới trạng thái mong đợi.
|
|
bool waitForState(MissionState expected)
|
|
{
|
|
const auto deadline = std::chrono::steady_clock::now() + kTimeout;
|
|
while (std::chrono::steady_clock::now() < deadline)
|
|
{
|
|
if (layer_.state() == expected)
|
|
{
|
|
return true;
|
|
}
|
|
std::this_thread::sleep_for(kPollStep);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// @brief Báo kết cục của chặng vừa nhận, đúng như ControlLoop làm ở cuối một phiên.
|
|
void finishLastLeg(NavigationOutcome outcome)
|
|
{
|
|
ASSERT_FALSE(received_.empty());
|
|
bridge_.reportOutcome(received_.back().mission_sequence_id, outcome);
|
|
}
|
|
|
|
MissionLayer layer_;
|
|
MissionAdapterBridge bridge_;
|
|
std::vector<NavigationRequest> received_;
|
|
int cancel_calls_ = 0;
|
|
std::shared_ptr<SplittingAdapter> adapter_;
|
|
};
|
|
|
|
// ================================================================================================
|
|
// Chuỗi nhiều chặng — lý do lớp này tồn tại
|
|
// ================================================================================================
|
|
|
|
TEST(MissionLayerTest, ThreeLegOrderReachesNavigationOneLegAtATime)
|
|
{
|
|
Fixture fixture(3);
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal()));
|
|
|
|
// Chặng 1
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
ASSERT_EQ(1u, fixture.received_.size());
|
|
EXPECT_DOUBLE_EQ(1.0, fixture.received_[0].goal.pose.position.x);
|
|
EXPECT_NE(0u, fixture.received_[0].mission_sequence_id);
|
|
|
|
// Chặng 2 chỉ được giao SAU khi chặng 1 báo xong: hàng đợi tuần tự, không phải bắn hết một lượt.
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
ASSERT_EQ(2u, fixture.received_.size());
|
|
EXPECT_DOUBLE_EQ(2.0, fixture.received_[1].goal.pose.position.x);
|
|
|
|
// Chặng 3
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
ASSERT_EQ(3u, fixture.received_.size());
|
|
EXPECT_DOUBLE_EQ(3.0, fixture.received_[2].goal.pose.position.x);
|
|
|
|
// Mỗi chặng một id riêng: outcome trễ của chặng cũ không thể được tính cho chặng mới.
|
|
EXPECT_NE(fixture.received_[0].mission_sequence_id, fixture.received_[1].mission_sequence_id);
|
|
EXPECT_NE(fixture.received_[1].mission_sequence_id, fixture.received_[2].mission_sequence_id);
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
EXPECT_TRUE(fixture.waitForState(MissionState::COMPLETED));
|
|
EXPECT_FALSE(fixture.layer_.hasMission());
|
|
}
|
|
|
|
TEST(MissionLayerTest, MissionStillPendingBetweenLegs)
|
|
{
|
|
Fixture fixture(2);
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal()));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
|
|
// Đây là tín hiệu mà NavigationServer dùng để KHÔNG báo SUCCEEDED cho host giữa hai chặng. Sai ở
|
|
// đây nghĩa là fleet master nghe "đã tới node cuối" khi robot mới đi được nửa tuyến.
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
EXPECT_TRUE(fixture.bridge_.hasActiveMission());
|
|
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
EXPECT_TRUE(fixture.waitForState(MissionState::COMPLETED));
|
|
EXPECT_FALSE(fixture.bridge_.hasActiveMission());
|
|
}
|
|
|
|
// ================================================================================================
|
|
// Đường lỗi và đường huỷ
|
|
// ================================================================================================
|
|
|
|
TEST(MissionLayerTest, FailedLegClearsTheRestOfTheQueue)
|
|
{
|
|
Fixture fixture(3);
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal()));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
|
|
// clear_queue_on_failure mặc định true: không tới được node n thì chạy tiếp chặng n+1 là cắt ngang
|
|
// đoạn đường fleet manager chưa cho phép đi.
|
|
fixture.finishLastLeg(NavigationOutcome::kFailed);
|
|
EXPECT_TRUE(fixture.waitForState(MissionState::FAILED));
|
|
EXPECT_FALSE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ(1u, fixture.received_.size());
|
|
}
|
|
|
|
TEST(MissionLayerTest, CancelStopsNavigationAndDropsTheQueue)
|
|
{
|
|
Fixture fixture(3);
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal()));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
|
|
fixture.layer_.cancel();
|
|
EXPECT_TRUE(fixture.waitForState(MissionState::CANCELLED));
|
|
|
|
// Huỷ phải dừng được robot, không chỉ xoá hàng đợi trong bộ nhớ (A3).
|
|
const auto deadline = std::chrono::steady_clock::now() + kTimeout;
|
|
while (fixture.cancel_calls_ == 0 && std::chrono::steady_clock::now() < deadline)
|
|
{
|
|
std::this_thread::sleep_for(kPollStep);
|
|
}
|
|
EXPECT_GE(fixture.cancel_calls_, 1);
|
|
|
|
EXPECT_FALSE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ(1u, fixture.received_.size());
|
|
}
|
|
|
|
TEST(MissionLayerTest, EmergencyStopsNavigationWipesTheQueueAndBlocksSubmitUntilCleared)
|
|
{
|
|
// Đường 2026-09-14: NavigationServer::emergency() → drain → MissionLayer::emergency(). Layer đã có
|
|
// sẵn hàm này; test khoá ba tính chất host dựa vào: robot được bảo dừng, hàng đợi trống, và không
|
|
// order nào được nhận cho tới khi gỡ chốt.
|
|
Fixture fixture(3);
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal()));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
|
|
fixture.layer_.emergency();
|
|
EXPECT_TRUE(fixture.waitForState(MissionState::EMERGENCY));
|
|
|
|
const auto deadline = std::chrono::steady_clock::now() + kTimeout;
|
|
while (fixture.cancel_calls_ == 0 && std::chrono::steady_clock::now() < deadline)
|
|
{
|
|
std::this_thread::sleep_for(kPollStep);
|
|
}
|
|
EXPECT_GE(fixture.cancel_calls_, 1) << "emergency must ask navigation to stop, not just forget the queue";
|
|
EXPECT_FALSE(fixture.layer_.hasMission()) << "the remaining legs are gone";
|
|
|
|
// Chốt: order mới bị manager gạt (trên thread sự kiện) — không có chặng nào xuống, state giữ nguyên.
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal())) << "submitGoal only means 'queued on the event bus'";
|
|
EXPECT_FALSE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ(fixture.layer_.state(), MissionState::EMERGENCY);
|
|
EXPECT_EQ(1u, fixture.received_.size());
|
|
|
|
// Gỡ chốt: về CLEAR_EMERGENCY, KHÔNG tự chạy lại gì; order kế mới được nhận.
|
|
fixture.layer_.clearEmergency();
|
|
EXPECT_TRUE(fixture.waitForState(MissionState::CLEAR_EMERGENCY));
|
|
EXPECT_FALSE(fixture.pumpUntilRequest());
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal()));
|
|
EXPECT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ(2u, fixture.received_.size());
|
|
}
|
|
|
|
TEST(MissionLayerTest, PausedLayerHoldsTheQueueUntilResume)
|
|
{
|
|
Fixture fixture(2);
|
|
|
|
fixture.layer_.pause();
|
|
ASSERT_TRUE(fixture.waitForState(MissionState::PAUSED));
|
|
|
|
// Yêu cầu tới trong lúc người vận hành đang chủ động dừng: nhận vào hàng đợi nhưng KHÔNG tự chạy.
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal()));
|
|
EXPECT_FALSE(fixture.pumpUntilRequest());
|
|
EXPECT_TRUE(fixture.received_.empty());
|
|
|
|
fixture.layer_.resume();
|
|
EXPECT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ(1u, fixture.received_.size());
|
|
}
|
|
|
|
// ================================================================================================
|
|
// Định tuyến: layer chỉ nhận thứ nó có nguồn để xử lý
|
|
// ================================================================================================
|
|
|
|
TEST(MissionLayerTest, RejectsSchemaWithNoRegisteredSource)
|
|
{
|
|
Fixture fixture(1);
|
|
|
|
// Không có nguồn nào khai schema `vda5050.order` — layer phải TỪ CHỐI để NavigationServer biết
|
|
// đường mà rơi về nhánh trực tiếp, thay vì nuốt order rồi im lặng.
|
|
robot_protocol_msgs::Order order;
|
|
EXPECT_FALSE(fixture.layer_.handles(mission_adapters::schema::kVda5050Order));
|
|
EXPECT_FALSE(fixture.layer_.submitOrder(order));
|
|
}
|
|
|
|
TEST(MissionLayerTest, RejectsEverythingBeforeStartAndAfterStop)
|
|
{
|
|
MissionLayer layer;
|
|
MissionAdapterBridge bridge;
|
|
auto adapter = std::make_shared<SplittingAdapter>(1);
|
|
ASSERT_TRUE(layer.registry().registerAdapter(adapter));
|
|
layer.markActiveForTesting();
|
|
layer.attach(bridge);
|
|
|
|
// Chưa start: không được nhận việc mà sẽ không ai chạy.
|
|
EXPECT_FALSE(layer.submitGoal(makeGoal()));
|
|
|
|
bridge.start();
|
|
layer.start();
|
|
EXPECT_TRUE(layer.submitGoal(makeGoal()));
|
|
|
|
layer.stop();
|
|
bridge.stop();
|
|
EXPECT_FALSE(layer.submitGoal(makeGoal()));
|
|
}
|
|
|
|
TEST(MissionLayerTest, InactiveLayerNeverStarts)
|
|
{
|
|
// Không nguồn nào -> configure sẽ hỏng ở runtime thật; ở đây kiểm bất biến tương ứng: layer không
|
|
// active thì start() là no-op và mọi đường vào đều đóng.
|
|
MissionLayer layer;
|
|
MissionAdapterBridge bridge;
|
|
layer.attach(bridge);
|
|
layer.start();
|
|
|
|
EXPECT_FALSE(layer.active());
|
|
EXPECT_FALSE(layer.started());
|
|
EXPECT_EQ(0u, layer.sourceCount());
|
|
EXPECT_FALSE(layer.submitGoal(makeGoal()));
|
|
}
|
|
|
|
// ================================================================================================
|
|
// Order update — phần release thêm nối tiếp, không chạy lại từ đầu
|
|
// ================================================================================================
|
|
|
|
TEST(MissionLayerTest, AppendModeDoesNotPreemptTheRunningLeg)
|
|
{
|
|
Fixture fixture(1, SubmitMode::kAppend);
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal()));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
const std::uint64_t running_id = fixture.received_.back().mission_sequence_id;
|
|
|
|
// Bản cập nhật tới trong lúc chặng cũ đang chạy: nó phải nằm chờ, không được huỷ chặng đang đi.
|
|
ASSERT_TRUE(fixture.layer_.submitGoal(makeGoal()));
|
|
EXPECT_FALSE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ(0, fixture.cancel_calls_);
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_NE(running_id, fixture.received_.back().mission_sequence_id);
|
|
}
|
|
|
|
// ================================================================================================
|
|
// instantAction — đường vào thứ ba
|
|
//
|
|
// Thứ phải khoá: instantAction đi ĐÚNG cùng một hàng đợi với order. Nếu nó tìm được đường tắt nào
|
|
// xuống lõi thì mọi thứ lớp này bảo đảm — một mission id, một vòng đời, một đường cancel — mất
|
|
// hiệu lực đúng vào lúc fleet gửi lệnh khẩn.
|
|
// ================================================================================================
|
|
|
|
namespace
|
|
{
|
|
/// @brief Nguồn instantAction giả: nở một action thành @c legs chặng, giống compound action thật.
|
|
class FakeInstantAdapter : public MissionSourceAdapter
|
|
{
|
|
public:
|
|
explicit FakeInstantAdapter(std::size_t legs, SubmitMode mode = SubmitMode::kReplace)
|
|
: legs_(legs), mode_(mode)
|
|
{
|
|
}
|
|
|
|
bool configure(const std::string&, robot::NodeHandle&) override
|
|
{
|
|
return true;
|
|
}
|
|
|
|
std::string schema() const override
|
|
{
|
|
return mission_adapters::schema::kInstantAction;
|
|
}
|
|
|
|
bool validate(const MissionRequest& request, std::string& reason) const override
|
|
{
|
|
if (!request.actions || request.actions->empty())
|
|
{
|
|
reason = "request carries no action";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
ConversionResult convert(const MissionRequest& request) override
|
|
{
|
|
ConversionResult result;
|
|
result.mode = mode_;
|
|
|
|
// Mỗi action nở thành `legs_` chặng, nối tiếp nhau theo đúng thứ tự fleet gửi — cùng hình dạng
|
|
// với compound action thật.
|
|
for (const robot_protocol_msgs::Action& action : *request.actions)
|
|
{
|
|
for (std::size_t i = 0; i < legs_; ++i)
|
|
{
|
|
auto mission = std::make_shared<Mission>();
|
|
mission->has_goal = false;
|
|
mission_adapters::Action carried;
|
|
carried.action = action;
|
|
// Đánh số để test phân biệt được chặng nào của chuỗi đang chạy.
|
|
carried.action.actionId = action.actionId + "-" + std::to_string(i);
|
|
mission->actions.push_back(carried);
|
|
result.missions.push_back(mission);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
std::size_t legs_;
|
|
SubmitMode mode_;
|
|
};
|
|
|
|
robot_protocol_msgs::Action makeInstantAction(const std::string& type = "PickUp")
|
|
{
|
|
robot_protocol_msgs::Action action;
|
|
action.actionType = type;
|
|
action.actionId = type + "_1";
|
|
action.blockingType = "HARD";
|
|
return action;
|
|
}
|
|
|
|
/// @brief Layer chỉ có nguồn instantAction, cộng chỗ nhận yêu cầu như control thread thật.
|
|
class InstantFixture
|
|
{
|
|
public:
|
|
explicit InstantFixture(std::size_t legs, SubmitMode mode = SubmitMode::kReplace)
|
|
{
|
|
EXPECT_TRUE(layer_.registry().registerAdapter(std::make_shared<FakeInstantAdapter>(legs, mode)));
|
|
layer_.markActiveForTesting();
|
|
layer_.attach(bridge_);
|
|
|
|
bridge_.setRequestCallback([this](const NavigationRequest& request) {
|
|
received_.push_back(request);
|
|
});
|
|
bridge_.setCancelCallback([this]() { ++cancel_calls_; });
|
|
|
|
bridge_.start();
|
|
layer_.start();
|
|
}
|
|
|
|
~InstantFixture()
|
|
{
|
|
layer_.stop();
|
|
bridge_.stop();
|
|
}
|
|
|
|
bool pumpUntilRequest()
|
|
{
|
|
const auto deadline = std::chrono::steady_clock::now() + kTimeout;
|
|
while (std::chrono::steady_clock::now() < deadline)
|
|
{
|
|
if (bridge_.pumpPendingRequest())
|
|
{
|
|
return true;
|
|
}
|
|
std::this_thread::sleep_for(kPollStep);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void finishLastLeg(NavigationOutcome outcome)
|
|
{
|
|
ASSERT_FALSE(received_.empty());
|
|
bridge_.reportOutcome(received_.back().mission_sequence_id, outcome);
|
|
}
|
|
|
|
bool waitForStateOnLayer(MissionState expected)
|
|
{
|
|
const auto deadline = std::chrono::steady_clock::now() + kTimeout;
|
|
while (std::chrono::steady_clock::now() < deadline)
|
|
{
|
|
if (layer_.state() == expected)
|
|
{
|
|
return true;
|
|
}
|
|
std::this_thread::sleep_for(kPollStep);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
MissionLayer layer_;
|
|
MissionAdapterBridge bridge_;
|
|
std::vector<NavigationRequest> received_;
|
|
int cancel_calls_ = 0;
|
|
};
|
|
} // namespace
|
|
|
|
TEST(MissionLayerInstantAction, RejectsWhenNoSourceDeclaresTheSchema)
|
|
{
|
|
// Fixture thường chỉ có nguồn pose. Layer phải TỪ CHỐI, để `executeInstantAction` báo false và
|
|
// host báo FAILED ngay — nuốt rồi im lặng là fleet chờ tới hết giờ vì nó đang chặn mọi lệnh khác.
|
|
Fixture fixture(1);
|
|
|
|
EXPECT_FALSE(fixture.layer_.handles(mission_adapters::schema::kInstantAction));
|
|
EXPECT_FALSE(fixture.layer_.submitInstantActions({makeInstantAction()}));
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, RejectsBeforeStartAndAfterStop)
|
|
{
|
|
MissionLayer layer;
|
|
MissionAdapterBridge bridge;
|
|
ASSERT_TRUE(layer.registry().registerAdapter(std::make_shared<FakeInstantAdapter>(1)));
|
|
layer.markActiveForTesting();
|
|
layer.attach(bridge);
|
|
|
|
EXPECT_FALSE(layer.submitInstantActions({makeInstantAction()}));
|
|
|
|
bridge.start();
|
|
layer.start();
|
|
EXPECT_TRUE(layer.submitInstantActions({makeInstantAction()}));
|
|
|
|
layer.stop();
|
|
bridge.stop();
|
|
EXPECT_FALSE(layer.submitInstantActions({makeInstantAction()}));
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, AnExpandedActionReachesNavigationOneLegAtATime)
|
|
{
|
|
// `PickUp` nở thành ba chặng. Chúng phải đi tuần tự qua đúng hàng đợi của order, chứ không được
|
|
// bắn một lượt xuống lõi.
|
|
InstantFixture fixture(3);
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({makeInstantAction()}));
|
|
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
ASSERT_EQ(1u, fixture.received_.size());
|
|
ASSERT_EQ(1u, fixture.received_[0].actions.size());
|
|
EXPECT_EQ("PickUp_1-0", fixture.received_[0].actions[0].actionId);
|
|
EXPECT_FALSE(fixture.received_[0].has_goal) << "chặng chỉ-action không có quãng đường nào";
|
|
EXPECT_NE(0u, fixture.received_[0].mission_sequence_id) << "phải có mission id như mọi chặng khác";
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
ASSERT_EQ(2u, fixture.received_.size());
|
|
EXPECT_EQ("PickUp_1-1", fixture.received_[1].actions.at(0).actionId);
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
ASSERT_EQ(3u, fixture.received_.size());
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
EXPECT_TRUE(fixture.waitForStateOnLayer(MissionState::COMPLETED));
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, TheQueueStaysPendingUntilTheLastLegOfTheChain)
|
|
{
|
|
// Đây là thứ gánh cả cách báo trạng thái về fleet: host suy `actionStatus` từ `NavigationState`,
|
|
// mà `refreshFeedback` che SUCCEEDED giữa các chặng thành ACTIVE chừng nào hàng đợi còn việc.
|
|
// Nếu hàng đợi báo cạn sớm ở chặng đầu, fleet nhận FINISHED khi càng nâng chưa nhúc nhích.
|
|
InstantFixture fixture(3);
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({makeInstantAction()}));
|
|
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_TRUE(fixture.layer_.hasMission());
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_TRUE(fixture.layer_.hasMission()) << "mới xong chặng 1/3 — chưa được coi là xong";
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_TRUE(fixture.layer_.hasMission());
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
EXPECT_TRUE(fixture.waitForStateOnLayer(MissionState::COMPLETED));
|
|
EXPECT_FALSE(fixture.layer_.hasMission()) << "chỉ tới đây fleet mới được nghe FINISHED";
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, AFailedLegDropsTheRestOfTheChain)
|
|
{
|
|
// `clear_queue_on_failure`: dò hỏng thì KHÔNG được chạy tiếp bước nâng càng.
|
|
InstantFixture fixture(3);
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({makeInstantAction()}));
|
|
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
fixture.finishLastLeg(NavigationOutcome::kFailed);
|
|
|
|
EXPECT_TRUE(fixture.waitForStateOnLayer(MissionState::FAILED));
|
|
EXPECT_FALSE(fixture.pumpUntilRequest()) << "không được giao chặng nào nữa sau khi chặng đầu hỏng";
|
|
EXPECT_EQ(1u, fixture.received_.size());
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, HardBlockingPreemptsTheRunningLeg)
|
|
{
|
|
// `blockingType: HARD` -> kReplace. Chặng đang chạy phải bị bảo dừng, không chỉ bị quên đi.
|
|
InstantFixture fixture(1, SubmitMode::kReplace);
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({makeInstantAction("first")}));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
const auto running_id = fixture.received_.back().mission_sequence_id;
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({makeInstantAction("second")}));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
|
|
EXPECT_NE(running_id, fixture.received_.back().mission_sequence_id);
|
|
EXPECT_GT(fixture.cancel_calls_, 0) << "preempt phải DỪNG chặng cũ, không chỉ bỏ nó khỏi hàng đợi";
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, ANewMessageClearsTheLegsOfThePreviousOne)
|
|
{
|
|
// Fleet gửi message tiếp theo khi message trước còn đang chạy. Chuỗi cũ phải bị XOÁ, không phải
|
|
// xếp message mới sau nó: lệnh "instant" mà chạy sau vài chục giây thì không còn là instant, và
|
|
// chuỗi cũ vẫn kịp thao tác thiết bị dù đã bị lệnh mới thay thế.
|
|
InstantFixture fixture(3, SubmitMode::kReplace);
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({ makeInstantAction("old") }));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
const auto old_id = fixture.received_.back().mission_sequence_id;
|
|
EXPECT_EQ("old_1-0", fixture.received_.back().actions.at(0).actionId);
|
|
|
|
// Message mới tới khi chuỗi cũ mới chạy được 1/3 chặng.
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({ makeInstantAction("new") }));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
|
|
EXPECT_EQ("new_1-0", fixture.received_.back().actions.at(0).actionId)
|
|
<< "chặng kế tiếp phải thuộc message MỚI, không phải chặng 2/3 của message cũ";
|
|
EXPECT_NE(old_id, fixture.received_.back().mission_sequence_id);
|
|
EXPECT_GT(fixture.cancel_calls_, 0)
|
|
<< "chặng cũ phải được BẢO DỪNG, không chỉ bị quên khỏi hàng đợi";
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, TheRemainingLegsOfTheOldMessageNeverRun)
|
|
{
|
|
// Không chỉ chặng đang chạy: hai chặng CÒN LẠI của message cũ cũng phải biến mất. Nếu chúng còn
|
|
// trong hàng đợi, robot sẽ chạy nốt bước nâng càng của một lệnh đã bị thay thế.
|
|
InstantFixture fixture(3, SubmitMode::kReplace);
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({ makeInstantAction("old") }));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({ makeInstantAction("new") }));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
|
|
// Message mới có 3 chặng: chạy hết chúng rồi hàng đợi phải cạn hẳn.
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
|
|
EXPECT_TRUE(fixture.waitForStateOnLayer(MissionState::COMPLETED));
|
|
EXPECT_FALSE(fixture.layer_.hasMission());
|
|
|
|
for (const auto& request : fixture.received_)
|
|
{
|
|
if (&request == &fixture.received_.front())
|
|
continue; // chặng 1/3 của message cũ đã chạy trước khi bị thay — hợp lệ
|
|
EXPECT_EQ(request.actions.at(0).actionId.rfind("new_1-", 0), 0u)
|
|
<< "chặng của message cũ vẫn được giao sau khi nó đã bị thay thế: "
|
|
<< request.actions.at(0).actionId;
|
|
}
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, AMultiActionMessageQueuesAsOneBatch)
|
|
{
|
|
// Ba action, mỗi cái nở một chặng. Chúng phải nằm CÙNG một lô trong hàng đợi và chạy tuần tự —
|
|
// không cái nào preempt cái nào. Đây đúng là ca mà bản gửi-từng-cái làm hỏng.
|
|
InstantFixture fixture(1);
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({ makeInstantAction("MutedOn"),
|
|
makeInstantAction("PickUp"),
|
|
makeInstantAction("MutedOff") }));
|
|
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ("MutedOn_1-0", fixture.received_.at(0).actions.at(0).actionId);
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ("PickUp_1-0", fixture.received_.at(1).actions.at(0).actionId);
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ("MutedOff_1-0", fixture.received_.at(2).actions.at(0).actionId);
|
|
|
|
EXPECT_EQ(0, fixture.cancel_calls_) << "không action nào được preempt action đứng trước nó";
|
|
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
EXPECT_TRUE(fixture.waitForStateOnLayer(MissionState::COMPLETED));
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, AFailureStopsTheRestOfTheMessage)
|
|
{
|
|
// Yêu cầu vận hành: action nào fail thì DỪNG luôn và báo fail. Chặng thứ hai của message hỏng thì
|
|
// action thứ ba không được chạy — nếu chạy, robot thao tác thiết bị sau một bước đã hỏng.
|
|
InstantFixture fixture(1);
|
|
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({ makeInstantAction("first"),
|
|
makeInstantAction("second"),
|
|
makeInstantAction("third") }));
|
|
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
fixture.finishLastLeg(NavigationOutcome::kSucceeded);
|
|
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
EXPECT_EQ("second_1-0", fixture.received_.at(1).actions.at(0).actionId);
|
|
fixture.finishLastLeg(NavigationOutcome::kFailed);
|
|
|
|
EXPECT_TRUE(fixture.waitForStateOnLayer(MissionState::FAILED));
|
|
EXPECT_FALSE(fixture.pumpUntilRequest()) << "action thứ ba KHÔNG được chạy sau khi action hai hỏng";
|
|
EXPECT_EQ(2u, fixture.received_.size());
|
|
EXPECT_FALSE(fixture.layer_.hasMission()) << "hàng đợi phải sạch để host báo fail rồi kết thúc";
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, AnEmptyMessageIsRefused)
|
|
{
|
|
InstantFixture fixture(1);
|
|
EXPECT_FALSE(fixture.layer_.submitInstantActions({}));
|
|
}
|
|
|
|
TEST(MissionLayerInstantAction, CancelDropsAnInstantActionChainToo)
|
|
{
|
|
// Vòng đời của instantAction phải là vòng đời chung: `cancelOrder` tới giữa chuỗi `PickUp` phải
|
|
// dừng được nó. Đây là lý do instantAction bắt buộc đi qua lớp này thay vì xuống thẳng lõi.
|
|
InstantFixture fixture(3);
|
|
ASSERT_TRUE(fixture.layer_.submitInstantActions({makeInstantAction()}));
|
|
ASSERT_TRUE(fixture.pumpUntilRequest());
|
|
|
|
fixture.layer_.cancel();
|
|
|
|
EXPECT_TRUE(fixture.waitForStateOnLayer(MissionState::CANCELLED));
|
|
EXPECT_GT(fixture.cancel_calls_, 0);
|
|
EXPECT_FALSE(fixture.layer_.hasMission());
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|