Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions source/commonlib/telemetry_busmessage_sender.c
Original file line number Diff line number Diff line change
Expand Up @@ -919,3 +919,18 @@ T2ERROR t2_event_d(const char* marker, int value)
pthread_mutex_unlock(&dMutex);
return retStatus ;
}
#ifdef GTEST_ENABLE
bool* test_get_isRbusEnabled_ptr(void)
{
return &isRbusEnabled;
}
bool* test_get_isT2Ready_ptr(void)
{
return &isT2Ready;
}
typedef T2ERROR (*doPopulateEventMarkerListFunc)(void);
doPopulateEventMarkerListFunc getDoPopulateEventMarkerListCallback(void)
{
return doPopulateEventMarkerList;
}
#endif
113 changes: 113 additions & 0 deletions source/test/commonlib/TelemetryBusMsgSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern "C" {
#include "telemetry_busmessage_sender.h"

T2ERROR getParamValue(const char* paramName, char **paramValue);
void *cacheEventToFile(void *arg);
}
#include "../mocks/rbusMock.h"
#include "../mocks/SystemMock.h"
Expand Down Expand Up @@ -64,6 +65,15 @@ class TelemetryBusmessageSenderTest : public ::testing::Test {
}
};

#ifdef GTEST_ENABLE
extern "C" {
typedef T2ERROR (*doPopulateEventMarkerListFunc)(void);
doPopulateEventMarkerListFunc getDoPopulateEventMarkerListCallback(void);
bool* test_get_isRbusEnabled_ptr(void);
bool* test_get_isT2Ready_ptr(void);
}
#endif

// Positive test: Init and Uninit
TEST_F(TelemetryBusmessageSenderTest, InitAndUninit) {
t2_init((char*)"test_component");
Expand Down Expand Up @@ -230,6 +240,75 @@ TEST_F(TelemetryBusmessageSenderTest, t2_event_s_iscachingenabled_false)
EXPECT_EQ(ret, T2ERROR_SUCCESS);
}

TEST_F(TelemetryBusmessageSenderTest, t2_event_d_iscachingenabled_false)
{
t2_init((char*)"sysint");
EXPECT_CALL(*g_systemMock, access(_,_))
.WillRepeatedly(Return(-1));
EXPECT_CALL(*g_rbusMock, rbus_getUint(_, _, _))
.Times(1)
.WillOnce([](rbusHandle_t handle, const char* name, uint32_t* value) {
*value = 0;
return RBUS_ERROR_BUS_ERROR;
});

int ret;
ret = t2_event_d("marker", 13);
EXPECT_EQ(ret, T2ERROR_SUCCESS);
}
Comment on lines +243 to +258
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test setup makes isCachingRequired() return true (because rbus_getUint returns an error), so report_or_cache_data() will spawn a background thread via pthread_create(cacheEventToFile, ...). The fixture TearDown() calls t2_uninit() which destroys mutexes and can race with that thread, making the test flaky/UB. Please avoid triggering the caching path in unit tests (e.g., return RBUS_ERROR_SUCCESS and set t2ReadyStatus to include T2_STATE_COMPONENT_READY), or stub out pthread_create/cacheEventToFile and join/disable the thread.

Copilot uses AI. Check for mistakes.

TEST_F(TelemetryBusmessageSenderTest, t2_event_f_iscachingenabled_false)
{
t2_init((char*)"sysint");
EXPECT_CALL(*g_systemMock, access(_,_))
.WillRepeatedly(Return(-1));
EXPECT_CALL(*g_rbusMock, rbus_getUint(_, _, _))
.Times(1)
.WillOnce([](rbusHandle_t handle, const char* name, uint32_t* value) {
*value = 0;
return RBUS_ERROR_BUS_ERROR;
});
int ret;
ret = t2_event_f("marker", 123.456);
EXPECT_EQ(ret, T2ERROR_SUCCESS);
}
Comment on lines +260 to +274
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test also forces the caching path (via rbus_getUint error), which spawns the async cacheEventToFile thread. That can race with t2_uninit() in TearDown() and cause nondeterministic failures. Consider adjusting mocks so isCachingRequired() returns false, or stub/join the caching thread for deterministic unit tests.

Copilot uses AI. Check for mistakes.

TEST_F(TelemetryBusmessageSenderTest, t2_event_d_iscachingenabled_true)
{
t2_init((char*)"sysint");

EXPECT_CALL(*g_systemMock, access(_,_))
.WillRepeatedly(Return(-1)); // Accept any number of calls

EXPECT_CALL(*g_rbusMock, rbus_getUint(_, _, _))
.Times(1)
.WillOnce([](rbusHandle_t handle, const char* name, uint32_t* value) {
*value = 0;
return RBUS_ERROR_SUCCESS; // <-- Simulate SUCCESS
});

int ret = t2_event_d("marker", 13);
EXPECT_EQ(ret, T2ERROR_SUCCESS);
}
Comment on lines +276 to +292
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here rbus_getUint returns success but sets *value = 0, which makes (t2ReadyStatus & T2_STATE_COMPONENT_READY) == 0 and therefore triggers caching + pthread_create(cacheEventToFile, ...). As written, this can race with t2_uninit() in TearDown() (mutex destruction while the caching thread runs). To keep the test deterministic, set *value to include T2_STATE_COMPONENT_READY (e.g., 1) and/or stub out the caching thread.

Copilot uses AI. Check for mistakes.
TEST_F(TelemetryBusmessageSenderTest, t2_event_d_iscachingenabled_true_1)
{
t2_init((char*)"sysinit");

EXPECT_CALL(*g_systemMock, access(_,_))
.WillRepeatedly(Return(-1)); // Accept any number of calls
EXPECT_CALL(*g_rbusMock, rbus_getUint(_, _, _))
.Times(1)
.WillOnce([](rbusHandle_t handle, const char* name, uint32_t* value) {
*value = 1;
return RBUS_ERROR_SUCCESS; // <-- Simulate SUCCESS
});
*test_get_isRbusEnabled_ptr() = false;
*test_get_isT2Ready_ptr() = true;
int ret = t2_event_d("marker", 13);

*test_get_isRbusEnabled_ptr() = true;
EXPECT_EQ(ret, T2ERROR_SUCCESS);
}
Comment on lines +293 to +311
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t2_event_d_iscachingenabled_true_1 directly calls test_get_isRbusEnabled_ptr() / test_get_isT2Ready_ptr(), but those helper declarations are wrapped in #ifdef GTEST_ENABLE while this test itself is not. If GTEST_ENABLE isn’t defined in a given test build, this will fail to compile. Please wrap this test in the same #ifdef GTEST_ENABLE block (or remove the dependency on those helpers).

Copilot uses AI. Check for mistakes.

TEST_F(TelemetryBusmessageSenderTest, getParameterValue_success)
{
Expand Down Expand Up @@ -270,6 +349,40 @@ TEST_F(TelemetryBusmessageSenderTest, getParameterValue_success_boolean)

EXPECT_EQ(T2ERROR_SUCCESS, getParamValue("Device.DeviceInfo.SerialNumber", &paramValue));
}

TEST_F(TelemetryBusmessageSenderTest, getParameterValue_failure_boolean)
{
char* paramValue = NULL;
t2_init((char*)"test_component");

EXPECT_CALL(*g_rbusMock, rbus_get(_, _, _))
.Times(1)
.WillOnce(Return(RBUS_ERROR_SUCCESS));
EXPECT_CALL(*g_rbusMock, rbusValue_GetType(_))
.Times(1)
.WillOnce(Return(RBUS_BOOLEAN));
EXPECT_CALL(*g_rbusMock, rbusValue_GetBoolean(_))
.Times(1)
.WillOnce(Return(false));
EXPECT_CALL(*g_rbusMock, rbusValue_Release(_))
.Times(1);

EXPECT_EQ(T2ERROR_SUCCESS, getParamValue("Device.DeviceInfo.SerialNumber", &paramValue));
}

#ifdef GTEST_ENABLE
TEST_F(TelemetryBusmessageSenderTest, doPopulateEventMarkerList_ReturnsEarlyIfRbusDisabled) {
t2_init((char*)"test_component");

*test_get_isRbusEnabled_ptr() = false;

auto cb = getDoPopulateEventMarkerListCallback();
T2ERROR ret = cb();
EXPECT_EQ(ret, T2ERROR_SUCCESS);
*test_get_isRbusEnabled_ptr() = true;
}
#endif

/*
TEST_F(TelemetryBusmessageSenderTest, filtered_event_send_1)
{
Expand Down
Loading