Added testcases for reference count operations 63/275563/7 accepted/tizen/unified/20220616.141907 submit/tizen/20220615.021403 submit/unittc/20220615.013956
authordesperadok <heechul.jeon@samsung.com>
Thu, 26 May 2022 02:58:49 +0000 (11:58 +0900)
committerdesperadok <heechul.jeon@samsung.com>
Tue, 31 May 2022 02:51:12 +0000 (11:51 +0900)
- 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 <heechul.jeon@samsung.com>
include/media_format_internal.h
packaging/capi-media-tool.spec
src/media_format_internal.c
unittest/unittest.cpp

index a94ec89280b66aa5185e1a11664fc799e8461f4b..9f9fdf5631383f17cc7152525944920808059611 100644 (file)
@@ -17,8 +17,6 @@
 #ifndef __TIZEN_MEDIA_FORMAT_INTERNAL_H__
 #define __TIZEN_MEDIA_FORMAT_INTERNAL_H__
 
-#include <stdint.h>
-#include <tizen.h>
 #include <media_format.h>
 
 #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
index ff60f70fae3f01a8b44b3b371277c5d2482d6948..03d15f0c87f892925f95f01725abdb3dc9d186b6 100644 (file)
@@ -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
index cf02762a801c8c00132d5bf33f81cec2b5e1b18e..c0c1fb0a7dff2d01ff5f89957f2001ba6435fac3 100644 (file)
@@ -13,9 +13,6 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-// #include <stdio.h>
-// #include <stdlib.h>
-// #include <string.h>
 
 #include <dlog.h>
 #include <media_format.h>
@@ -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);
index 43a7a9fd7dcf7df6ebd9ca7f3081df42ad47ce04..94434761921bd1f0a7868582db4f9d81e33ef8bb 100644 (file)
  * limitations under the License.
  */
 
+#include <vector>
 #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);