From 441ea0d271ac48a6b5c9d17ab1d1c9d29d6111d9 Mon Sep 17 00:00:00 2001 From: Amit Tewari Date: Thu, 17 Dec 2015 16:39:06 +0530 Subject: [PATCH] Multithread dbus server client communication test-case To test Multithread data communication between Dbus server client --- cmake/test/name-test/CMakeLists.txt | 2 + test/Makefile.am | 1 + ...sktop.DBus.TestSuite.Multithreaddbus.service.in | 3 + test/name-test/Makefile.am | 4 +- test/name-test/test-multithread-client.c | 91 ++++++++++++++++++++ test/name-test/test-multithread-server.c | 82 ++++++++++++++++++ 6 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 test/data/valid-service-files/org.freedesktop.DBus.TestSuite.Multithreaddbus.service.in create mode 100644 test/name-test/test-multithread-client.c create mode 100644 test/name-test/test-multithread-server.c diff --git a/cmake/test/name-test/CMakeLists.txt b/cmake/test/name-test/CMakeLists.txt index befb28f..d0ec4e0 100644 --- a/cmake/test/name-test/CMakeLists.txt +++ b/cmake/test/name-test/CMakeLists.txt @@ -12,4 +12,6 @@ add_helper_executable(test-shutdown ${NAMEtest-DIR}/test-shutdown.c dbus-testuti add_helper_executable(test-privserver ${NAMEtest-DIR}/test-privserver.c dbus-testutils) add_helper_executable(test-privserver-client ${NAMEtest-DIR}/test-privserver-client.c dbus-testutils) add_helper_executable(test-autolaunch ${NAMEtest-DIR}/test-autolaunch.c dbus-testutils) +add_helper_executable(test-multithread-server ${NAMEtest-DIR}/test-multithread-server.c dbus-testutils) +add_helper_executable(test-multithread-client ${NAMEtest-DIR}/test-multithread-client.c dbus-testutils) endif (DBUS_ENABLE_EMBEDDED_TESTS) diff --git a/test/Makefile.am b/test/Makefile.am index 605170e..b6c9306 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -324,6 +324,7 @@ in_data = \ data/valid-service-files/org.freedesktop.DBus.TestSuiteSegfaultService.service.in \ data/valid-service-files/org.freedesktop.DBus.TestSuiteShellEchoServiceFail.service.in \ data/valid-service-files/org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess.service.in \ + data/valid-service-files/org.freedesktop.DBus.TestSuite.Multithreaddbus.service.in \ $(NULL) EXTRA_DIST += $(in_data) diff --git a/test/data/valid-service-files/org.freedesktop.DBus.TestSuite.Multithreaddbus.service.in b/test/data/valid-service-files/org.freedesktop.DBus.TestSuite.Multithreaddbus.service.in new file mode 100644 index 0000000..2e9ad8c --- /dev/null +++ b/test/data/valid-service-files/org.freedesktop.DBus.TestSuite.Multithreaddbus.service.in @@ -0,0 +1,3 @@ +[D-BUS Service] +Name=org.freedesktop.DBus.TestSuite.Multithreaddbus +Exec=@DBUS_TEST_EXEC@/name-test/test-multithread-server@EXEEXT@ diff --git a/test/name-test/Makefile.am b/test/name-test/Makefile.am index 3b7fcc6..5c77d3d 100644 --- a/test/name-test/Makefile.am +++ b/test/name-test/Makefile.am @@ -41,7 +41,7 @@ if DBUS_ENABLE_EMBEDDED_TESTS ## we use noinst_PROGRAMS not check_PROGRAMS for TESTS so that we ## build even when not doing "make check" -noinst_PROGRAMS=test-pending-call-dispatch test-pending-call-timeout test-threads-init test-ids test-shutdown test-privserver test-privserver-client test-autolaunch +noinst_PROGRAMS=test-pending-call-dispatch test-pending-call-timeout test-threads-init test-ids test-shutdown test-privserver test-privserver-client test-autolaunch test-multithread-server test-multithread-client test_pending_call_dispatch_LDADD=$(top_builddir)/dbus/libdbus-1.la test_pending_call_timeout_LDADD=$(top_builddir)/dbus/libdbus-1.la @@ -52,5 +52,7 @@ test_shutdown_LDADD=../libdbus-testutils.la test_privserver_LDADD=../libdbus-testutils.la test_privserver_client_LDADD=../libdbus-testutils.la test_autolaunch_LDADD=../libdbus-testutils.la +test_multithread_server_LDADD=../libdbus-testutils.la +test_multithread_client_LDADD=../libdbus-testutils.la endif diff --git a/test/name-test/test-multithread-client.c b/test/name-test/test-multithread-client.c new file mode 100644 index 0000000..562945b --- /dev/null +++ b/test/name-test/test-multithread-client.c @@ -0,0 +1,91 @@ +#include +#include +#include "../test-utils.h" + +#define DBUS_MULTITHREAD_SERVICE "org.freedesktop.DBus.TestSuite.Multithreaddbus" +#define DBUS_MULTITHREAD_PATH "/org/freedesktop/DBus/TestSuite/Multithreaddbus" +#define DBUS_MULTITHREAD_IFACE DBUS_MULTITHREAD_SERVICE +#define MAXTHREAD 5 +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +static void +die (const char *message, ...) +{ + va_list args; + va_start (args, message); + vfprintf (stderr, message, args); + va_end (args); + exit (1); +} + +static void* +send_multithread_data() +{ + DBusMessage *message; + DBusConnection *dbus_conn; + DBusMessage *reply; + DBusError error; + + const long long DataSize = 1024*1024; + int i = 0; + const int Num_Msg = 25; + int Ret_Id = 0; + char *ack; + dbus_error_init (&error); + dbus_conn = dbus_bus_get (DBUS_BUS_SESSION, &error); + if (dbus_error_is_set (&error)) + die ("Client:couldn't access session bus\n"); + + for(i = 0 ; i < Num_Msg ; i++ ) + { + char *data = (char*)malloc(DataSize); + if(!data) + die ("couldn't malloc data\n"); + memset(data,'a',DataSize*sizeof(char)); + data[DataSize - 1] = '\0'; + + message = dbus_message_new_method_call(DBUS_MULTITHREAD_SERVICE, + DBUS_MULTITHREAD_PATH, + DBUS_MULTITHREAD_IFACE, + "MultithreadData"); + dbus_message_append_args (message, DBUS_TYPE_INT32, &i, DBUS_TYPE_STRING, &data, DBUS_TYPE_INVALID); + reply = dbus_connection_send_with_reply_and_block(dbus_conn, message, -1, &error); + if (dbus_error_is_set(&error)) + { die ("Client:couldn't send message: %s\n", error.message); + dbus_message_unref (message); + } + + if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &Ret_Id, DBUS_TYPE_STRING, &ack, DBUS_TYPE_INVALID)) + die("Client: Error getting ARG from Reply"); + + if(!(i == Ret_Id && ack && !strcmp(ack,"DataRecieved"))) + die ("Invalid reply from server"); + printf("Client : Reply received thread = %u\n", pthread_self()); + + if(reply) + dbus_message_unref (reply); + free(data); + dbus_message_unref (message); + } + return 0; + +} + +int +main(int argc, char** argv) +{ + int i = 0; + pthread_t mythread[MAXTHREAD]; + dbus_threads_init_default(); + for(i = 0; i < MAXTHREAD; i++) + { + if(pthread_create(&mythread[i],NULL,send_multithread_data,NULL)!=0) + die("error creating thread"); + } + + for(i = 0; i < MAXTHREAD; i++) + pthread_join(mythread[i],NULL); + + return 0; + +} diff --git a/test/name-test/test-multithread-server.c b/test/name-test/test-multithread-server.c new file mode 100644 index 0000000..fe6b27b --- /dev/null +++ b/test/name-test/test-multithread-server.c @@ -0,0 +1,82 @@ +#include +#include "../test-utils.h" + +static void +die (const char *message, ...) +{ + va_list args; + va_start (args, message); + vfprintf (stderr, message, args); + va_end (args); + exit (1); +} + +static DBusHandlerResult +handler_function(DBusConnection *conn, DBusMessage *msg, void *user_data) +{ + DBusMessage *reply; + DBusError error; + char *ack="DataRecieved"; + dbus_error_init (&error); + if (dbus_message_is_method_call (msg, + "org.freedesktop.DBus.TestSuite.Multithreaddbus", + "MultithreadData")) + { + int Id = 0; + reply = dbus_message_new_method_return(msg); + if (!dbus_message_get_args(msg, &error, DBUS_TYPE_INT32, &Id, DBUS_TYPE_INVALID)) + die("Error getting arguements from Reply"); + + dbus_message_append_args (reply, DBUS_TYPE_INT32, &Id, DBUS_TYPE_STRING, &ack, DBUS_TYPE_INVALID); + if(!dbus_connection_send(conn, reply, NULL)) + die("couldn't send reply"); + + dbus_message_unref(reply); + return DBUS_HANDLER_RESULT_HANDLED; + } + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + +} + +int +main(int argc, char **argv) +{ + DBusConnection *dbus_conn; + DBusObjectPathVTable *dbus_vtable; + DBusError error; + DBusLoop *loop; + + dbus_threads_init_default(); + dbus_error_init(&error); + + dbus_conn = dbus_bus_get (DBUS_BUS_SESSION, &error); + if(dbus_error_is_set(&error)) + die ("couldn't access session bus"); + + loop = _dbus_loop_new (); + test_connection_setup (loop, dbus_conn); + + dbus_bus_request_name(dbus_conn, "org.freedesktop.DBus.TestSuite.Multithreaddbus", 0, &error); + if (dbus_error_is_set (&error)) + die ("couldn't request name: %s", error.message); + + dbus_vtable = malloc(sizeof(DBusObjectPathVTable)); + if(!dbus_vtable) + die ("error malloc vtable"); + dbus_vtable->unregister_function = NULL; + dbus_vtable->message_function = handler_function; + + if(!dbus_connection_register_object_path(dbus_conn, + "/org/freedesktop/DBus/TestSuite/Multithreaddbus", + dbus_vtable, + NULL)) + die ("couldn't register object_path"); + + _dbus_loop_run (loop); + test_connection_shutdown (loop, dbus_conn); + dbus_connection_unref (dbus_conn); + _dbus_loop_unref (loop); + free(dbus_vtable); + return 0; + +} -- 1.7.9.5