Skip to content

Commit

Permalink
test(PluginServices): state change subscription thread-safety
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisklein committed Nov 10, 2023
1 parent fbb6577 commit 961eca5
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion test/plugin_services/_control.cxx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/********************************************************************************
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* Copyright (C) 2017-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/

#include "Fixture.h"
#include <array>
#include <condition_variable>
#include <fairmq/Tools.h>
#include <memory>
#include <mutex>
#include <string>

namespace
{
Expand Down Expand Up @@ -142,4 +146,27 @@ TEST_F(PluginServices, ControlStateTransitionConversions)
EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::ErrorFound));
}

TEST_F(PluginServices, SubscriptionThreadSafety)
{
// obviously not a perfect test, but I could segfault fmq reliably with it (without the fix)

constexpr auto attempts = 1000;
constexpr auto subscribers = 5;

std::array<std::unique_ptr<std::thread>, subscribers> threads;
auto id = 0;
for (auto& thread : threads) {
thread = std::make_unique<std::thread>([&](){
auto const subscriber = fair::mq::tools::ToString("subscriber_", id);
for (auto i = 0; i < attempts; ++i) {
mServices.SubscribeToDeviceStateChange(subscriber, [](DeviceState){});
mServices.UnsubscribeFromDeviceStateChange(subscriber);
}
});
++id;
}

for (auto& thread : threads) { thread->join(); }
}

} /* namespace */

0 comments on commit 961eca5

Please sign in to comment.