From 5ac37e8280dbdd62397138b15d9406bfad5dcd84 Mon Sep 17 00:00:00 2001 From: desperadok Date: Thu, 26 May 2022 11:58:49 +0900 Subject: [PATCH] Added testcases for reference count operations - Adding '--define "gtests 1"' to enable gtest - Use 'gbs chroot -r' to get into qemu env. Then 'make install' and then run unittc Change-Id: Idc1d529b00ba59257ef8e8c9f6ea9d4aaad60d3d Signed-off-by: desperadok --- include/media_format_internal.h | 6 +-- packaging/capi-media-tool.spec | 2 +- src/media_format_internal.c | 5 --- unittest/unittest.cpp | 70 ++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/include/media_format_internal.h b/include/media_format_internal.h index a94ec89..9f9fdf5 100644 --- a/include/media_format_internal.h +++ b/include/media_format_internal.h @@ -17,8 +17,6 @@ #ifndef __TIZEN_MEDIA_FORMAT_INTERNAL_H__ #define __TIZEN_MEDIA_FORMAT_INTERNAL_H__ -#include -#include #include #ifdef __cplusplus @@ -36,9 +34,11 @@ extern "C" { */ /** + * @internal * @brief Get reference count of #media_format_h object. * @since_tizen 7.0 - * @param[in] fmt exist #media_format_h + * @param[in] fmt The #media_format_h to get + * @param[out] ref_count Reference count of #media_format_h * * @return @c 0 on success, * otherwise a negative error value diff --git a/packaging/capi-media-tool.spec b/packaging/capi-media-tool.spec index ff60f70..03d15f0 100644 --- a/packaging/capi-media-tool.spec +++ b/packaging/capi-media-tool.spec @@ -1,6 +1,6 @@ Name: capi-media-tool Summary: A Core API media tool library in Tizen Native API -Version: 0.1.53 +Version: 0.1.54 Release: 0 Group: Multimedia/API License: Apache-2.0 diff --git a/src/media_format_internal.c b/src/media_format_internal.c index cf02762..c0c1fb0 100644 --- a/src/media_format_internal.c +++ b/src/media_format_internal.c @@ -13,9 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// #include -// #include -// #include #include #include @@ -26,8 +23,6 @@ int media_format_get_refcount(media_format_h fmt, int *ref_count) MEDIA_FORMAT_INSTANCE_CHECK(fmt); MEDIA_FORMAT_NULL_ARG_CHECK(ref_count); - media_format_s *fmt_handle; - *ref_count = MEDIA_FORMAT_GET_REFCOUNT(fmt); LOGI("format[%p] ref_count[%d]", fmt, *ref_count); diff --git a/unittest/unittest.cpp b/unittest/unittest.cpp index 43a7a9f..9443476 100644 --- a/unittest/unittest.cpp +++ b/unittest/unittest.cpp @@ -14,13 +14,14 @@ * limitations under the License. */ +#include #include "media_tool_gtest.h" -#include "media_format.h" #include "media_format_internal.h" using ::testing::InitGoogleTest; using ::testing::Test; using ::testing::TestCase; +using namespace std; class MediaToolTest : public ::testing::Test { protected: @@ -55,6 +56,73 @@ TEST(MediaToolTest, media_tool_gtest_refcount_init) media_format_unref(fmt); } +TEST(MediaToolTest, media_tool_gtest_refcount_general) +{ + media_format_h fmt; + int ref_count; + int ret = media_format_create(&fmt); + ASSERT_EQ(ret, MEDIA_FORMAT_ERROR_NONE); + + media_format_ref(fmt); + media_format_get_refcount(fmt, &ref_count); + ASSERT_EQ(ref_count, 2); + + media_format_ref(fmt); + media_format_get_refcount(fmt, &ref_count); + ASSERT_EQ(ref_count, 3); + + media_format_unref(fmt); + media_format_get_refcount(fmt, &ref_count); + ASSERT_EQ(ref_count, 2); + + media_format_unref(fmt); + media_format_unref(fmt); /* fmt got freed at this moment */ + media_format_get_refcount(fmt, &ref_count); + ASSERT_EQ(ref_count, 0); /* but the freed memory still hold refcount value(zero) */ + + ret = media_format_unref(fmt); /* trying to unref fmt which already destroyed */ + ASSERT_EQ(ret, MEDIA_FORMAT_ERROR_INVALID_OPERATION); +} + +static void *refcount_monkey_thread_func(gpointer data, gpointer param) +{ + media_format_h fmt = (media_format_h)param; + unsigned int refcount_limit = GPOINTER_TO_UINT(data); + + for (int i = 0; i < refcount_limit; i++) + media_format_ref(fmt); + + for (int i = 0; i < refcount_limit; i++) + media_format_unref(fmt); + + return NULL; +} + +TEST(MediaToolTest, media_tool_gtest_refcount_thread_safty) +{ + const unsigned int num_threads = 5; + const unsigned int refcount_limit_per_thread = 10000; + + media_format_h fmt; + int ref_count; + int ret = media_format_create(&fmt); + ASSERT_EQ(ret, MEDIA_FORMAT_ERROR_NONE); + + GThreadPool* thread_pool = g_thread_pool_new( + (GFunc)refcount_monkey_thread_func, + (gpointer)fmt, num_threads, TRUE, NULL); + + for (int i = 0; i < num_threads; i++) + g_thread_pool_push(thread_pool, GUINT_TO_POINTER(refcount_limit_per_thread), NULL); + + g_thread_pool_free(thread_pool, FALSE, TRUE); + + media_format_get_refcount(fmt, &ref_count); + ASSERT_EQ(ref_count, 1); + + media_format_unref(fmt); +} + int main(int argc, char **argv) { InitGoogleTest(&argc, argv); -- 2.34.1