Bring local station and route editing/deletion, verified map saves, confirmed switches, direct map transport and manual velocity ramps onto latest main. Preserve upstream non-AGV implementations, interface definitions and startup behavior. Validated with the existing qmake Release build, AGV map/UI/manual-motion tests, loopback upload/deletion/SEER transport tests and function-level retention audits.
429 lines
19 KiB
C++
429 lines
19 KiB
C++
#include "seeragvmapclient_fake.h"
|
|
#include "robotclient.h"
|
|
#include "cmvr/api/agv_service.grpc.pb.h"
|
|
#include "cmvr/api/configuration_service.grpc.pb.h"
|
|
|
|
#include <grpcpp/grpcpp.h>
|
|
|
|
#include <QApplication>
|
|
#include <QThread>
|
|
|
|
#include <map>
|
|
|
|
namespace {
|
|
const QString deviceId = QStringLiteral("fixture_seer");
|
|
|
|
void waitFor(const std::function<bool()> &predicate, const char *message, int timeoutMs = 15000)
|
|
{
|
|
QElapsedTimer elapsed;
|
|
elapsed.start();
|
|
while (!predicate() && elapsed.elapsed() < timeoutMs) {
|
|
QCoreApplication::processEvents();
|
|
QThread::msleep(2);
|
|
}
|
|
require(predicate(), message);
|
|
}
|
|
|
|
void pumpFor(int milliseconds)
|
|
{
|
|
QElapsedTimer elapsed;
|
|
elapsed.start();
|
|
while (elapsed.elapsed() < milliseconds) {
|
|
QCoreApplication::processEvents();
|
|
QThread::msleep(2);
|
|
}
|
|
}
|
|
|
|
// The existing configuration API is used only to discover three random
|
|
// loopback ports. It never serves or changes a real configuration file.
|
|
class FakeConfiguration final : public cmvr::api::ConfigurationService::Service {
|
|
public:
|
|
explicit FakeConfiguration(const FakeRobot &robot)
|
|
{
|
|
m_content = QStringLiteral("agv { agvs { id: \"fixture_seer\" seer_robokit_agv { "
|
|
"id: \"fixture_seer\" ip: \"127.0.0.1\" enable: true "
|
|
"port_status: %1 port_control: %2 port_config: %3 } } }")
|
|
.arg(robot.connection.statusPort).arg(robot.connection.controlPort)
|
|
.arg(robot.connection.configPort).toStdString();
|
|
}
|
|
|
|
grpc::Status ListConfigFiles(grpc::ServerContext *,
|
|
const cmvr::api::ListConfigFilesCommand::Request *,
|
|
cmvr::api::ListConfigFilesCommand::Feedback *response) override
|
|
{
|
|
++lists;
|
|
response->mutable_header()->set_success(true);
|
|
auto *file = response->add_files();
|
|
file->set_relative_path("test/agv.pbtxt");
|
|
file->set_message_type("cmvr.config.AGVRootConfig");
|
|
return grpc::Status::OK;
|
|
}
|
|
|
|
grpc::Status ReadConfigFile(grpc::ServerContext *,
|
|
const cmvr::api::ReadConfigFileCommand::Request *request,
|
|
cmvr::api::ReadConfigFileCommand::Feedback *response) override
|
|
{
|
|
++reads;
|
|
require(request->relative_path() == "test/agv.pbtxt", "bridge reads only discovered fake config path");
|
|
response->mutable_header()->set_success(true);
|
|
response->set_content(m_content);
|
|
return grpc::Status::OK;
|
|
}
|
|
|
|
std::atomic<int> lists{0};
|
|
std::atomic<int> reads{0};
|
|
private:
|
|
std::string m_content;
|
|
};
|
|
|
|
// Record legacy map calls so tests prove the enabled bridge bypasses their
|
|
// body packaging, while disabled direct mode still uses this existing API.
|
|
class FakeLegacyAgv final : public cmvr::api::AgvService::Service {
|
|
public:
|
|
grpc::Status setVelocity(grpc::ServerContext *context,
|
|
const cmvr::api::AgvSetVelocityCommand::Request *,
|
|
cmvr::api::AgvSetVelocityCommand::Feedback *response) override
|
|
{
|
|
++velocities;
|
|
while (holdVelocity.load() && !context->IsCancelled())
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
++velocitiesFinished;
|
|
response->mutable_header()->set_success(true);
|
|
return grpc::Status::OK;
|
|
}
|
|
|
|
grpc::Status downloadMap(grpc::ServerContext *,
|
|
const cmvr::api::AgvMapCommand::Request *,
|
|
cmvr::api::AgvMapCommand::Feedback *response) override
|
|
{
|
|
++reads;
|
|
response->mutable_header()->set_success(true);
|
|
response->set_content(completeMap.toStdString());
|
|
return grpc::Status::OK;
|
|
}
|
|
|
|
grpc::Status uploadMap(grpc::ServerContext *,
|
|
const cmvr::api::AgvMapCommand::Request *request,
|
|
cmvr::api::AgvMapCommand::Feedback *response) override
|
|
{
|
|
++uploads;
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
uploaded = request->content();
|
|
}
|
|
response->mutable_header()->set_success(true);
|
|
return grpc::Status::OK;
|
|
}
|
|
|
|
grpc::Status switchMap(grpc::ServerContext *,
|
|
const cmvr::api::AgvMapCommand::Request *,
|
|
cmvr::api::AgvMapCommand::Feedback *response) override
|
|
{
|
|
++switches;
|
|
response->mutable_header()->set_success(true);
|
|
return grpc::Status::OK;
|
|
}
|
|
|
|
std::atomic<int> reads{0};
|
|
std::atomic<int> uploads{0};
|
|
std::atomic<int> switches{0};
|
|
std::atomic<int> velocities{0};
|
|
std::atomic<int> velocitiesFinished{0};
|
|
std::atomic<bool> holdVelocity{false};
|
|
std::mutex mutex;
|
|
std::string uploaded;
|
|
};
|
|
|
|
class FakeServices final {
|
|
public:
|
|
explicit FakeServices(const FakeRobot &robot) : configuration(robot)
|
|
{
|
|
grpc::ServerBuilder builder;
|
|
builder.AddListeningPort("127.0.0.1:0", grpc::InsecureServerCredentials(), &port);
|
|
builder.RegisterService(&configuration);
|
|
builder.RegisterService(&legacy);
|
|
server = builder.BuildAndStart();
|
|
require(server && port > 0, "fake configuration and legacy services bind loopback ephemeral port");
|
|
}
|
|
~FakeServices()
|
|
{
|
|
server->Shutdown();
|
|
server->Wait();
|
|
}
|
|
|
|
FakeConfiguration configuration;
|
|
FakeLegacyAgv legacy;
|
|
int port = 0;
|
|
std::unique_ptr<grpc::Server> server;
|
|
};
|
|
|
|
struct ReadEvent {
|
|
bool success;
|
|
QString device;
|
|
QString map;
|
|
QString content;
|
|
QString detail;
|
|
};
|
|
struct SaveEvent {
|
|
bool success;
|
|
QString detail;
|
|
};
|
|
struct Capture {
|
|
explicit Capture(RobotClient &client)
|
|
{
|
|
QObject::connect(&client, &RobotClient::connectionChanged, &context,
|
|
[&] (bool value, int, const QString &) { connected = value; });
|
|
QObject::connect(&client, &RobotClient::agvMapDirectBusyChanged, &context,
|
|
[&] (bool value) { busy = value; busyEvents.push_back(value); });
|
|
QObject::connect(&client, &RobotClient::agvMapContentReceived, &context,
|
|
[&] (quint64 id, const QString &device, const QString &map, const QString &content,
|
|
bool success, const QString &detail) {
|
|
readEvents[id].push_back({success, device, map, content, detail});
|
|
});
|
|
QObject::connect(&client, &RobotClient::agvMapSaveFinished, &context,
|
|
[&] (quint64 id, const QString &, const QString &, bool success, const QString &detail) {
|
|
saveEvents[id].push_back({success, detail});
|
|
});
|
|
QObject::connect(&client, &RobotClient::commandFinished, &context,
|
|
[&] (const QString &command, bool success, const QString &detail) {
|
|
if (command == QStringLiteral("AGV SwitchMap"))
|
|
switchEvents.push_back({success, detail});
|
|
if (command == QStringLiteral("AGV SetVelocity") && !success)
|
|
++rejectedVelocities;
|
|
});
|
|
QObject::connect(&client, &RobotClient::agvDirectMapSwitchConfirmed, &context,
|
|
[&] (const QString &device, const QString &map, const QString &) {
|
|
confirmations.push_back({device, map});
|
|
});
|
|
}
|
|
|
|
bool connected = false;
|
|
bool busy = false;
|
|
int rejectedVelocities = 0;
|
|
std::vector<bool> busyEvents;
|
|
std::map<quint64, std::vector<ReadEvent>> readEvents;
|
|
std::map<quint64, std::vector<SaveEvent>> saveEvents;
|
|
std::vector<SaveEvent> switchEvents;
|
|
std::vector<std::pair<QString, QString>> confirmations;
|
|
QObject context;
|
|
};
|
|
|
|
void configure(RobotClient &client, Capture &capture, FakeServices &services, bool enabled = true, bool demo = false)
|
|
{
|
|
client.configureAgvMapDirect(enabled, QString(), QStringLiteral("127.0.0.1"), services.port);
|
|
client.connectToServer(QStringLiteral("127.0.0.1"), services.port, demo);
|
|
waitFor([&] { return capture.connected; }, "RobotClient connects to loopback or local Demo source");
|
|
}
|
|
|
|
void successfulChain()
|
|
{
|
|
FakeRobot robot;
|
|
robot.state.fragment.store(true);
|
|
FakeServices services(robot);
|
|
RobotClient client;
|
|
Capture capture(client);
|
|
configure(client, capture, services);
|
|
// Mimic the editor's read-latest -> upload callback and the page's
|
|
// switch-confirmed -> reload callback. Both must see busy already cleared.
|
|
QObject::connect(&client, &RobotClient::agvMapContentReceived,
|
|
[&] (quint64 id, const QString &, const QString &, const QString &content,
|
|
bool success, const QString &) {
|
|
if (id == 1 && success)
|
|
client.saveAgvMapContent(2, deviceId, mapName, content);
|
|
});
|
|
QObject::connect(&client, &RobotClient::agvMapSaveFinished,
|
|
[&] (quint64 id, const QString &, const QString &, bool success, const QString &) {
|
|
if (id == 2 && success)
|
|
client.requestAgvSwitchMap(deviceId, mapName);
|
|
});
|
|
QObject::connect(&client, &RobotClient::agvDirectMapSwitchConfirmed,
|
|
[&] (const QString &, const QString &, const QString &) {
|
|
client.requestAgvMapContent(3, deviceId, mapName);
|
|
});
|
|
client.requestAgvMapContent(1, deviceId, mapName);
|
|
waitFor([&] { return !capture.readEvents[3].empty(); }, "real bridge callback chain completes read/save/switch/reload");
|
|
require(capture.readEvents[1].size() == 1 && capture.readEvents[1][0].success,
|
|
"bridge reports initial complete map read once");
|
|
require(capture.saveEvents[2].size() == 1 && capture.saveEvents[2][0].success,
|
|
"read callback can start save without spurious busy rejection");
|
|
require(capture.readEvents[3].size() == 1 && capture.readEvents[3][0].success
|
|
&& capture.readEvents[3][0].content == completeMap, "confirmed switch callback reloads exact target map");
|
|
require(capture.switchEvents.size() == 1 && capture.switchEvents[0].success
|
|
&& capture.confirmations.size() == 1 && capture.confirmations[0].second == mapName,
|
|
"bridge reports confirmed switch exactly once");
|
|
require(!capture.busy && capture.busyEvents.size() == 8, "four operations each open and close busy state");
|
|
require(robot.state.uploads == 1 && robot.state.switches == 1 && robot.state.reads == 2,
|
|
"callback chain sends exactly one mutation per operation");
|
|
require(services.configuration.reads == 4 && services.configuration.lists == 4,
|
|
"bridge uses existing read-only config API to resolve selected device");
|
|
require(services.legacy.uploads == 0 && services.legacy.switches == 0 && services.legacy.reads == 0,
|
|
"enabled map bridge bypasses old map gRPC wrapper");
|
|
std::lock_guard<std::mutex> lock(robot.state.captureMutex);
|
|
require(robot.state.uploaded == completeMap.toUtf8(), "actual RobotClient bridge preserves complete raw 4010 payload");
|
|
std::cout << "PASS bridge read-latest/save/switch-confirmed/reload callback chain\n";
|
|
}
|
|
|
|
void definiteErrors()
|
|
{
|
|
{
|
|
FakeRobot robot(Fault::HeaderRejected);
|
|
FakeServices services(robot);
|
|
RobotClient client;
|
|
Capture capture(client);
|
|
configure(client, capture, services);
|
|
client.saveAgvMapContent(10, deviceId, mapName, completeMap);
|
|
waitFor([&] { return !capture.saveEvents[10].empty(); }, "bridge returns controller save rejection");
|
|
require(capture.saveEvents[10].size() == 1 && !capture.saveEvents[10][0].success
|
|
&& capture.saveEvents[10][0].detail.contains(QStringLiteral("40050")),
|
|
"save rejection never becomes success in queued signal delivery");
|
|
require(robot.state.uploads == 1 && services.legacy.uploads == 0, "save rejection has no hidden legacy fallback or resend");
|
|
}
|
|
{
|
|
FakeRobot robot(Fault::LoadRejected);
|
|
FakeServices services(robot);
|
|
RobotClient client;
|
|
Capture capture(client);
|
|
configure(client, capture, services);
|
|
client.requestAgvSwitchMap(deviceId, mapName);
|
|
waitFor([&] { return !capture.switchEvents.empty(); }, "bridge returns explicit failed map load after accepted ACK");
|
|
require(capture.switchEvents.size() == 1 && !capture.switchEvents[0].success
|
|
&& capture.confirmations.empty(), "accepted ACK plus explicit load failure never reports successful switch");
|
|
require(robot.state.switches == 1, "load failure is not resent by bridge");
|
|
}
|
|
std::cout << "PASS bridge preserves save rejection and explicit load failure\n";
|
|
}
|
|
|
|
void busyAndContextCancellation()
|
|
{
|
|
FakeRobot robot;
|
|
robot.state.readDelayMs.store(500);
|
|
FakeServices services(robot);
|
|
RobotClient client;
|
|
Capture capture(client);
|
|
configure(client, capture, services);
|
|
client.requestAgvMapContent(20, deviceId, mapName);
|
|
require(capture.busy, "starting async map read immediately marks bridge busy");
|
|
client.saveAgvMapContent(21, deviceId, mapName, completeMap);
|
|
require(capture.saveEvents[21].size() == 1 && !capture.saveEvents[21][0].success,
|
|
"second mutation while busy is immediately rejected");
|
|
waitFor([&] { return robot.state.reads == 1; }, "fake first read reached TCP channel");
|
|
client.requestAgvMapContent(22, QStringLiteral("different_device"), mapName);
|
|
waitFor([&] { return !capture.busy; }, "device context change cancels old direct worker promptly");
|
|
pumpFor(650);
|
|
require(capture.readEvents[20].size() == 1 && !capture.readEvents[20][0].success,
|
|
"old context receives cancellation failure but no late success");
|
|
require(capture.readEvents[22].size() == 1 && !capture.readEvents[22][0].success,
|
|
"new context does not queue another operation while prior worker drains");
|
|
require(robot.state.uploads == 0 && robot.state.reads == 1,
|
|
"busy rejection and context cancellation send no queued mutation or duplicate read");
|
|
robot.state.readDelayMs.store(0);
|
|
client.requestAgvMapContent(23, deviceId, mapName);
|
|
waitFor([&] { return !capture.readEvents[23].empty(); }, "bridge becomes usable again after cancelled context drains");
|
|
require(capture.readEvents[23].size() == 1 && capture.readEvents[23][0].success,
|
|
"fresh context returns requested complete map without old result leakage");
|
|
std::cout << "PASS bridge busy deduplication and stale context cancellation\n";
|
|
}
|
|
|
|
void legacyAndDemoPaths()
|
|
{
|
|
{
|
|
FakeRobot robot;
|
|
FakeServices services(robot);
|
|
RobotClient client;
|
|
Capture capture(client);
|
|
configure(client, capture, services, false);
|
|
client.requestAgvMapContent(30, deviceId, mapName);
|
|
waitFor([&] { return !capture.readEvents[30].empty(); }, "disabled direct mode uses legacy read");
|
|
client.saveAgvMapContent(31, deviceId, mapName, completeMap);
|
|
waitFor([&] { return !capture.saveEvents[31].empty(); }, "disabled direct mode uses legacy upload");
|
|
require(capture.readEvents[30][0].success && capture.saveEvents[31][0].success
|
|
&& services.legacy.reads == 1 && services.legacy.uploads == 1,
|
|
"existing gRPC map interface still works when direct mode disabled");
|
|
require(robot.state.reads == 0 && robot.state.uploads == 0 && services.configuration.lists == 0,
|
|
"disabled direct mode opens no TCP or config discovery requests");
|
|
}
|
|
{
|
|
FakeRobot robot;
|
|
FakeServices services(robot);
|
|
RobotClient client;
|
|
Capture capture(client);
|
|
configure(client, capture, services, true, true);
|
|
const QString demoDevice = QStringLiteral("demo_agv_1");
|
|
client.saveAgvMapContent(40, demoDevice, mapName, completeMap);
|
|
waitFor([&] { return !capture.saveEvents[40].empty(); }, "Demo map save remains on local worker");
|
|
client.requestAgvMapContent(41, demoDevice, mapName);
|
|
waitFor([&] { return !capture.readEvents[41].empty(); }, "Demo reload remains on local worker");
|
|
client.requestAgvSwitchMap(demoDevice, mapName);
|
|
waitFor([&] { return !capture.switchEvents.empty(); }, "Demo switch remains on local worker");
|
|
require(capture.saveEvents[40][0].success && capture.readEvents[41][0].success
|
|
&& capture.readEvents[41][0].content == completeMap && capture.switchEvents[0].success,
|
|
"Demo save/read/switch still completes with direct option enabled");
|
|
require(capture.busyEvents.empty() && capture.confirmations.empty()
|
|
&& robot.state.reads == 0 && robot.state.uploads == 0 && robot.state.switches == 0
|
|
&& services.configuration.lists == 0 && services.legacy.uploads == 0,
|
|
"Demo uses neither real map TCP nor config lookup nor legacy network writes");
|
|
}
|
|
std::cout << "PASS unchanged legacy gRPC and local Demo map paths\n";
|
|
}
|
|
|
|
void backendQueueBarrier()
|
|
{
|
|
{
|
|
FakeRobot robot;
|
|
FakeServices services(robot);
|
|
RobotClient client;
|
|
Capture capture(client);
|
|
configure(client, capture, services);
|
|
services.legacy.holdVelocity.store(true);
|
|
client.requestAgvVelocity(deviceId, 0.1, 0.0, 0.0);
|
|
waitFor([&] { return services.legacy.velocities == 1; }, "old motion RPC is blocked in original worker");
|
|
client.requestAgvVelocity(deviceId, 0.2, 0.0, 0.0);
|
|
client.saveAgvMapContent(50, deviceId, mapName, completeMap);
|
|
client.requestAgvVelocity(deviceId, 0.3, 0.0, 0.0);
|
|
pumpFor(300);
|
|
require(capture.busy && capture.rejectedVelocities == 1, "busy map operation blocks newly requested motion");
|
|
require(services.configuration.lists == 0 && robot.state.acquires == 0 && robot.state.uploads == 0,
|
|
"direct lookup, authority and map upload all wait for prior backend worker commands");
|
|
services.legacy.holdVelocity.store(false);
|
|
waitFor([&] { return !capture.saveEvents[50].empty(); }, "queued old motions drain before direct map operation");
|
|
require(capture.saveEvents[50].size() == 1 && capture.saveEvents[50][0].success,
|
|
"direct operation succeeds once backend barrier is passed");
|
|
require(services.legacy.velocities == 2 && services.legacy.velocitiesFinished == 2 && robot.state.uploads == 1,
|
|
"only preexisting motions and exactly one map mutation were dispatched");
|
|
}
|
|
{
|
|
FakeRobot robot;
|
|
FakeServices services(robot);
|
|
RobotClient client;
|
|
Capture capture(client);
|
|
configure(client, capture, services);
|
|
services.legacy.holdVelocity.store(true);
|
|
client.requestAgvVelocity(deviceId, 0.1, 0.0, 0.0);
|
|
waitFor([&] { return services.legacy.velocities == 1; }, "cancellation case has old worker RPC in flight");
|
|
client.saveAgvMapContent(51, deviceId, mapName, completeMap);
|
|
client.cancelAgvMapDirectRequests();
|
|
require(capture.saveEvents[51].size() == 1 && !capture.saveEvents[51][0].success,
|
|
"cancelling barrier-waiting operation immediately reports cancelled save");
|
|
services.legacy.holdVelocity.store(false);
|
|
waitFor([&] { return !capture.busy; }, "cancelled backend barrier drains boundedly");
|
|
pumpFor(100);
|
|
require(capture.saveEvents[51].size() == 1 && services.configuration.lists == 0
|
|
&& robot.state.acquires == 0 && robot.state.uploads == 0,
|
|
"cancelled barrier never runs direct lookup or mutation and produces no late result");
|
|
}
|
|
std::cout << "PASS backend queue barrier, new motion rejection and cancellation before barrier\n";
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
QApplication application(argc, argv);
|
|
successfulChain();
|
|
definiteErrors();
|
|
busyAndContextCancellation();
|
|
legacyAndDemoPaths();
|
|
backendQueueBarrier();
|
|
std::cout << "PASS RobotClient SEER map bridge integration: only ephemeral loopback services used\n";
|
|
return 0;
|
|
}
|