test : bring event tester from daemon 30/141630/3
authorKichan Kwon <k_c.kwon@samsung.com>
Tue, 1 Aug 2017 07:15:33 +0000 (16:15 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Wed, 2 Aug 2017 02:06:53 +0000 (11:06 +0900)
- Test code is located in the daemon is not good
- To run event test, you can send D-Bus to the test module

Change-Id: I21ea44c924f187283935a2a7831bdcecd8fa049f
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/daemon/main.c
src/test/test.c

index 0dd04329dfc6a4a1cdb18e3f95324da941839b9b..9cdf7982980ca25df3abae49045fdac70d615e6b 100644 (file)
@@ -131,29 +131,6 @@ static int base_load_library(void)
        return 0;
 }
 
-void base_test(void)
-{
-       struct module *test_module = module_find("test");
-       int *arg1, *arg2;
-
-       if (!test_module)
-               return;
-
-       arg1 = malloc(sizeof(int));
-       *arg1 = 623;
-
-       _I("Broadcast event (arg = %d)", *arg1);
-       if (event_broadcast(RDHL_EVENT_TEST, arg1) < 0)
-               _E("Failed to broadcast event");
-
-       arg2 = malloc(sizeof(int));
-       *arg2 = 731;
-
-       _I("Send event to the test module (arg = %d)", *arg2);
-       if (event_send("test", RDHL_EVENT_TEST, arg2) < 0)
-               _E("Failed to send event");
-}
-
 int main(int argc, char **argv)
 {
        int ret;
@@ -182,8 +159,6 @@ int main(int argc, char **argv)
                goto finalize;
        }
 
-       base_test();
-
        base_mainloop_run();
 
 finalize:
index d5416d228b5484c73bf217a24a872f5b42c2643b..9334b3357939c9c2caf1f3606cb191b348191544 100644 (file)
 #define RDHL_DBUS_PATH_TEST                    RDHL_DBUS_PATH(Test)
 #define RDHL_DBUS_INTERFACE_TEST       RDHL_DBUS_INTERFACE(test)
 
+/* Wait 1 second from now */
+#define TIME_TO_WAIT_EVENT                     (g_get_monotonic_time() + 1000000)
+
+static GCond test_worker_thread_cond;
+static GMutex test_worker_thread_mutex;
+
 gboolean test_event_handler(gpointer user_data)
 {
        struct event *event = (struct event *)user_data;
        int *arg = (int *)event->data;
 
-       if (event->type == RDHL_EVENT_TEST)
+       if (event->type == RDHL_EVENT_TEST) {
                _I("Test module's event (type = %d) callback is called. Receive %d",
                                event->type, *arg);
+               g_cond_signal(&test_worker_thread_cond);
+       }
 
        return TRUE;
 }
 
-void test_dbus_method_handler(GDBusMethodInvocation *invocation, GVariant *params)
+void test_dbus_method(GDBusMethodInvocation *invocation, GVariant *params)
 {
        GVariantIter *iter = NULL;
        gsize size;
@@ -63,11 +71,59 @@ void test_dbus_method_handler(GDBusMethodInvocation *invocation, GVariant *param
        g_dbus_method_invocation_return_value(invocation, g_variant_new("(ii)", sum, mul));
 }
 
+void test_event(GDBusMethodInvocation *invocation, GVariant *params)
+{
+       int *arg_broadcast = malloc(sizeof(int));
+       int *arg_send = malloc(sizeof(int));
+       gint err_code;
+
+       g_assert(arg_broadcast && arg_send);
+
+       /* Broadcast event */
+       g_mutex_lock(&test_worker_thread_mutex);
+       *arg_broadcast = 623;
+       _I("Broadcast event (arg = %d)", *arg_broadcast);
+       if (event_broadcast(RDHL_EVENT_TEST, arg_broadcast) < 0) {
+               err_code = G_DBUS_ERROR_FAILED;
+               goto err;
+       }
+       if (!g_cond_wait_until(&test_worker_thread_cond, &test_worker_thread_mutex,
+                                               TIME_TO_WAIT_EVENT)) {
+               err_code = G_DBUS_ERROR_TIMEOUT;
+               goto err;
+       }
+       g_mutex_unlock(&test_worker_thread_mutex);
+
+       /* Send event */
+       g_mutex_lock(&test_worker_thread_mutex);
+       *arg_send = 731;
+       _I("Send event to the test module (arg = %d)", *arg_send);
+       if (event_send("test", RDHL_EVENT_TEST, arg_send) < 0) {
+               err_code = G_DBUS_ERROR_FAILED;
+               goto err;
+       }
+       if (!g_cond_wait_until(&test_worker_thread_cond, &test_worker_thread_mutex,
+                                               TIME_TO_WAIT_EVENT)) {
+               err_code = G_DBUS_ERROR_TIMEOUT;
+               goto err;
+       }
+       g_mutex_unlock(&test_worker_thread_mutex);
+
+       g_dbus_method_invocation_return_value(invocation,
+                       g_variant_new("(s)", "Event test : success"));
+       return;
+err:
+       g_mutex_unlock(&test_worker_thread_mutex);
+       g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
+                       err_code, "Event test : fail");
+}
+
 static struct dbus_method test_dbus_methods[] = {
-       { "TestMethod", "ai", "ii", test_dbus_method_handler },
+       { "DBusMethodTest", "ai", "ii", test_dbus_method },
+       { "EventTest", NULL, "s", test_event },
 };
 
-void test_dbus_signal_handler(GVariant *params)
+void test_dbus_signal(GVariant *params)
 {
        int arg;
 
@@ -77,7 +133,7 @@ void test_dbus_signal_handler(GVariant *params)
 }
 
 static struct dbus_signal test_dbus_signals[] = {
-       { "TestSignal", test_dbus_signal_handler },
+       { "DBusSignalTest", test_dbus_signal },
 };
 
 int __INIT__ test_init(void)
@@ -88,6 +144,9 @@ int __INIT__ test_init(void)
        dbus_register_signals(RDHL_DBUS_PATH_TEST, RDHL_DBUS_INTERFACE_TEST,
                                                test_dbus_signals, ARRAY_SIZE(test_dbus_signals));
 
+       g_cond_init(&test_worker_thread_cond);
+       g_mutex_init(&test_worker_thread_mutex);
+
        _I("Test module is initialized!");
 
        return 0;