This is an automated email from the ASF dual-hosted git repository.

pengzheng pushed a commit to branch feature/556-osgi-uninstall
in repository https://gitbox.apache.org/repos/asf/celix.git

commit a0a37549f27322836011e045e6155d97dcc756da
Author: PengZheng <[email protected]>
AuthorDate: Wed May 17 12:35:56 2023 +0800

    Add more test cases for celix_bundleCache_create error handling.
---
 .../src/CelixBundleCacheErrorInjectionTestSuite.cc | 26 +++++++++++++++++-----
 libs/framework/src/celix_bundle_cache.c            | 22 +++++++++---------
 libs/utils/include/celix_file_utils.h              |  2 +-
 3 files changed, 32 insertions(+), 18 deletions(-)

diff --git 
a/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc 
b/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc
index dd181972..c5d894f6 100644
--- a/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc
+++ b/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc
@@ -18,14 +18,15 @@
  */
 
 #include "asprintf_ei.h"
-#include "celix_constants.h"
 #include "celix_bundle_cache.h"
-#include "celix_properties.h"
+#include "celix_constants.h"
+#include "celix_hash_map_ei.h"
 #include "celix_log.h"
+#include "celix_properties.h"
+#include "celix_utils_ei.h"
 #include "framework_private.h"
-#include "gtest/gtest.h"
-#include "celix_hash_map_ei.h"
 #include "malloc_ei.h"
+#include "gtest/gtest.h"
 
 class CelixBundleCacheErrorInjectionTestSuite : public ::testing::Test {
 public:
@@ -35,13 +36,17 @@ public:
     }
 
     ~CelixBundleCacheErrorInjectionTestSuite() override {
+        celix_ei_expect_celix_utils_createDirectory(nullptr, 0, CELIX_SUCCESS);
+        celix_ei_expect_celix_utils_deleteDirectory(nullptr, 0, CELIX_SUCCESS);
+        celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
         celix_ei_expect_asprintf(nullptr, 0, -1);
         celix_ei_expect_celix_stringHashMap_create(nullptr, 0, nullptr);
         celix_ei_expect_calloc(nullptr, 0, nullptr);
         celix_frameworkLogger_destroy(fw.logger);
         celix_properties_destroy(fw.configurationMap);
     }
-    struct celix_framework fw{};
+
+    struct celix_framework fw {};
 };
 
 TEST_F(CelixBundleCacheErrorInjectionTestSuite, CacheCreateErrorTest) {
@@ -57,4 +62,15 @@ TEST_F(CelixBundleCacheErrorInjectionTestSuite, 
CacheCreateErrorTest) {
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_create(&fw, &cache));
     EXPECT_EQ(nullptr, cache);
     celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CACHE_USE_TMP_DIR, false);
+    celix_ei_expect_celix_utils_strdup((void *) celix_bundleCache_create, 0, 
nullptr);
+    EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_create(&fw, &cache));
+    EXPECT_EQ(nullptr, cache);
+    celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE, true);
+    celix_ei_expect_celix_utils_deleteDirectory((void *) 
celix_bundleCache_create, 1, CELIX_FILE_IO_EXCEPTION);
+    EXPECT_EQ(CELIX_FILE_IO_EXCEPTION, celix_bundleCache_create(&fw, &cache));
+    EXPECT_EQ(nullptr, cache);
+    celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE, false);
+    celix_ei_expect_celix_utils_createDirectory((void *) 
celix_bundleCache_create, 0, CELIX_FILE_IO_EXCEPTION);
+    EXPECT_EQ(CELIX_FILE_IO_EXCEPTION, celix_bundleCache_create(&fw, &cache));
+    EXPECT_EQ(nullptr, cache);
 }
\ No newline at end of file
diff --git a/libs/framework/src/celix_bundle_cache.c 
b/libs/framework/src/celix_bundle_cache.c
index 1b52a00f..59cf01a4 100644
--- a/libs/framework/src/celix_bundle_cache.c
+++ b/libs/framework/src/celix_bundle_cache.c
@@ -54,8 +54,6 @@ struct celix_bundle_cache {
     celix_string_hash_map_t* locationToBundleIdLookupMap; //key = location, 
value = bundle id.
 };
 
-static void 
celix_bundleCache_updateIdForLocationLookupMap(celix_bundle_cache_t* cache);
-
 static const char* bundleCache_progamName() {
 #if defined(__APPLE__) || defined(__FreeBSD__)
        return getprogname();
@@ -152,23 +150,18 @@ celix_status_t 
celix_bundleCache_create(celix_framework_t* fw, celix_bundle_cach
     }
 
     if (cache->deleteOnCreate) {
-        status = celix_bundleCache_deleteCacheDir(cache);
-        if (status != CELIX_SUCCESS) {
-            celix_bundleCache_destroy(cache);
-            return status;
-        }
+        CELIX_GOTO_IF_ERR(status = celix_bundleCache_deleteCacheDir(cache), 
manipulate_dir_failure);
     }
-
     const char* errorStr;
     status = celix_utils_createDirectory(cache->cacheDir, false, &errorStr);
     if (status != CELIX_SUCCESS) {
         fw_logCode(fw->logger, CELIX_LOG_LEVEL_ERROR, status, "Cannot create 
bundle cache directory %s, error %s", cache->cacheDir, errorStr);
-        celix_bundleCache_destroy(cache);
-        return status;
+        goto manipulate_dir_failure;
     }
-    celix_bundleCache_updateIdForLocationLookupMap(cache);
     *out = cache;
     return CELIX_SUCCESS;
+manipulate_dir_failure:
+    free(cache->cacheDir);
 cache_dir_failure:
     celixThreadMutex_destroy(&cache->mutex);
     celix_stringHashMap_destroy(cache->locationToBundleIdLookupMap);
@@ -247,7 +240,6 @@ celix_status_t  
celix_bundleCache_destroyArchive(celix_bundle_cache_t *cache, bu
 
 /**
  * Update location->bundle id lookup map.
- * Assumes that bundle cache dir are not removed, so only adding not removing 
entries.
  */
 static void 
celix_bundleCache_updateIdForLocationLookupMap(celix_bundle_cache_t* cache) {
     celixThreadMutex_lock(&cache->mutex);
@@ -289,6 +281,12 @@ long 
celix_bundleCache_findBundleIdForLocation(celix_framework_t *fw, const char
         bndId = 
celix_stringHashMap_getLong(fw->cache->locationToBundleIdLookupMap, location, 
-1);
     }
     celixThreadMutex_unlock(&fw->cache->mutex);
+    if (bndId == -1) {
+        celix_bundleCache_updateIdForLocationLookupMap(fw->cache);
+        celixThreadMutex_lock(&fw->cache->mutex);
+        bndId = 
celix_stringHashMap_getLong(fw->cache->locationToBundleIdLookupMap, location, 
-1);
+        celixThreadMutex_unlock(&fw->cache->mutex);
+    }
     return bndId;
 }
 
diff --git a/libs/utils/include/celix_file_utils.h 
b/libs/utils/include/celix_file_utils.h
index f71cdab0..bff99763 100644
--- a/libs/utils/include/celix_file_utils.h
+++ b/libs/utils/include/celix_file_utils.h
@@ -41,7 +41,7 @@ CELIX_UTILS_EXPORT bool celix_utils_fileExists(const char* 
path);
 CELIX_UTILS_EXPORT bool celix_utils_directoryExists(const char* path);
 
 /**
- * @brief Create dir and if needed subdirs.
+ * @brief Create dir and if needed parent direcotries.
  * @param path The directory to create.
  * @param failIfPresent Whether to fail if the directory already exists.
  * @param errorOut An optional error output argument. If an error occurs this 
will point to a (static) error message.

Reply via email to