refactor: use async tag to denote async calls or operations

This commit is contained in:
Stanislav Angelovic
2023-12-30 22:06:00 +01:00
parent f7afe3d1b1
commit 106500a555
16 changed files with 115 additions and 103 deletions
+12 -11
View File
@@ -92,16 +92,17 @@ TYPED_TEST(AConnection, CanInstallMatchRuleAsynchronously)
auto matchRule = "sender='" + BUS_NAME + "',path='" + OBJECT_PATH + "'";
std::atomic<bool> matchingMessageReceived{false};
std::atomic<bool> matchRuleInstalled{false};
auto slot = this->s_proxyConnection->addMatchAsync( matchRule
, [&](sdbus::Message msg)
{
if(msg.getPath() == OBJECT_PATH)
matchingMessageReceived = true;
}
, [&](sdbus::Message /*msg*/)
{
matchRuleInstalled = true;
} );
auto slot = this->s_proxyConnection->addMatch( matchRule
, [&](sdbus::Message msg)
{
if(msg.getPath() == OBJECT_PATH)
matchingMessageReceived = true;
}
, [&](sdbus::Message /*msg*/)
{
matchRuleInstalled = true;
}
, sdbus::async);
EXPECT_TRUE(waitUntil(matchRuleInstalled));
@@ -131,7 +132,7 @@ TYPED_TEST(AConnection, CanAddFloatingMatchRule)
auto matchRule = "sender='" + BUS_NAME + "',path='" + OBJECT_PATH + "'";
std::atomic<bool> matchingMessageReceived{false};
auto con = sdbus::createSystemBusConnection();
con->enterEventLoopAsync();
con->enterEventLoop(sdbus::async);
auto callback = [&](sdbus::Message msg)
{
if(msg.getPath() == OBJECT_PATH)
@@ -81,21 +81,22 @@ TYPED_TEST(SdbusTestObject, GetsPropertyAsynchronouslyViaPropertiesInterface)
{
std::promise<std::string> promise;
auto future = promise.get_future();
this->m_proxy->GetAsync(INTERFACE_NAME, "state", [&](const sdbus::Error* err, sdbus::Variant value)
auto getCallback = [&](const sdbus::Error* err, sdbus::Variant value)
{
if (err == nullptr)
promise.set_value(value.get<std::string>());
promise.set_value(value.get<std::string>());
else
promise.set_exception(std::make_exception_ptr(*err));
});
promise.set_exception(std::make_exception_ptr(*err));
};
this->m_proxy->Get(INTERFACE_NAME, "state", std::move(getCallback), sdbus::async);
ASSERT_THAT(future.get(), Eq(DEFAULT_STATE_VALUE));
}
TYPED_TEST(SdbusTestObject, GetsPropertyAsynchronouslyViaPropertiesInterfaceWithFuture)
{
auto future = this->m_proxy->GetAsync(INTERFACE_NAME, "state", sdbus::with_future);
auto future = this->m_proxy->Get(INTERFACE_NAME, "state", sdbus::async, sdbus::with_future);
ASSERT_THAT(future.get().template get<std::string>(), Eq(DEFAULT_STATE_VALUE));
}
@@ -114,14 +115,15 @@ TYPED_TEST(SdbusTestObject, SetsPropertyAsynchronouslyViaPropertiesInterface)
uint32_t newActionValue = 2346;
std::promise<void> promise;
auto future = promise.get_future();
this->m_proxy->SetAsync(INTERFACE_NAME, "action", sdbus::Variant{newActionValue}, [&](const sdbus::Error* err)
auto setCallback = [&](const sdbus::Error* err)
{
if (err == nullptr)
promise.set_value();
else
promise.set_exception(std::make_exception_ptr(*err));
});
};
this->m_proxy->Set(INTERFACE_NAME, "action", sdbus::Variant{newActionValue}, std::move(setCallback), sdbus::async);
ASSERT_NO_THROW(future.get());
ASSERT_THAT(this->m_proxy->action(), Eq(newActionValue));
@@ -131,7 +133,7 @@ TYPED_TEST(SdbusTestObject, SetsPropertyAsynchronouslyViaPropertiesInterfaceWith
{
uint32_t newActionValue = 2347;
auto future = this->m_proxy->SetAsync(INTERFACE_NAME, "action", sdbus::Variant{newActionValue}, sdbus::with_future);
auto future = this->m_proxy->Set(INTERFACE_NAME, "action", sdbus::Variant{newActionValue}, sdbus::async, sdbus::with_future);
ASSERT_NO_THROW(future.get());
ASSERT_THAT(this->m_proxy->action(), Eq(newActionValue));
@@ -151,14 +153,15 @@ TYPED_TEST(SdbusTestObject, GetsAllPropertiesAsynchronouslyViaPropertiesInterfac
{
std::promise<std::map<std::string, sdbus::Variant>> promise;
auto future = promise.get_future();
this->m_proxy->GetAllAsync(INTERFACE_NAME, [&](const sdbus::Error* err, std::map<std::string, sdbus::Variant> value)
auto getCallback = [&](const sdbus::Error* err, std::map<std::string, sdbus::Variant> value)
{
if (err == nullptr)
promise.set_value(std::move(value));
else
promise.set_exception(std::make_exception_ptr(*err));
});
};
this->m_proxy->GetAll(INTERFACE_NAME, std::move(getCallback), sdbus::async);
const auto properties = future.get();
ASSERT_THAT(properties, SizeIs(3));
@@ -169,7 +172,7 @@ TYPED_TEST(SdbusTestObject, GetsAllPropertiesAsynchronouslyViaPropertiesInterfac
TYPED_TEST(SdbusTestObject, GetsAllPropertiesAsynchronouslyViaPropertiesInterfaceWithFuture)
{
auto future = this->m_proxy->GetAllAsync(INTERFACE_NAME, sdbus::with_future);
auto future = this->m_proxy->GetAll(INTERFACE_NAME, sdbus::async, sdbus::with_future);
auto properties = future.get();
+4 -4
View File
@@ -102,8 +102,8 @@ public:
static void SetUpTestCase()
{
BaseTestFixture::SetUpTestCase();
s_proxyConnection->enterEventLoopAsync();
s_adaptorConnection->enterEventLoopAsync();
s_proxyConnection->enterEventLoop(async);
s_adaptorConnection->enterEventLoop(async);
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // Give time for the proxy connection to start listening to signals
}
@@ -237,11 +237,11 @@ private:
auto fd = accept4(sock, NULL, NULL, /*SOCK_NONBLOCK|*/SOCK_CLOEXEC);
m_adaptorConnection = sdbus::createServerBus(fd);
// This is necessary so that createDirectBusConnection() below does not block
m_adaptorConnection->enterEventLoopAsync();
m_adaptorConnection->enterEventLoop(async);
});
m_proxyConnection = sdbus::createDirectBusConnection("unix:path=" + DIRECT_CONNECTION_SOCKET_PATH);
m_proxyConnection->enterEventLoopAsync();
m_proxyConnection->enterEventLoop(async);
t.join();
}
+5 -5
View File
@@ -114,7 +114,7 @@ uint32_t TestProxy::doOperationWithTimeout(const std::chrono::microseconds &time
sdbus::PendingAsyncCall TestProxy::doOperationClientSideAsync(uint32_t param)
{
return getProxy().callMethodAsync("doOperation")
return getProxy().callMethod("doOperation", async)
.onInterface(sdbus::test::INTERFACE_NAME)
.withArguments(param)
.uponReplyInvoke([this](const sdbus::Error* error, uint32_t returnValue)
@@ -125,7 +125,7 @@ sdbus::PendingAsyncCall TestProxy::doOperationClientSideAsync(uint32_t param)
std::future<uint32_t> TestProxy::doOperationClientSideAsync(uint32_t param, with_future_t)
{
return getProxy().callMethodAsync("doOperation")
return getProxy().callMethod("doOperation", async)
.onInterface(sdbus::test::INTERFACE_NAME)
.withArguments(param)
.getResultAsFuture<uint32_t>();
@@ -141,7 +141,7 @@ std::future<MethodReply> TestProxy::doOperationClientSideAsyncOnBasicAPILevel(ui
void TestProxy::doErroneousOperationClientSideAsync()
{
getProxy().callMethodAsync("throwError")
getProxy().callMethod("throwError", async)
.onInterface(sdbus::test::INTERFACE_NAME)
.uponReplyInvoke([this](const sdbus::Error* error)
{
@@ -151,7 +151,7 @@ void TestProxy::doErroneousOperationClientSideAsync()
std::future<void> TestProxy::doErroneousOperationClientSideAsync(with_future_t)
{
return getProxy().callMethodAsync("throwError")
return getProxy().callMethod("throwError", async)
.onInterface(sdbus::test::INTERFACE_NAME)
.getResultAsFuture<>();
}
@@ -159,7 +159,7 @@ std::future<void> TestProxy::doErroneousOperationClientSideAsync(with_future_t)
void TestProxy::doOperationClientSideAsyncWithTimeout(const std::chrono::microseconds &timeout, uint32_t param)
{
using namespace std::chrono_literals;
getProxy().callMethodAsync("doOperation")
getProxy().callMethod("doOperation", async)
.onInterface(sdbus::test::INTERFACE_NAME)
.withTimeout(timeout)
.withArguments(param)
+2 -1
View File
@@ -44,7 +44,8 @@ protected:
public:
sdbus::PendingAsyncCall concatenate(const std::map<std::string, sdbus::Variant>& params)
{
return proxy_->callMethodAsync("concatenate").onInterface(INTERFACE_NAME).withArguments(params).uponReplyInvoke([this](const sdbus::Error* error, const std::string& result){ this->onConcatenateReply(result, error); });
using sdbus::async;
return proxy_->callMethod("concatenate", async).onInterface(INTERFACE_NAME).withArguments(params).uponReplyInvoke([this](const sdbus::Error* error, const std::string& result){ this->onConcatenateReply(result, error); });
}
private:
+1 -1
View File
@@ -481,7 +481,7 @@ int main(int argc, char *argv[])
// We could run the loop in a sync way, but we want it to run also when proxies are destroyed for better
// coverage of multi-threaded scenarios, so we run it async and use condition variable for exit notification
//con.enterEventLoop();
con.enterEventLoopAsync();
con.enterEventLoop(sdbus::async);
std::unique_lock<std::mutex> lock(clientThreadExitMutex);
clientThreadExitCond.wait(lock, [&]{return clientThreadExit;});