optimal
This commit is contained in:
@@ -359,6 +359,386 @@ TEST(MissionLayerTest, AppendModeDoesNotPreemptTheRunningLeg)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user