Skip to content

Commit

Permalink
tests: remove MakeTestRegistryUri (#101)
Browse files Browse the repository at this point in the history
Use the dynamically allocated registry port in all tests, which should make the test executions more reliable.
Previously, the registry port number was based on the process ID of the test runner and clamped to a narrow range.
  • Loading branch information
MariusBgm authored Aug 30, 2024
1 parent d100804 commit 1c9eb0e
Show file tree
Hide file tree
Showing 35 changed files with 192 additions and 285 deletions.
23 changes: 11 additions & 12 deletions SilKit/IntegrationTests/FTest_CanControllerThreadSafety.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <string>
#include <thread>

#include "GetTestPid.hpp"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -89,7 +87,7 @@ class FTest_CanControllerThreadSafety : public testing::Test
participant.canController = participant.participant->CreateCanController("CAN", "CAN");
participant.canController->Start();

while (runAsync)
while (_runAsync)
{
CanFrame frame{};
frame.canId = increasingCanId++;
Expand Down Expand Up @@ -131,7 +129,7 @@ class FTest_CanControllerThreadSafety : public testing::Test
}
};

while (runAsync)
while (_runAsync)
{
std::vector<SilKit::Services::HandlerId> handlerIds{};
for (int i = 0; i < numHandlersPerLoop; i++)
Expand Down Expand Up @@ -161,13 +159,14 @@ class FTest_CanControllerThreadSafety : public testing::Test
participant.participant.reset();
}

void RunRegistry(const std::string& registryUri)
auto RunRegistry() -> std::string
{
std::string registryUri;
try
{
registry =
SilKit::Vendor::Vector::CreateSilKitRegistry(SilKit::Config::ParticipantConfigurationFromString(""));
registry->StartListening(registryUri);
registryUri = registry->StartListening("silkit://localhost:0");
}
catch (const SilKit::ConfigurationError& error)
{
Expand All @@ -181,12 +180,13 @@ class FTest_CanControllerThreadSafety : public testing::Test
ss << "Something went wrong: " << error.what() << std::endl;
ShutdownAndFailTest(ss.str());
}
return registryUri;
}


void RunAsyncCanWriter(TestParticipant& participant, const std::string& registryUri)
{
runAsync = true;
_runAsync = true;

try
{
Expand All @@ -209,7 +209,7 @@ class FTest_CanControllerThreadSafety : public testing::Test

void RunAsyncCanReader(TestParticipant& participant, const std::string& registryUri)
{
runAsync = true;
_runAsync = true;

try
{
Expand Down Expand Up @@ -238,7 +238,7 @@ class FTest_CanControllerThreadSafety : public testing::Test

void StopAsyncParticipants()
{
runAsync = false;
_runAsync = false;
for (auto&& thread : asyncParticipantThreads)
{
thread.join();
Expand All @@ -255,19 +255,18 @@ class FTest_CanControllerThreadSafety : public testing::Test
protected:
std::unique_ptr<SilKit::Vendor::Vector::ISilKitRegistry> registry;
std::vector<std::thread> asyncParticipantThreads;
bool runAsync{true};
bool _runAsync{true};
};


TEST_F(FTest_CanControllerThreadSafety, DISABLED_add_remove_handler_during_reception)
{
numParticipants = 0;
auto registryUri = MakeTestRegistryUri();

TestParticipant canWriterParticipant{"CanWriterParticipant"};
TestParticipant canReaderParticipant{"CanReaderParticipant"};

RunRegistry(registryUri);
auto registryUri = RunRegistry();

RunAsyncCanWriter(canWriterParticipant, registryUri);
RunAsyncCanReader(canReaderParticipant, registryUri);
Expand Down
5 changes: 1 addition & 4 deletions SilKit/IntegrationTests/FTest_CanWithoutSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "GetTestPid.hpp"

namespace {

using namespace std::chrono_literals;
Expand All @@ -50,7 +48,6 @@ class FTest_CanWithoutSync : public testing::Test
protected:
FTest_CanWithoutSync()
{
_registryUri = MakeTestRegistryUri();
SetupTestData();
}

Expand Down Expand Up @@ -214,7 +211,7 @@ TEST_F(FTest_CanWithoutSync, can_communication_no_simulation_flow_vasio)
{
auto registry =
SilKit::Vendor::Vector::CreateSilKitRegistry(SilKit::Config::ParticipantConfigurationFromString(""));
registry->StartListening(_registryUri);
_registryUri = registry->StartListening("silkit://localhost:0");
ExecuteTest();
}

Expand Down
4 changes: 1 addition & 3 deletions SilKit/IntegrationTests/FTest_EthWithoutSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "GetTestPid.hpp"
#include "EthernetHelpers.hpp"

namespace {
Expand All @@ -52,7 +51,6 @@ class FTest_EthWithoutSync : public testing::Test
protected:
FTest_EthWithoutSync()
{
_registryUri = MakeTestRegistryUri();
SetupTestData();
}

Expand Down Expand Up @@ -203,7 +201,7 @@ TEST_F(FTest_EthWithoutSync, eth_communication_no_simulation_flow_vasio)
{
auto registry =
SilKit::Vendor::Vector::CreateSilKitRegistry(SilKit::Config::ParticipantConfigurationFromString(""));
registry->StartListening(_registryUri);
_registryUri = registry->StartListening("silkit://localhost:0");
ExecuteTest();
}

Expand Down
5 changes: 1 addition & 4 deletions SilKit/IntegrationTests/FTest_PubSubPerf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "SimTestHarness.hpp"

#include "GetTestPid.hpp"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -74,8 +72,7 @@ class FTest_PubSubPerf : public testing::Test
auto start = Now();

std::vector<std::string> syncParticipantNames = {"Publisher", "Subscriber"};
auto registryUri = MakeTestRegistryUri();
SilKit::Tests::SimTestHarness testHarness(syncParticipantNames, registryUri, true);
SilKit::Tests::SimTestHarness testHarness(syncParticipantNames, "silkit://localhost:0", true);

auto definePubSpec = [topicModePub, labelModePub](int i) {
std::string topic = "Topic";
Expand Down
5 changes: 1 addition & 4 deletions SilKit/IntegrationTests/FTest_ServiceDiscoveryPerf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "SimTestHarness.hpp"

#include "GetTestPid.hpp"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand All @@ -52,9 +50,8 @@ class FTest_ServiceDiscoveryPerf : public testing::Test
auto start = Now();

std::vector<std::string> syncParticipantNames = {"Publisher", "Subscriber", "Subscriber2"};
auto registryUri = MakeTestRegistryUri();

SilKit::Tests::SimTestHarness testHarness(syncParticipantNames, registryUri, true);
SilKit::Tests::SimTestHarness testHarness(syncParticipantNames, "silkit://localhost:0", true);
auto&& publisher = testHarness.GetParticipant("Publisher");

for (auto i = 0; i < numberOfServices; i++)
Expand Down
6 changes: 2 additions & 4 deletions SilKit/IntegrationTests/FTest_WallClockCoupling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "SimTestHarness.hpp"

#include "GetTestPid.hpp"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand All @@ -46,7 +44,7 @@ TEST(FTest_WallClockCoupling, test_wallclock_sync_simtask)
double animationFactor = 2.0;
std::string configWithAnimFactor = R"({"Experimental": {"TimeSynchronization": { "AnimationFactor": 2.0 } }})";

SimTestHarness testHarness({"P1", "P2", "P3"}, MakeTestRegistryUri(), true);
SimTestHarness testHarness({"P1", "P2", "P3"}, "silkit://localhost:0", true);

auto wc_P1{0ns};
auto wc_P2{0ns};
Expand Down Expand Up @@ -115,7 +113,7 @@ TEST(FTest_WallClockCoupling, test_wallclock_mixed_simtask)
double animationFactor = 2.0;
std::string configWithAnimFactor = R"({"Experimental": {"TimeSynchronization": { "AnimationFactor": 2.0 } }})";

SimTestHarness testHarness({"P1", "P2", "P3"}, MakeTestRegistryUri(), true);
SimTestHarness testHarness({"P1", "P2", "P3"}, "silkit://localhost:0", true);

auto wc_P1{0ns};
auto wc_P2{0ns};
Expand Down
10 changes: 0 additions & 10 deletions SilKit/IntegrationTests/GetTestPid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <unistd.h>
#endif

inline auto MakeTestRegistryUri()
{
std::stringstream ss;
int port = 8500;
int pid = getpid();
port += pid % 1000; // clamp to [8500, 9500)
// add a random offset to prevent two tests listening on the same port
ss << "silkit://localhost:" << port;
return ss.str();
}

inline auto MakeTestDashboardUri()
{
Expand Down
13 changes: 6 additions & 7 deletions SilKit/IntegrationTests/ITestFixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ namespace Tests {
class ITest_SimTestHarness : public testing::Test
{
protected: //CTor and operators
ITest_SimTestHarness()
: _registryUri{MakeTestRegistryUri()}
{
}

auto TestHarness() -> SimTestHarness&
{
Expand All @@ -52,11 +48,12 @@ class ITest_SimTestHarness : public testing::Test
{
// create test harness with deferred participant creation.
// Will only create the SIL Kit Registry and tell the SystemController the participantNames
_simTestHarness = std::make_unique<SimTestHarness>(participantNames, _registryUri, true);
_simTestHarness = std::make_unique<SimTestHarness>(participantNames, "silkit://localhost:0", true);
_registryUri = _simTestHarness->GetRegistryUri();
}

protected: // members
std::string _registryUri;
std::string _registryUri{};
std::unique_ptr<SimTestHarness> _simTestHarness;
};

Expand All @@ -78,8 +75,10 @@ class ITest_DashboardTestHarness : public ITest_SimTestHarness
{
// create test harness with deferred participant and controller creation.
// Will only create the SIL Kit Registry and tell the SystemController the participantNames
_simTestHarness = std::make_unique<SimTestHarness>(coordinatedParticipantNames, _registryUri, true, true,
_simTestHarness =
std::make_unique<SimTestHarness>(coordinatedParticipantNames, "silkit://localhost:0", true, true,
autonomousParticipantNames);
_registryUri = _simTestHarness->GetRegistryUri();
}

protected: // members
Expand Down
16 changes: 8 additions & 8 deletions SilKit/IntegrationTests/ITest_Abort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ TEST_F(ITest_Abort, test_Abort_Paused_Simulation_Sync)
AbortSystemController();

// Wait for coordinated to end
JoinParticipantThreads(participantThreads_Sync_Coordinated);
JoinParticipantThreads(_participantThreads_Sync_Coordinated);

// Expect shutdown state, which should happen after the abort handlers are called.
for (const auto& participant : monitorParticipants.front().CopyMonitoredParticipantStates())
Expand All @@ -114,7 +114,7 @@ TEST_F(ITest_Abort, test_Abort_Paused_Simulation_Sync)
}

monitorParticipants.front().i.stopRequested = true;
JoinParticipantThreads(participantThreads_Async_Autonomous);
JoinParticipantThreads(_participantThreads_Async_Autonomous);

StopRegistry();
}
Expand Down Expand Up @@ -174,7 +174,7 @@ TEST_F(ITest_Abort, test_Abort_Running_Simulation_Sync)
AbortSystemController();

// Wait for coordinated to end
JoinParticipantThreads(participantThreads_Sync_Coordinated);
JoinParticipantThreads(_participantThreads_Sync_Coordinated);

// Expect shutdown state, which should happen after the abort handlers are called.
for (const auto& participant : monitorParticipants.front().CopyMonitoredParticipantStates())
Expand All @@ -186,7 +186,7 @@ TEST_F(ITest_Abort, test_Abort_Running_Simulation_Sync)
}

monitorParticipants.front().i.stopRequested = true;
JoinParticipantThreads(participantThreads_Async_Autonomous);
JoinParticipantThreads(_participantThreads_Async_Autonomous);

StopRegistry();
}
Expand Down Expand Up @@ -260,7 +260,7 @@ TEST_F(ITest_Abort, test_Abort_Stopped_Simulation_Sync)
StopSystemController();

// Wait for coordinated to end
JoinParticipantThreads(participantThreads_Sync_Coordinated);
JoinParticipantThreads(_participantThreads_Sync_Coordinated);

// Expect shutdown state, which should happen after the abort handlers are called.
for (const auto& participant : monitorParticipants.front().CopyMonitoredParticipantStates())
Expand All @@ -272,7 +272,7 @@ TEST_F(ITest_Abort, test_Abort_Stopped_Simulation_Sync)
}

monitorParticipants.front().i.stopRequested = true;
JoinParticipantThreads(participantThreads_Async_Autonomous);
JoinParticipantThreads(_participantThreads_Async_Autonomous);

StopRegistry();
}
Expand Down Expand Up @@ -338,10 +338,10 @@ TEST_F(ITest_Abort, test_Abort_Communication_Ready_Simulation_Sync)
systemControllerParticipant.AwaitCommunicationInitialized();

// Wait for coordinated to end
JoinParticipantThreads(participantThreads_Sync_Coordinated);
JoinParticipantThreads(_participantThreads_Sync_Coordinated);

monitorParticipants.front().i.stopRequested = true;
JoinParticipantThreads(participantThreads_Async_Autonomous);
JoinParticipantThreads(_participantThreads_Async_Autonomous);

// Expect shutdown state, which should happen after the abort handlers are called.
for (const auto& participant : monitorParticipants.front().CopyMonitoredParticipantStates())
Expand Down
10 changes: 4 additions & 6 deletions SilKit/IntegrationTests/ITest_AsyncSimTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "SimTestHarness.hpp"

#include "GetTestPid.hpp"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand All @@ -54,7 +52,7 @@ TEST(ITest_AsyncSimTask, test_async_simtask_lockstep)
// Async may not start a new SimTask before calling CompleteSimulationtask to complete the current one.


SimTestHarness testHarness({"Sync", "Async"}, MakeTestRegistryUri());
SimTestHarness testHarness({"Sync", "Async"}, "silkit://localhost:0");

std::atomic<std::chrono::nanoseconds> syncTimeNs{0ns};
std::atomic<bool> done{false};
Expand Down Expand Up @@ -142,7 +140,7 @@ TEST(ITest_AsyncSimTask, test_async_simtask_nodeadlock)
// The async participant uses the CompleteSimTask calls to request next simulation step.
// The sync participant will be used to check the time progress

SimTestHarness testHarness({"Sync", "Async"}, MakeTestRegistryUri());
SimTestHarness testHarness({"Sync", "Async"}, "silkit://localhost:0");

auto syncTimeNs{0ns};

Expand Down Expand Up @@ -179,7 +177,7 @@ TEST(ITest_AsyncSimTask, test_async_simtask_different_periods)
// The async and sync participant use different time periods to validate the that a slower participant does
// not execute its simtask too often.

SimTestHarness testHarness({"Sync", "Async"}, MakeTestRegistryUri());
SimTestHarness testHarness({"Sync", "Async"}, "silkit://localhost:0");

auto syncTime{0ns};
auto asyncTime{0ns};
Expand Down Expand Up @@ -214,7 +212,7 @@ TEST(ITest_AsyncSimTask, test_async_simtask_multiple_completion_calls)
{
// Verify that multiple CompleteSimTask calls do not trigger malicious behaviour

SimTestHarness testHarness({"Sync", "Async"}, MakeTestRegistryUri());
SimTestHarness testHarness({"Sync", "Async"}, "silkit://localhost:0");

auto syncTime{0ns};
auto asyncTime{0ns};
Expand Down
Loading

0 comments on commit 1c9eb0e

Please sign in to comment.