unit tests: new api function return values no longer break tests 49/211649/1
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Wed, 7 Aug 2019 13:19:05 +0000 (15:19 +0200)
committerMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Wed, 7 Aug 2019 13:19:05 +0000 (15:19 +0200)
Change-Id: I5eeb421fd6e4c51ce250bdb331e1f8ff65882430
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
tests/unit_tests.c

index 9582b84b8309606c56ddb9afeb29229b22f62162..96d678d2280ce5373b1f4a179e6e15d396d186ff 100644 (file)
@@ -158,7 +158,7 @@ gboolean test_timeout(gpointer user_data)
 {
        struct context *ctx = (struct context *)user_data;
 
-       ctx->result = UNIT_CONTROL_ERROR;
+       ctx->result = UNIT_CONTROL_TIMEOUT;
        g_main_loop_quit(ctx->loop);
        return FALSE;
 }
@@ -166,13 +166,13 @@ gboolean test_timeout(gpointer user_data)
 gboolean func_start(gpointer user_data)
 {
        struct context *ctx = (struct context *)user_data;
-       int ret = ctx->func_sync(ctx->bus_type, TEST_SERVICE, -1);
+       int ret = ctx->func_sync(ctx->bus_type, TEST_SERVICE, UNIT_CONTROL_TIMEOUT_DEFAULT);
 
-       if (ret < 0) {
-               ctx->result = UNIT_CONTROL_ERROR;
+       if (ret == UNIT_CONTROL_ERROR || ret == UNIT_CONTROL_TIMEOUT) {
+               ctx->result = ret;
                g_main_loop_quit(ctx->loop);
        }
-return FALSE;
+       return FALSE;
 }
 
 int test_sync_action(struct context *ctx)
@@ -239,7 +239,7 @@ void handler(int status, void *user_data, GError *err)
 {
        struct context *ctx = (struct context *)user_data;
        ctx->result = status;
-       if (status != UNIT_CONTROL_OK)
+       if (status == UNIT_CONTROL_ERROR || status == UNIT_CONTROL_TIMEOUT)
                g_main_loop_quit(ctx->loop);
 }
 
@@ -252,14 +252,14 @@ int test_async_action(struct context *ctx)
        ctx->loop = g_main_loop_new(NULL, FALSE);
        ctx->test_timeout = g_timeout_add_seconds(10, test_timeout, ctx);
 
-       ret = ctx->func_async(ctx->bus_type, TEST_SERVICE, handler, ctx, -1);
-       if (ret < 0)
+       ret = ctx->func_async(ctx->bus_type, TEST_SERVICE, handler, ctx, UNIT_CONTROL_TIMEOUT_DEFAULT);
+       if (ret == UNIT_CONTROL_ERROR)
                return ret;
 
        g_main_loop_run(ctx->loop);
        g_main_loop_unref(ctx->loop);
 
-       return ret;
+       return ctx->result;
 }
 
 int test_unit_start_async(struct context *ctx)
@@ -286,8 +286,8 @@ int test_unit_stop_async(struct context *ctx)
 
 int test_unit_restart_async(struct context *ctx)
 {
-       ctx->unit_state[0] = UNIT_ON;
-       ctx->unit_state[1] = UNIT_OFF;
+       ctx->unit_state[0] = UNIT_OFF;
+       ctx->unit_state[1] = UNIT_ON;
        ctx->unit_state_pos = 0;
        ctx->unit_state_cnt = 2;
        ctx->result = UNIT_CONTROL_OK;
@@ -295,6 +295,24 @@ int test_unit_restart_async(struct context *ctx)
        return test_async_action(ctx);
 }
 
+static const char *retval2string(int r)
+{
+       switch (r) {
+       case UNIT_CONTROL_REQUEST_SENT:
+               return "request sent";
+               break;
+       case UNIT_CONTROL_TIMEOUT:
+               return "timeout";
+               break;
+       case UNIT_CONTROL_OK:
+               return "ok";
+               break;
+       default:
+               return "error";
+               break;
+       }
+}
+
 void test(GBusType bus_type, struct unit_test tests[])
 {
        assert(bus_type == G_BUS_TYPE_SYSTEM || bus_type == G_BUS_TYPE_SESSION);
@@ -312,8 +330,8 @@ void test(GBusType bus_type, struct unit_test tests[])
 
        for (int i = 0; tests[i].id; ++i) {
                ret = tests[i].func(&ctx);
-               if (ret < 0)
-                       printf("[FAILED] %s (ret = %d)\n", tests[i].id, ret);
+               if (ret != UNIT_CONTROL_OK)
+                       printf("[FAILED] %s (%s)\n", tests[i].id, retval2string(ret));
                else
                        printf("[OK] %s\n", tests[i].id);