sync code

This commit is contained in:
Ned Wright
2026-01-03 18:59:01 +00:00
parent c1058db57d
commit 2105628f05
188 changed files with 8272 additions and 2723 deletions

View File

@@ -72,6 +72,11 @@ MessagingComp::init()
auto i_time_get = Singleton::Consume<I_TimeGet>::by<Messaging>();
auto cache_timeout = getConfigurationWithDefault<int>(40, "message", "Cache timeout");
fog_get_requests_cache.startExpiration(chrono::seconds(cache_timeout), i_mainloop, i_time_get);
proxy_addr = getConfigurationWithDefault<string>(
getProfileAgentSettingWithDefault<string>("", "proxy.address"),
"message",
"Proxy Address"
);
should_buffer_failed_messages = getConfigurationWithDefault<bool>(
getProfileAgentSettingWithDefault<bool>(true, "eventBuffer.bufferFailedRequests"),
@@ -125,7 +130,7 @@ MessagingComp::sendMessage(
dbgWarning(D_MESSAGING) << "Failed to get connection. Error: " << maybe_conn.getErr();
return genError<HTTPResponse>(HTTPStatusCode::HTTP_UNKNOWN, maybe_conn.getErr());
}
Connection conn = maybe_conn.unpack();
if (message_metadata.shouldSuspend() && conn.isSuspended()) {
return suspendMessage(body, method, uri, category, message_metadata);
@@ -133,12 +138,11 @@ MessagingComp::sendMessage(
bool is_to_fog = isMessageToFog(message_metadata);
auto metadata = message_metadata;
if (is_to_fog) {
if (method == HTTPMethod::GET && fog_get_requests_cache.doesKeyExists(uri)) {
HTTPResponse res = fog_get_requests_cache.getEntry(uri);
dbgTrace(D_MESSAGING) << "Response returned from Fog cache. res body: " << res.getBody();
return fog_get_requests_cache.getEntry(uri);
}
@@ -197,7 +201,6 @@ MessagingComp::sendSyncMessage(
)
{
Maybe<HTTPResponse, HTTPResponse> is_msg_send = sendMessage(method, uri, body, category, message_metadata);
if (is_msg_send.ok()) return *is_msg_send;
if (should_buffer_failed_messages && message_metadata.shouldBufferMessage()) {
@@ -412,3 +415,10 @@ MessagingComp::suspendMessage(
HTTPStatusCode::HTTP_SUSPEND, "The connection is suspended due to consecutive message sending errors."
);
}
void
MessagingComp::clearConnections()
{
dbgTrace(D_MESSAGING) << "Clearing all connections (called from AgentDetails)";
i_conn->clearConnections();
}

View File

@@ -62,6 +62,7 @@ public:
{
EXPECT_CALL(mock_agent_details, getFogDomain()).WillRepeatedly(Return(string(fog_addr)));
EXPECT_CALL(mock_agent_details, getFogPort()).WillRepeatedly(Return(fog_port));
EXPECT_CALL(mock_agent_details, getProxy()).WillRepeatedly(Return(string("")));
EXPECT_CALL(mock_agent_details, getOpenSSLDir()).WillRepeatedly(Return(string("/usr/lib/ssl/certs/")));
EXPECT_CALL(mock_agent_details, getAccessToken()).WillRepeatedly(Return(string("accesstoken")));
EXPECT_CALL(mock_agent_details, readAgentDetails()).WillRepeatedly(Return(true));
@@ -262,6 +263,28 @@ operator==(const MessageMetadata &one, const MessageMetadata &two)
one.isDualAuth() == two.isDualAuth();
}
TEST_F(TestMessagingComp, testClearConnections)
{
setAgentDetails();
MessageCategory category = MessageCategory::GENERIC;
MessageConnectionKey conn_key(fog_addr, fog_port, category);
MessageMetadata metadata(fog_addr, fog_port, true);
MessageProxySettings proxy_settings("7.7.7.7", "cred", 8080);
metadata.setProxySettings(proxy_settings);
Connection conn(conn_key, metadata);
EXPECT_CALL(mock_messaging_connection, establishConnection(metadata, category)).WillOnce(Return(conn));
EXPECT_TRUE(messaging_comp.setFogConnection(category));
EXPECT_CALL(mock_messaging_connection, clearConnections()).Times(1);
messaging_comp.clearConnections();
EXPECT_CALL(mock_messaging_connection, establishConnection(metadata, category)).WillOnce(Return(conn));
EXPECT_TRUE(messaging_comp.setFogConnection(category));
}
TEST_F(TestMessagingComp, testSetFogConnection)
{
setAgentDetails();