Retry at most 3 times if aul_app_get_appid_bypid() fails 22/264622/4
authorJi-hoon Lee <dalton.lee@samsung.com>
Mon, 27 Sep 2021 09:15:11 +0000 (18:15 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Mon, 27 Sep 2021 09:32:11 +0000 (18:32 +0900)
If AMD and Launchpad is busy processing other tasks,
it might not respond to aul_app_get_appid_bypid() request
in its timeout limit, which is 5 seconds.
For this reason, after discussing with AppFW team,
we decided to add a retry logic at most 3 times if
the given function unexpectedly fails.

[==========] Running 4 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 4 tests from DefaultFixture
[ RUN      ] DefaultFixture.HardcodedAppIDReturnedForHardcodedPID
[       OK ] DefaultFixture.HardcodedAppIDReturnedForHardcodedPID (0 ms)
[ RUN      ] DefaultFixture.AppIDNotFoundForNonExistingPID
[       OK ] DefaultFixture.AppIDNotFoundForNonExistingPID (0 ms)
[ RUN      ] DefaultFixture.RetriesForAtLeastThreeTimes
[       OK ] DefaultFixture.RetriesForAtLeastThreeTimes (0 ms)
[ RUN      ] DefaultFixture.RetriesNoMoreThanThreeTimes
[       OK ] DefaultFixture.RetriesNoMoreThanThreeTimes (0 ms)
[----------] 4 tests from DefaultFixture (0 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 1 test suite ran. (0 ms total)
[  PASSED  ] 4 tests.

Change-Id: I49d42e6d664cf1bead1a00fb088ac0c8c2933514

src/application_manager_aul.cpp
tests/utc/CMakeLists.txt
tests/utc/application-manager/CMakeLists.txt [new file with mode: 0644]
tests/utc/application-manager/test_main.cpp [new file with mode: 0644]
tests/utc/application-manager/ttpo.xml [new file with mode: 0644]

index f1fd82a..782448b 100644 (file)
@@ -100,13 +100,21 @@ bool CApplicationManagerAul::launch_app_async(const std::string& appid, bool bac
 
 boost::optional<std::string> CApplicationManagerAul::get_appid_by_pid(pid_t pid)
 {
+       const int max_retry_num = 3;
        boost::optional<std::string> ret;
 
+       bool succeeded = false;
+       int retry_num = 0;
        char appid[MAX_APPID_LEN] = {'\0', };
-       if (AUL_R_OK == aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
-               appid[MAX_APPID_LEN - 1] = '\0';
-               ret = std::string{appid};
-       }
+
+       do {
+               if (AUL_R_OK == aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
+                       appid[MAX_APPID_LEN - 1] = '\0';
+                       ret = std::string{appid};
+                       succeeded = true;
+               }
+               retry_num++;
+       } while (!succeeded && retry_num < max_retry_num);
 
        return ret;
 }
index 4c20d2c..7d0ab00 100644 (file)
@@ -8,3 +8,4 @@ ADD_SUBDIRECTORY(preference-manager-vconf)
 ADD_SUBDIRECTORY(package-update-monitor)
 ADD_SUBDIRECTORY(wakeup-policy-default)
 ADD_SUBDIRECTORY(wakeup-manager)
+ADD_SUBDIRECTORY(application-manager)
diff --git a/tests/utc/application-manager/CMakeLists.txt b/tests/utc/application-manager/CMakeLists.txt
new file mode 100644 (file)
index 0000000..eb1fe31
--- /dev/null
@@ -0,0 +1,78 @@
+# THIS FILE WAS GENERATED BY THE TIZEN TDD-PROJECT OUTLINER.
+# IT IS NOT RECOMMENDED TO MODIFY THIS FILE SINCE THE
+# MODIFICATION CAN BE OVERWRITTEN BY THE GENERATION TOOL.
+
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(ttpo-project CXX C)
+
+LINK_DIRECTORIES(${CMAKE_BINARY_DIR})
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
+
+SET(TTPO_SRCS
+    test_main.cpp
+    ../../../src/application_manager_aul.cpp
+)
+
+SET(TTPO_PKGS
+    capi-appfw-application
+    glib-2.0
+    capi-appfw-service-application
+)
+
+SET(TTPO_INCLUDES
+    ../../../inc//
+)
+
+SET(TTPO_CFLAGS
+)
+
+SET(TTPO_WRAPPERS
+    "-Wl,\
+--wrap=aul_app_get_appid_bypid"
+)
+
+SET(TTPO_LDFLAGS
+)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -std=c++14")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -O0")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TTPO_CFLAGS}")
+SET(EXTRA_LDFLAGS "${TTPO_LDFLAGS}")
+
+# Find Packages
+INCLUDE(FindPkgConfig)
+pkg_check_modules(pkgs REQUIRED
+    ${TTPO_PKGS}
+)
+
+INCLUDE_DIRECTORIES(${TTPO_INCLUDES})
+
+FOREACH(flag ${pkgs_CFLAGS})
+    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
+ENDFOREACH(flag)
+
+FIND_PACKAGE(GTest REQUIRED)
+INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS})
+LINK_DIRECTORIES(${GTEST_LIBRARY_DIRS})
+
+SET(TTPO_EXECUTABLE test-application-manager)
+ADD_EXECUTABLE(
+    ${TTPO_EXECUTABLE}
+    ${TTPO_SRCS}
+)
+
+TARGET_LINK_LIBRARIES(${TTPO_EXECUTABLE} ${pkgs_LDFLAGS})
+if(NOT "${EXTRA_LDFLAGS}" STREQUAL " ")
+    TARGET_LINK_LIBRARIES(${TTPO_EXECUTABLE} ${EXTRA_LDFLAGS})
+endif()
+TARGET_LINK_LIBRARIES(${TTPO_EXECUTABLE} ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
+
+SET_TARGET_PROPERTIES(${TTPO_EXECUTABLE} PROPERTIES
+       COMPILE_FLAGS "-fPIE"
+       LINK_FLAGS "${TTPO_WRAPPERS}")
+ADD_TEST(NAME ${TTPO_EXECUTABLE} COMMAND ${TTPO_EXECUTABLE})
+
+# REPLACE THE TARGET INSTALL DESTINATION AS NEEDED (default : bin)
+INSTALL(TARGETS ${TTPO_EXECUTABLE} DESTINATION bin)
\ No newline at end of file
diff --git a/tests/utc/application-manager/test_main.cpp b/tests/utc/application-manager/test_main.cpp
new file mode 100644 (file)
index 0000000..1fdcb87
--- /dev/null
@@ -0,0 +1,108 @@
+#include <iostream>
+#include <gtest/gtest.h>
+
+#include <aul.h>
+
+#include "application_manager_aul.h"
+
+const char *gHardcodedAppID = "ApplicationA";
+const int gHardcodedPID = 1234;
+int gIntentionalFailureNum = 0;
+
+extern "C" {
+/* Add wrapper functions here with __wrap_ and __real prefix
+Example :
+
+int __real_vconf_get_int(const char *in_key, int *intval);
+int __wrap_vconf_get_int(const char *in_key, int *intval)
+{
+    if (strcmp(in_key, "TestKey") == 0)
+    return 0;
+
+    return __real_vconf_get_int(in_key, intval);
+}
+*/
+int __wrap_aul_app_get_appid_bypid(int pid, char *appid, int len)
+{
+    int ret = AUL_R_OK;
+    if (gIntentionalFailureNum == 0) {
+        if (pid == gHardcodedPID) {
+            int length = strlen(gHardcodedAppID);
+            if (len < length) length = len;
+            strncpy(appid, gHardcodedAppID, length);
+        } else {
+            ret = AUL_R_EINVAL;
+        }
+    } else {
+        gIntentionalFailureNum--;
+        ret = AUL_R_ETIMEOUT;
+    }
+    return ret;
+}
+
+}
+
+class DefaultFixture : public testing::Test
+{
+public:
+    DefaultFixture() {
+    }
+    virtual ~DefaultFixture() {
+    }
+    void SetUp() override {
+    }
+    void TearDown() override {
+    }
+
+    CApplicationManagerAul mApplicationMgr;
+};
+
+TEST_F(DefaultFixture, HardcodedAppIDReturnedForHardcodedPID) {
+    gIntentionalFailureNum = 0;
+    boost::optional<std::string> ret = mApplicationMgr.get_appid_by_pid(gHardcodedPID);
+
+    bool found = false;
+    if (ret) found = true;
+    ASSERT_TRUE(found);
+}
+
+TEST_F(DefaultFixture, AppIDNotFoundForNonExistingPID) {
+    gIntentionalFailureNum = 0;
+    const pid_t non_existing_pid = 0;
+    boost::optional<std::string> ret = mApplicationMgr.get_appid_by_pid(non_existing_pid);
+
+    bool found = false;
+    if (ret) found = true;
+    ASSERT_FALSE(found);
+}
+
+TEST_F(DefaultFixture, RetriesForAtLeastThreeTimes) {
+    int max_failure_num = 3;
+    for (int failure_num = 0;failure_num < max_failure_num;failure_num++) {
+        gIntentionalFailureNum = failure_num;
+        boost::optional<std::string> ret = mApplicationMgr.get_appid_by_pid(gHardcodedPID);
+
+        bool found = false;
+        if (ret) found = true;
+        ASSERT_TRUE(found);
+    }
+}
+
+TEST_F(DefaultFixture, RetriesNoMoreThanThreeTimes) {
+    gIntentionalFailureNum = 4;
+    boost::optional<std::string> ret = mApplicationMgr.get_appid_by_pid(gHardcodedPID);
+
+    bool found = false;
+    if (ret) found = true;
+    ASSERT_FALSE(found);
+}
+
+/* Uncomment the below piece of code if you need your own main() */
+/*
+int main(int argc, char** argv) {
+    std::cout << "Starting tests" << std::endl;
+    testing::InitGoogleTest(&argc, argv);
+    int ret = RUN_ALL_TESTS();
+    return ret;
+}
+*/
diff --git a/tests/utc/application-manager/ttpo.xml b/tests/utc/application-manager/ttpo.xml
new file mode 100644 (file)
index 0000000..991e48a
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ttpo>
+    <source_files>test_main.cpp ../../../src/application_manager_aul.cpp</source_files>
+    <include_directories>../../../inc/</include_directories>
+    <required_packages>capi-appfw-application</required_packages>
+    <required_packages>glib-2.0</required_packages>
+    <required_packages>capi-appfw-service-application</required_packages>
+    <wrapper_functions>aul_app_get_appid_bypid</wrapper_functions>
+</ttpo>