Skip to content

Commit

Permalink
Merge pull request #94 from fledge-power/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
aklira authored Aug 27, 2024
2 parents 43a87ce + 37cb00f commit fc4d383
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
find $HOME/fledge/C/plugins/ -name '*.h' -exec sudo cp -prv '{}' '/usr/include/fledge/' ';'
find $HOME/fledge/C/services/ -name '*.h' -exec sudo cp -prv '{}' '/usr/include/fledge/' ';'
find $HOME/fledge/C/tasks/ -name '*.h' -exec sudo cp -prv '{}' '/usr/include/fledge/' ';'
find $HOME/fledge/C/thirdparty/Simple-Web-Server/ -name '*.hpp' -exec sudo cp -prv '{}' '/usr/include/fledge' ';'
sudo cp -prv $HOME/fledge/C/thirdparty/rapidjson/include/rapidjson/* /usr/include/fledge/rapidjson/
sudo mkdir -p /usr/lib/fledge/
sudo cp -prv /usr/local/fledge/lib/* /usr/lib/fledge/
Expand Down
33 changes: 28 additions & 5 deletions tests/server/basic_hnz_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <sstream>
#include <thread>

#include "hnz_client.h"

#include "basic_hnz_server.h"

void BasicHNZServer::startHNZServer() {
Expand Down Expand Up @@ -35,23 +37,43 @@ bool BasicHNZServer::joinStartThread() {
this_thread::sleep_for(chrono::milliseconds(100));
timeMs += 100;
}
// Join failed, try to connect a temporary client to fix it
if (!joinSuccess) {
printf("[HNZ Server][%d] Could not join m_t1, exiting\n", m_port); fflush(stdout);
// Do not delete m_t1 if it was not joined or it will crash immediately, let the rest of the test complete first
return false;
printf("[HNZ Server][%d] Could not join m_t1, attempting connection from temp client...\n", m_port); fflush(stdout);
// Make a temporary TCP client and connect it to the server
// because the server is likely stuck in socket accept() until that happen
HNZClient tmpClient;
tmpClient.connect_Server("127.0.0.1", m_port, 100000);

timeMs = 0;
// Wait up to 10s for m_t1 thread to join
while (timeMs < 10000) {
if (joinSuccess) {
break;
}
this_thread::sleep_for(chrono::milliseconds(100));
timeMs += 100;
}
// Join still failed, something went wrong
if (!joinSuccess) {
printf("[HNZ Server][%d] Could not join m_t1, exiting\n", m_port); fflush(stdout);
// Do not delete m_t1 if it was not joined or it will crash immediately, let the rest of the test complete first
// so that we get the exact fail location
return false;
}
}
delete m_t1;
m_t1 = nullptr;
}
return true;
}

void BasicHNZServer::stopHNZServer() {
bool BasicHNZServer::stopHNZServer() {
printf("[HNZ Server][%d] Server stopping...\n", m_port); fflush(stdout);
is_running = false;

// Ensure that m_t1 was joined no matter what
joinStartThread();
bool success = joinStartThread();

// Stop HNZ server before joining receiving_thread
if (server != nullptr) {
Expand All @@ -72,6 +94,7 @@ void BasicHNZServer::stopHNZServer() {
server = nullptr;
}
printf("[HNZ Server][%d] Server stopped!\n", m_port); fflush(stdout);
return success;
}

void BasicHNZServer::receiving_loop() {
Expand Down
2 changes: 1 addition & 1 deletion tests/server/basic_hnz_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BasicHNZServer {
};

void startHNZServer();
void stopHNZServer();
bool stopHNZServer();
bool joinStartThread();

bool waitForTCPConnection(int timeout_s);
Expand Down
18 changes: 9 additions & 9 deletions tests/test_hnz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@ TEST_F(HNZTest, ConnectionLossAndGIStatus) {
if(HasFatalFailure()) return;

// Disconnect server
server->stopHNZServer();
ASSERT_TRUE(server->stopHNZServer());
debug_print("[HNZ Server] Server disconnected");
waitUntil(southEventsReceived, 1, 1000);
// Check that ingestCallback had been called only one time
Expand Down Expand Up @@ -1943,13 +1943,13 @@ TEST_F(HNZTest, ConnectionLossTwoPath) {
if(HasFatalFailure()) return;

// Disconnect server 1
server->stopHNZServer();
ASSERT_TRUE(server->stopHNZServer());
debug_print("[HNZ Server] Server 1 disconnected");
this_thread::sleep_for(chrono::milliseconds(1000));
// Check that ingestCallback had not been called (second path is still connected)
ASSERT_EQ(southEventsReceived, 0);
// Disconnect server 2
server2->stopHNZServer();
ASSERT_TRUE(server2->stopHNZServer());
debug_print("[HNZ Server] Server 2 disconnected");
waitUntil(southEventsReceived, 1, 1000);
// Check that ingestCallback had been called only one time
Expand Down Expand Up @@ -1996,8 +1996,8 @@ TEST_F(HNZTest, ConnectionLossTwoPath) {
if(HasFatalFailure()) return;

// Disconnect server 1 and 2 simultaneously
server->stopHNZServer();
server2->stopHNZServer();
ASSERT_TRUE(server->stopHNZServer());
ASSERT_TRUE(server2->stopHNZServer());
debug_print("[HNZ Server] Server 1 & 2 disconnected");
waitUntil(southEventsReceived, 1, 1000);
// Check that ingestCallback had been called only one time
Expand Down Expand Up @@ -2107,7 +2107,7 @@ TEST_F(HNZTest, ReconfigureWhileConnectionActive) {

// Also stop the server as it is unable to reconnect on the fly
debug_print("[HNZ server] Request server stop...");
server->stopHNZServer();
ASSERT_TRUE(server->stopHNZServer());
debug_print("[HNZ south plugin] Waiting for outdated TI emission...");
this_thread::sleep_for(chrono::milliseconds(1000));
validateAllTIQualityUpdate(false, true);
Expand Down Expand Up @@ -2188,7 +2188,7 @@ TEST_F(HNZTest, ReconfigureBadConfig) {

// Also stop the server as it is unable to reconnect on the fly
debug_print("[HNZ server] Request server stop...");
server->stopHNZServer();
ASSERT_TRUE(server->stopHNZServer());
debug_print("[HNZ server] Request server start...");
server->startHNZServer();

Expand All @@ -2207,7 +2207,7 @@ TEST_F(HNZTest, ReconfigureBadConfig) {

// Restart the server
debug_print("[HNZ server] Request server stop 2...");
server->stopHNZServer();
ASSERT_TRUE(server->stopHNZServer());
debug_print("[HNZ server] Request server start 2...");
server->startHNZServer();

Expand Down Expand Up @@ -2253,7 +2253,7 @@ TEST_F(HNZTest, ReconfigureBadConfig) {

// Manually stop the server here or we may end up in a deadlock in the HNZServer
debug_print("[HNZ server] Request server stop 3...");
server->stopHNZServer();
ASSERT_TRUE(server->stopHNZServer());
}

TEST_F(HNZTest, UnknownMessage) {
Expand Down

0 comments on commit fc4d383

Please sign in to comment.