[sim] WebSockets: don't override HAL_Main

Also clean up some other implementation aspects for cleaner shutdown and
reduce peak memory allocation.
This commit is contained in:
Peter Johnson
2020-09-04 10:57:05 -07:00
parent f1b1bdb121
commit f86a5f9b09
19 changed files with 780 additions and 851 deletions

View File

@@ -5,6 +5,8 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include <thread>
#include <hal/DriverStation.h>
#include <hal/HALBase.h>
#include <hal/Main.h>
@@ -16,89 +18,66 @@
#include "WebServerClientTest.h"
#include "gtest/gtest.h"
namespace uv = wpi::uv;
using namespace wpilibws;
extern "C" int HALSIM_InitExtension(
void); // from simulation/halsim_ws_server/src/main/native/cpp/main.cpp
const int POLLING_SPEED = 10; // 10 ms polling
static const int POLLING_SPEED = 10; // 10 ms polling
class WebServerIntegrationTest : public ::testing::Test {
public:
WebServerIntegrationTest() {
const int MAX_TEST_TIME = 1000; // 1 second
// Initialize HAL layer including webserver
HALSIM_InitExtension();
// Initialize server
m_server.Initialize();
// Create and initialize client
m_webserver_client =
std::shared_ptr<WebServerClientTest>((new WebServerClientTest()));
WebServerClientTest::SetInstance(m_webserver_client);
auto webserver = HALSimWeb::GetInstance();
auto loop = webserver->GetLoop();
m_webserver_client->Initialize(loop);
// Create failover timer to prevent running forever
m_failoverTimer = wpi::uv::Timer::Create(loop);
m_failoverTimer->timeout.connect([loop] { loop->Stop(); });
m_failoverTimer->Start(uv::Timer::Time(MAX_TEST_TIME));
m_server.runner.ExecSync([=](auto& loop) {
m_webserverClient = std::make_shared<WebServerClientTest>(loop);
m_webserverClient->Initialize();
});
}
~WebServerIntegrationTest() {
// Exit HAL layer which exits webserver
HAL_ExitMain();
HALSimWeb::Exit(nullptr);
bool IsConnectedClientWS() { return m_webserverClient->IsConnectedWS(); }
// Exit client
m_webserver_client->Exit();
WebServerClientTest::SetInstance(nullptr);
// Unreference timers from loop
m_failoverTimer->Unreference();
}
bool IsConnectedClientWS() { return m_webserver_client->IsConnectedWS(); }
void TerminateOnNextMessage(bool flag) {
m_webserver_client->SetTerminateFlag(flag);
}
private:
std::shared_ptr<WebServerClientTest> m_webserver_client;
std::shared_ptr<wpi::uv::Timer> m_failoverTimer;
protected:
std::shared_ptr<WebServerClientTest> m_webserverClient;
HALSimWSServer m_server;
};
TEST_F(WebServerIntegrationTest, DigitalOutput) {
// Create expected results
const bool EXPECTED_VALUE = false;
const int PIN = 0;
bool done = false;
// Attach timer to loop for test function
auto ws = HALSimWeb::GetInstance();
auto loop = ws->GetLoop();
auto timer = wpi::uv::Timer::Create(loop);
timer->timeout.connect([&] {
if (IsConnectedClientWS()) {
wpi::outs() << "***** Setting DIO value for pin " << PIN << " to "
<< (EXPECTED_VALUE ? "true" : "false") << "\n";
HALSIM_SetDIOValue(PIN, EXPECTED_VALUE);
TerminateOnNextMessage(true);
} else {
// Recheck in POLLING_SPEED ms
timer->Start(uv::Timer::Time(POLLING_SPEED));
}
m_server.runner.ExecSync([&](auto& loop) {
auto timer = wpi::uv::Timer::Create(loop);
timer->timeout.connect([&] {
if (done) {
return;
}
if (IsConnectedClientWS()) {
wpi::outs() << "***** Setting DIO value for pin " << PIN << " to "
<< (EXPECTED_VALUE ? "true" : "false") << "\n";
HALSIM_SetDIOValue(PIN, EXPECTED_VALUE);
done = true;
}
});
timer->Start(uv::Timer::Time(POLLING_SPEED),
uv::Timer::Time(POLLING_SPEED));
timer->Unreference();
});
timer->Start(uv::Timer::Time(POLLING_SPEED));
HAL_RunMain();
timer->Unreference();
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
// Get values from JSON message
std::string test_type;
std::string test_device;
bool test_value = true; // Default value from HAL initialization
try {
auto& msg = WebServerClientTest::GetInstance()->GetLastMessage();
auto& msg = m_webserverClient->GetLastMessage();
test_type = msg.at("type").get_ref<const std::string&>();
test_device = msg.at("device").get_ref<const std::string&>();
auto& data = msg.at("data");
@@ -120,28 +99,31 @@ TEST_F(WebServerIntegrationTest, DigitalInput) {
// Create expected results
const bool EXPECTED_VALUE = false;
const int PIN = 0;
bool done = false;
// Attach timer to loop for test function
auto ws = HALSimWeb::GetInstance();
auto loop = ws->GetLoop();
auto timer = wpi::uv::Timer::Create(loop);
timer->timeout.connect([&] {
if (IsConnectedClientWS()) {
wpi::json msg = {{"type", "DIO"},
{"device", std::to_string(PIN)},
{"data", {{"<>value", EXPECTED_VALUE}}}};
wpi::outs() << "***** Input JSON: " << msg.dump() << "\n";
WebServerClientTest::GetInstance()->SendMessage(msg);
loop->Stop();
} else {
// Recheck in POLLING_SPEED ms
timer->Start(uv::Timer::Time(POLLING_SPEED));
}
m_server.runner.ExecSync([&](auto& loop) {
auto timer = wpi::uv::Timer::Create(loop);
timer->timeout.connect([&] {
if (done) {
return;
}
if (IsConnectedClientWS()) {
wpi::json msg = {{"type", "DIO"},
{"device", std::to_string(PIN)},
{"data", {{"<>value", EXPECTED_VALUE}}}};
wpi::outs() << "***** Input JSON: " << msg.dump() << "\n";
m_webserverClient->SendMessage(msg);
done = true;
}
});
timer->Start(uv::Timer::Time(POLLING_SPEED),
uv::Timer::Time(POLLING_SPEED));
timer->Unreference();
});
timer->Start(uv::Timer::Time(POLLING_SPEED));
HAL_RunMain();
timer->Unreference();
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
// Compare results
bool test_value = HALSIM_GetDIOValue(PIN);
@@ -151,33 +133,32 @@ TEST_F(WebServerIntegrationTest, DigitalInput) {
TEST_F(WebServerIntegrationTest, DriverStation) {
// Create expected results
const bool EXPECTED_VALUE = true;
bool done = false;
// Attach timer to loop for test function
auto ws = HALSimWeb::GetInstance();
auto loop = ws->GetLoop();
auto timer = wpi::uv::Timer::Create(loop);
bool done = false;
timer->timeout.connect([&] {
if (done) {
loop->Stop();
} else {
// Recheck in POLLING_SPEED ms
timer->Start(uv::Timer::Time(POLLING_SPEED));
}
if (IsConnectedClientWS()) {
wpi::json msg = {
{"type", "DriverStation"},
{"device", ""},
{"data", {{">enabled", EXPECTED_VALUE}, {">new_data", true}}}};
wpi::outs() << "***** Input JSON: " << msg.dump() << "\n";
WebServerClientTest::GetInstance()->SendMessage(msg);
done = true;
}
m_server.runner.ExecSync([&](auto& loop) {
auto timer = wpi::uv::Timer::Create(loop);
timer->timeout.connect([&] {
if (done) {
return;
}
if (IsConnectedClientWS()) {
wpi::json msg = {
{"type", "DriverStation"},
{"device", ""},
{"data", {{">enabled", EXPECTED_VALUE}, {">new_data", true}}}};
wpi::outs() << "***** Input JSON: " << msg.dump() << "\n";
m_webserverClient->SendMessage(msg);
done = true;
}
});
timer->Start(uv::Timer::Time(POLLING_SPEED),
uv::Timer::Time(POLLING_SPEED));
timer->Unreference();
});
timer->Start(uv::Timer::Time(POLLING_SPEED));
HAL_RunMain();
timer->Unreference();
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
// Compare results
HAL_ControlWord cw;