Back to index

unity  6.0.0
TestUBus.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Gordon Allott <gord.allott@canonical.com>
00018  *
00019  */
00020 
00021 
00022 #include "config.h"
00023 #include "ubus-server.h"
00024 
00025 #define MESSAGE1 "TEST MESSAGE ONE"
00026 #define MESSAGE2 "ՄᕅᏆⲤꙨႧΈ Ϊટ ಗשׁຣ໐ɱË‼‼❢"
00027 
00028 static void TestAllocation(void);
00029 static void TestMainLoop(void);
00030 static void TestPropagation(void);
00031 
00032 void
00033 TestUBusCreateSuite()
00034 {
00035 #define _DOMAIN "/Unit/UBus"
00036   g_test_add_func(_DOMAIN"/Allocation", TestAllocation);
00037   g_test_add_func(_DOMAIN"/Propagation", TestPropagation);
00038   g_test_add_func(_DOMAIN"/MainLoop", TestMainLoop);
00039 }
00040 
00041 static void
00042 TestAllocation()
00043 {
00044   UBusServer* serv_1 = ubus_server_get_default();
00045   UBusServer* serv_2 = ubus_server_get_default();
00046 
00047   g_assert(serv_1 != NULL);
00048   g_assert(serv_2 != NULL);
00049 
00050   // i used a different way of making a singleton than i am used to
00051   // so i'm not 100% confident in it yet
00052   g_assert(serv_1 == serv_2);
00053 }
00054 
00055 void
00056 test_handler_inc_counter(GVariant* data, gpointer val)
00057 {
00058   // inc a counter when we get called
00059   gint* counter = (gint*)val;
00060   *counter = *counter + 1;
00061 }
00062 
00063 void
00064 test_handler_inc_counter_2(GVariant* data, gpointer val)
00065 {
00066   // inc a counter by two when called
00067   gint* counter = (gint*)val;
00068   *counter = *counter + 2;
00069 }
00070 
00071 static void
00072 TestPropagation()
00073 {
00074   UBusServer* ubus;
00075   gint        counter, i;
00076   guint       handler1, handler2;
00077 
00078   ubus = ubus_server_get_default();
00079   handler1 = ubus_server_register_interest(ubus, MESSAGE1,
00080                                            test_handler_inc_counter,
00081                                            &counter);
00082 
00083   handler2 = ubus_server_register_interest(ubus, MESSAGE2,  // tests UNICODE
00084                                            test_handler_inc_counter_2,
00085                                            &counter);
00086 
00087   counter = 0;
00088   for (i = 0; i < 1000; i++)
00089   {
00090     ubus_server_send_message(ubus, MESSAGE1, NULL);
00091   }
00092 
00093   ubus_server_force_message_pump(ubus);
00094 
00095   counter = 0;
00096   for (i = 0; i < 1000; i++)
00097   {
00098     ubus_server_send_message(ubus, MESSAGE1, NULL);
00099     ubus_server_send_message(ubus, MESSAGE2, NULL);
00100   }
00101   ubus_server_force_message_pump(ubus);
00102 
00103   g_assert(counter == 3000);
00104 
00105   ubus_server_unregister_interest(ubus, handler1);
00106   ubus_server_unregister_interest(ubus, handler2);
00107 
00108   counter = 0;
00109   ubus_server_send_message(ubus, MESSAGE1, NULL);
00110   ubus_server_send_message(ubus, MESSAGE2, NULL);
00111 
00112   ubus_server_force_message_pump(ubus);
00113 
00114   g_assert(counter == 0);
00115 }
00116 
00117 gboolean
00118 main_loop_bailout(gpointer data)
00119 {
00120   GMainLoop* mainloop = (GMainLoop*)data;
00121   g_main_quit(mainloop);
00122   return FALSE;
00123 }
00124 
00125 void
00126 test_handler_mainloop(GVariant* data, gpointer val)
00127 {
00128   // inc a counter when we get called
00129   gint* counter = (gint*)val;
00130   *counter = *counter + 1;
00131 
00132 }
00133 
00134 static void
00135 TestMainLoop()
00136 {
00137   GMainLoop*  mainloop;
00138   UBusServer* ubus;
00139   gint        counter = 0;
00140 
00141   ubus = ubus_server_get_default();
00142   mainloop = g_main_loop_new(NULL, TRUE);
00143 
00144   g_timeout_add_seconds(1, main_loop_bailout, mainloop);
00145 
00146   ubus_server_register_interest(ubus, MESSAGE1,
00147                                 test_handler_mainloop,
00148                                 &counter);
00149 
00150   ubus_server_send_message(ubus, MESSAGE1, NULL);
00151   g_main_loop_run(mainloop);
00152 
00153   g_assert(counter == 1);
00154 
00155 }
00156 
00157