76 lines
3.1 KiB
C++
76 lines
3.1 KiB
C++
|
|
// Local loopback only: verify automatic device discovery survives integration.
|
||
|
|
#include <QtTest>
|
||
|
|
#include <QApplication>
|
||
|
|
#include <atomic>
|
||
|
|
#include <grpcpp/grpcpp.h>
|
||
|
|
#include "cmvr/api/system_service.grpc.pb.h"
|
||
|
|
#include "../src/robotclient.h"
|
||
|
|
|
||
|
|
class ChangingSystem final : public cmvr::api::SystemService::Service
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
std::atomic<int> revision{0}, reads{0};
|
||
|
|
grpc::Status GetSystemStatus(grpc::ServerContext *,
|
||
|
|
const cmvr::api::GetSystemStatusCommand::Request *,
|
||
|
|
cmvr::api::GetSystemStatusCommand::Feedback *reply) override
|
||
|
|
{
|
||
|
|
++reads;
|
||
|
|
reply->mutable_header()->set_success(true);
|
||
|
|
auto *camera=reply->add_device_list();
|
||
|
|
camera->set_device_id(revision.load()==0 ? "camera_before" : "camera_after");
|
||
|
|
camera->set_device_type(cmvr::api::Camera);
|
||
|
|
camera->set_driver_type("AutoCamera");
|
||
|
|
return grpc::Status::OK;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
static bool waitFor(const std::function<bool()> &condition, int milliseconds=3000)
|
||
|
|
{
|
||
|
|
QElapsedTimer timer; timer.start();
|
||
|
|
while (!condition() && timer.elapsed()<milliseconds) QTest::qWait(10);
|
||
|
|
return condition();
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(int argc,char **argv)
|
||
|
|
{
|
||
|
|
QApplication app(argc,argv);
|
||
|
|
ChangingSystem service;
|
||
|
|
grpc::ServerBuilder builder;
|
||
|
|
int port=0;
|
||
|
|
builder.AddListeningPort("127.0.0.1:0",grpc::InsecureServerCredentials(),&port);
|
||
|
|
builder.RegisterService(&service);
|
||
|
|
auto server=builder.BuildAndStart();
|
||
|
|
if (!server || !port) return 2;
|
||
|
|
int result=0;
|
||
|
|
{
|
||
|
|
RobotClient client;
|
||
|
|
QSignalSpy connected(&client,&RobotClient::connectionChanged);
|
||
|
|
QString discovered;
|
||
|
|
QObject::connect(&client,&RobotClient::systemStatusReceived,&app,
|
||
|
|
[&](double,double,double,const QVariantList &devices) {
|
||
|
|
discovered=devices.value(0).toMap().value("deviceId").toString();
|
||
|
|
});
|
||
|
|
client.connectToServer("127.0.0.1",port,false);
|
||
|
|
if (!waitFor([&] { return !connected.isEmpty() && connected.last()[0].toBool(); })) {
|
||
|
|
std::fprintf(stderr,"FAIL: loopback connection\n"); result=1;
|
||
|
|
} else {
|
||
|
|
// No manual refresh and no arm telemetry: discovery must run by itself.
|
||
|
|
if (!waitFor([&] { return discovered=="camera_before"; })) {
|
||
|
|
std::fprintf(stderr,"FAIL: periodic device discovery is missing\n"); result=1;
|
||
|
|
} else {
|
||
|
|
service.revision.store(1);
|
||
|
|
if (!waitFor([&] { return discovered=="camera_after"; })) {
|
||
|
|
std::fprintf(stderr,"FAIL: camera replacement was not discovered\n"); result=1;
|
||
|
|
} else std::printf("PASS: automatic device discovery and replacement\n");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
client.disconnectFromServer();
|
||
|
|
waitFor([&] { return !connected.isEmpty() && !connected.last()[0].toBool(); });
|
||
|
|
const int before=service.reads.load(); QTest::qWait(1200);
|
||
|
|
if (service.reads.load()!=before) { std::fprintf(stderr,"FAIL: polling after disconnect\n"); result=1; }
|
||
|
|
else std::printf("PASS: device polling stops on disconnect\n");
|
||
|
|
}
|
||
|
|
server->Shutdown(); server->Wait();
|
||
|
|
return result;
|
||
|
|
}
|