Compare commits
2 Commits
docs/flow-
...
4a743f26a4
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a743f26a4 | |||
| 2fe983d288 |
Binary file not shown.
|
Before Width: | Height: | Size: 405 KiB |
@@ -73,3 +73,18 @@ target_link_libraries(test_main_window
|
|||||||
)
|
)
|
||||||
|
|
||||||
add_test(NAME test_main_window COMMAND test_main_window)
|
add_test(NAME test_main_window COMMAND test_main_window)
|
||||||
|
|
||||||
|
add_executable(test_race_conditions
|
||||||
|
test_race_conditions.cxx
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(test_race_conditions
|
||||||
|
PRIVATE
|
||||||
|
core
|
||||||
|
gtest
|
||||||
|
gtest_main
|
||||||
|
Qt5::Core
|
||||||
|
Qt5::Test
|
||||||
|
)
|
||||||
|
|
||||||
|
add_test(NAME test_race_conditions COMMAND test_race_conditions)
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QSignalSpy>
|
#include <QSignalSpy>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "Consumer.hpp"
|
#include "Consumer.hpp"
|
||||||
#include "UnixIpcBridge.hpp"
|
#include "UnixIpcBridge.hpp"
|
||||||
@@ -118,3 +122,94 @@ TEST(ConsumerThreadTest, StopsCleanlyWhenNeverStarted)
|
|||||||
// stop() on a consumer that was never started must not crash
|
// stop() on a consumer that was never started must not crash
|
||||||
consumer.stop();
|
consumer.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Requirement 2: Consumer receiving corrupted data (non-numeric)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Helper: raw-connect to a UNIX socket and send arbitrary bytes.
|
||||||
|
static void send_raw_bytes(const std::string& path, const void* data,
|
||||||
|
size_t len)
|
||||||
|
{
|
||||||
|
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
ASSERT_GE(fd, 0);
|
||||||
|
|
||||||
|
struct sockaddr_un addr = {};
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
std::strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);
|
||||||
|
|
||||||
|
ASSERT_EQ(
|
||||||
|
connect(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)), 0);
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
::send(fd, data, len, 0);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ConsumerThreadTest, DropsCorruptedShortMessage)
|
||||||
|
{
|
||||||
|
const std::string sock = "/tmp/test_ct_corrupt_short.sock";
|
||||||
|
|
||||||
|
ConsumerThread consumer(sock);
|
||||||
|
QSignalSpy spy(&consumer, &ConsumerThread::valueReceived);
|
||||||
|
consumer.start();
|
||||||
|
|
||||||
|
// Send only 2 bytes instead of sizeof(int)==4 — corrupted / partial message
|
||||||
|
uint16_t garbage = 0xBEEF;
|
||||||
|
send_raw_bytes(sock, &garbage, sizeof(garbage));
|
||||||
|
|
||||||
|
// Give the consumer time to process (or not)
|
||||||
|
spy.wait(500);
|
||||||
|
consumer.stop();
|
||||||
|
|
||||||
|
// No signal should have been emitted
|
||||||
|
EXPECT_EQ(spy.count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ConsumerThreadTest, DropsEmptyConnection)
|
||||||
|
{
|
||||||
|
const std::string sock = "/tmp/test_ct_corrupt_empty.sock";
|
||||||
|
|
||||||
|
ConsumerThread consumer(sock);
|
||||||
|
QSignalSpy spy(&consumer, &ConsumerThread::valueReceived);
|
||||||
|
consumer.start();
|
||||||
|
|
||||||
|
// Connect and immediately close — zero bytes sent
|
||||||
|
send_raw_bytes(sock, nullptr, 0);
|
||||||
|
|
||||||
|
spy.wait(500);
|
||||||
|
consumer.stop();
|
||||||
|
|
||||||
|
EXPECT_EQ(spy.count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ConsumerThreadTest, SurvivesCorruptedThenReceivesValid)
|
||||||
|
{
|
||||||
|
const std::string sock = "/tmp/test_ct_corrupt_then_valid.sock";
|
||||||
|
|
||||||
|
ConsumerThread consumer(sock);
|
||||||
|
QSignalSpy spy(&consumer, &ConsumerThread::valueReceived);
|
||||||
|
consumer.start();
|
||||||
|
|
||||||
|
// First: send corrupted (1 byte)
|
||||||
|
uint8_t one_byte = 0xFF;
|
||||||
|
send_raw_bytes(sock, &one_byte, sizeof(one_byte));
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
|
|
||||||
|
// Then: send a valid int via the normal bridge
|
||||||
|
UnixIpcBridge bridge(sock);
|
||||||
|
bridge.send(777);
|
||||||
|
|
||||||
|
// Wait for the valid signal
|
||||||
|
for (int attempt = 0; spy.count() < 1 && attempt < 20; ++attempt)
|
||||||
|
{
|
||||||
|
spy.wait(100);
|
||||||
|
}
|
||||||
|
consumer.stop();
|
||||||
|
|
||||||
|
// The corrupted message must have been dropped, valid one received
|
||||||
|
ASSERT_EQ(spy.count(), 1);
|
||||||
|
EXPECT_EQ(spy.at(0).at(0).toInt(), 777);
|
||||||
|
}
|
||||||
|
|||||||
83
tests/test_race_conditions.cxx
Normal file
83
tests/test_race_conditions.cxx
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
// test_race_conditions.cxx
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
// Author: Unai Blazquez <unaibg2000@gmail.com>
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <atomic>
|
||||||
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "Consumer.hpp"
|
||||||
|
#include "UnixIpcBridge.hpp"
|
||||||
|
|
||||||
|
static int argc_ = 0;
|
||||||
|
static QCoreApplication app_(argc_, nullptr);
|
||||||
|
|
||||||
|
|
||||||
|
TEST(RaceConditionTest, RepeatedStartStopWhileProducerSends)
|
||||||
|
{
|
||||||
|
const std::string sock = "/tmp/test_race.sock";
|
||||||
|
constexpr int kCycles = 20;
|
||||||
|
|
||||||
|
// Watchdog: if the test takes longer than 15s, declare deadlock.
|
||||||
|
std::atomic<bool> test_done{false};
|
||||||
|
std::thread watchdog([&test_done]() {
|
||||||
|
for (int i = 0; i < 150 && !test_done.load(); ++i)
|
||||||
|
{
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
}
|
||||||
|
if (!test_done.load())
|
||||||
|
{
|
||||||
|
std::cerr
|
||||||
|
<< "DEADLOCK DETECTED: RepeatedStartStopWhileProducerSends timed out"
|
||||||
|
<< std::endl;
|
||||||
|
std::abort();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Producer thread: keeps trying to send values. connect() failures
|
||||||
|
// (consumer mid-restart) are expected and silently ignored.
|
||||||
|
|
||||||
|
std::atomic<bool> producer_running{true};
|
||||||
|
std::thread producer([&]() {
|
||||||
|
while (producer_running.load())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
UnixIpcBridge bridge(sock);
|
||||||
|
bridge.send(42);
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error&)
|
||||||
|
{
|
||||||
|
// Expected: consumer socket not ready or just torn down.
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Main thread: repeatedly start/stop the consumer.
|
||||||
|
for (int i = 0; i < kCycles; ++i)
|
||||||
|
{
|
||||||
|
ConsumerThread consumer(sock);
|
||||||
|
consumer.start();
|
||||||
|
|
||||||
|
// Let it run briefly so the producer can connect during some cycles.
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(10 + (i % 5) * 5));
|
||||||
|
|
||||||
|
// stop() must return without deadlock every single time.
|
||||||
|
consumer.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
producer_running.store(false);
|
||||||
|
producer.join();
|
||||||
|
|
||||||
|
test_done.store(true);
|
||||||
|
watchdog.join();
|
||||||
|
|
||||||
|
// If we reach here, no deadlock across kCycles start/stop cycles.
|
||||||
|
SUCCEED();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user