Client example: extract duplicate code 11/276611/2
authorMichal Bloch <m.bloch@samsung.com>
Tue, 21 Jun 2022 20:15:46 +0000 (22:15 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Wed, 22 Jun 2022 13:05:37 +0000 (15:05 +0200)
 * changes a loop condition to the first-last form,
   which is equivalent and used everywhere else.

 * changes `switch_user_test` to return bool to
   conform with the new convention I am in the
   process of enforcing.

Change-Id: Id233a33ab0f0977b4e6ce660457761ce27520c8e
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
clientExample/app/main.cpp

index aef1082..59291da 100644 (file)
@@ -120,7 +120,7 @@ void test_reply_switchuser_callback(int result, void *cb_data)
        g_mutex_unlock(&mutex);
 }
 
-int switch_user_test(int user_id)
+bool switch_user_test(int user_id)
 {
        volatile test_user_data test_switch_ud;
        test_switch_ud.callback_result = CALLBACK_DATA_MAGIC;
@@ -132,7 +132,7 @@ int switch_user_test(int user_id)
        {
                g_mutex_unlock(&mutex);
                printf("Error subsession_switch_user res is%d\n", switch_user_res);
-               return -1;
+               return false;
        }
 
        /* The wait has to be in a loop because of
@@ -148,8 +148,32 @@ int switch_user_test(int user_id)
        int new_user;
        int r = subsession_get_current_user(SESSION_UID, &new_user);
        if (r != SUBSESSION_ERROR_NONE)
-               return -1;
-       return new_user;
+               return false;
+       return new_user == user_id;
+}
+
+bool switch_to_each_user_to_generate_events()
+{
+       for (int i = firstUser; i <= lastUser; ++i) {
+               if (!switch_user_test(i)) {
+                       printf("Failed to switch user\n");
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+bool check_callbacks_called(int expected)
+{
+       int cc = g_atomic_int_get(&callbackCount);
+       if (cc != expected) {
+               printf("Failed to call the appropriate number of callbacks (%d expected, got %d)\n", expected, cc);
+               return false;
+       }
+
+       green_print("done");
+       return true;
 }
 
 void wait_for_all_pending_callbacks(GMainLoop *loop, GThread *loop_thread)
@@ -239,27 +263,10 @@ int main(int argc, char *argv[])
                return EXIT_FAILURE;
        }
 
-       for (int i = firstUser; i < registered_users + firstUser; ++i)
-       {
-               int res = switch_user_test(i);
-               if (res != i)
-               {
-                       printf("Switch user error\n");
-                       return EXIT_FAILURE;
-               }
-       }
-
-       green_print("done");
-
-       printf("Register event callback test... ");
-       int cc = g_atomic_int_get(&callbackCount);
-       if (cc == registered_users)
-               green_print("ok");
-       else
-       {
-               printf("Failed to call all callbacks (%d expected, got %d)\n", registered_users, cc);
+       if (!switch_to_each_user_to_generate_events()
+       ||  !check_callbacks_called(registered_users))
                return EXIT_FAILURE;
-       }
+
        ///===================================///
        printf("Subsession unregister event callback test...");
 
@@ -270,31 +277,13 @@ int main(int argc, char *argv[])
                return EXIT_FAILURE;
        }
 
-       for (int i = firstUser; i < registered_users + firstUser; ++i)
-       {
-               int res = switch_user_test(i);
-               if (res != i)
-               {
-                       printf("Switch user error\n");
-                       return EXIT_FAILURE;
-               }
-
-       }
-
-       cc = g_atomic_int_get(&callbackCount);
-       if (cc == 0)
-               green_print("done");
-       else
-       {
-               printf("Failed to unregister all callbacks (%d went through)\n", cc);
+       if (!switch_to_each_user_to_generate_events()
+       ||  !check_callbacks_called(0))
                return EXIT_FAILURE;
-       }
 
        ///======================================///
        printf("Removing users...");
-       int end_res = switch_user_test(0);
-       if (end_res != 0)
-       {
+       if (!switch_user_test(0)) {
                printf("Error setting user to 0\n");
                return EXIT_FAILURE;
        }