[10/14] tests: make the test context persistent

Submitted by Nobuhiko Tanibata on June 22, 2015, 6:35 a.m.

Details

Message ID 1434954949-27520-1-git-send-email-nobuhiko_tanibata@xddp.denso.co.jp
State Accepted
Commit a10352e5c3418f9539cb53f4aecc537d2b412a4b
Headers show

Not browsing as part of any series.

Commit Message

Nobuhiko Tanibata June 22, 2015, 6:35 a.m.
From: Nobuhiko Tanibata <NOBUHIKO_TANIBATA@xddp.denso.co.jp>

The TESTs in ivi_layout-test.c may have several server-side parts
(RUNNER_TEST in ivi_layout-test-plugin.c) each. Sometimes we need to
carry state from one RUNNER_TEST to another within one TEST, but not
across multiple TESTs. The correct lifetime of that state would be the
lifetime (and identity) of the runner_resource, as one TEST creates and
uses at most one weston_test_runner during its lifetime.

However, tests are executed one by one. Take a shortcut, and use a static
global for storing that state. This turns the test_context into a
singleton. To ensure it is not confused between multiple TESTs, add
asserts to verify its identity.

Following patches will add tests for notification callbacks. These will
be using the carried state.

[Pekka: add serialization checks, rename the global, rewrite commit message.]
Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
---
 tests/ivi_layout-test-plugin.c | 45 ++++++++++++++++++++++++++++++++----------
 1 file changed, 35 insertions(+), 10 deletions(-)

Patch hide | download patch | download mbox

diff --git a/tests/ivi_layout-test-plugin.c b/tests/ivi_layout-test-plugin.c
index d24c9a1..a134a14 100644
--- a/tests/ivi_layout-test-plugin.c
+++ b/tests/ivi_layout-test-plugin.c
@@ -30,6 +30,7 @@ 
 #include <unistd.h>
 #include <signal.h>
 #include <string.h>
+#include <assert.h>
 
 #include "src/compositor.h"
 #include "weston-test-server-protocol.h"
@@ -78,28 +79,42 @@  struct test_launcher {
 	const struct ivi_controller_interface *controller_interface;
 };
 
+struct test_context {
+	const struct ivi_controller_interface *controller_interface;
+	struct wl_resource *runner_resource;
+};
+
+static struct test_context static_context;
+
+static void
+destroy_runner(struct wl_resource *resource)
+{
+	assert(static_context.runner_resource == NULL ||
+	       static_context.runner_resource == resource);
+
+	static_context.controller_interface = NULL;
+	static_context.runner_resource = NULL;
+}
+
 static void
 runner_destroy_handler(struct wl_client *client, struct wl_resource *resource)
 {
 	wl_resource_destroy(resource);
 }
 
-struct test_context {
-	const struct ivi_controller_interface *controller_interface;
-	struct wl_resource *runner_resource;
-};
-
 static void
 runner_run_handler(struct wl_client *client, struct wl_resource *resource,
 		   const char *test_name)
 {
 	struct test_launcher *launcher;
 	const struct runner_test *t;
-	struct test_context ctx;
+
+	assert(static_context.runner_resource == NULL ||
+	       static_context.runner_resource == resource);
 
 	launcher = wl_resource_get_user_data(resource);
-	ctx.controller_interface = launcher->controller_interface;
-	ctx.runner_resource = resource;
+	static_context.controller_interface = launcher->controller_interface;
+	static_context.runner_resource = resource;
 
 	t = find_runner_test(test_name);
 	if (!t) {
@@ -114,7 +129,7 @@  runner_run_handler(struct wl_client *client, struct wl_resource *resource,
 
 	weston_log("weston_test_runner.run(\"%s\")\n", test_name);
 
-	t->run(&ctx);
+	t->run(&static_context);
 
 	weston_test_runner_send_finished(resource);
 }
@@ -139,7 +154,15 @@  bind_runner(struct wl_client *client, void *data,
 	}
 
 	wl_resource_set_implementation(resource, &runner_implementation,
-				       launcher, NULL);
+				       launcher, destroy_runner);
+
+	if (static_context.runner_resource != NULL) {
+		weston_log("test FATAL: "
+			   "attempting to run several tests in parallel.\n");
+		wl_resource_post_error(resource,
+				       WESTON_TEST_RUNNER_ERROR_TEST_FAILED,
+				       "attempt to run parallel tests");
+	}
 }
 
 static void
@@ -240,6 +263,8 @@  runner_assert_fail(const char *cond, const char *file, int line,
 {
 	weston_log("Assert failure in %s:%d, %s: '%s'\n",
 		   file, line, func, cond);
+
+	assert(ctx->runner_resource);
 	wl_resource_post_error(ctx->runner_resource,
 			       WESTON_TEST_RUNNER_ERROR_TEST_FAILED,
 			       "Assert failure in %s:%d, %s: '%s'\n",

Comments

On 06/21/2015 11:35 PM, Nobuhiko Tanibata wrote:
> From: Nobuhiko Tanibata <NOBUHIKO_TANIBATA@xddp.denso.co.jp>
> 
> The TESTs in ivi_layout-test.c may have several server-side parts
> (RUNNER_TEST in ivi_layout-test-plugin.c) each. Sometimes we need to
> carry state from one RUNNER_TEST to another within one TEST, but not
> across multiple TESTs. The correct lifetime of that state would be the
> lifetime (and identity) of the runner_resource, as one TEST creates and
> uses at most one weston_test_runner during its lifetime.
> 
> However, tests are executed one by one. Take a shortcut, and use a static
> global for storing that state. This turns the test_context into a
> singleton. To ensure it is not confused between multiple TESTs, add
> asserts to verify its identity.
> 
> Following patches will add tests for notification callbacks. These will
> be using the carried state.
> 
> [Pekka: add serialization checks, rename the global, rewrite commit message.]
> Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>

Looks good.

Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>

> ---
>  tests/ivi_layout-test-plugin.c | 45 ++++++++++++++++++++++++++++++++----------
>  1 file changed, 35 insertions(+), 10 deletions(-)
> 
> diff --git a/tests/ivi_layout-test-plugin.c b/tests/ivi_layout-test-plugin.c
> index d24c9a1..a134a14 100644
> --- a/tests/ivi_layout-test-plugin.c
> +++ b/tests/ivi_layout-test-plugin.c
> @@ -30,6 +30,7 @@
>  #include <unistd.h>
>  #include <signal.h>
>  #include <string.h>
> +#include <assert.h>
>  
>  #include "src/compositor.h"
>  #include "weston-test-server-protocol.h"
> @@ -78,28 +79,42 @@ struct test_launcher {
>  	const struct ivi_controller_interface *controller_interface;
>  };
>  
> +struct test_context {
> +	const struct ivi_controller_interface *controller_interface;
> +	struct wl_resource *runner_resource;
> +};
> +
> +static struct test_context static_context;
> +
> +static void
> +destroy_runner(struct wl_resource *resource)
> +{
> +	assert(static_context.runner_resource == NULL ||
> +	       static_context.runner_resource == resource);
> +
> +	static_context.controller_interface = NULL;
> +	static_context.runner_resource = NULL;
> +}
> +
>  static void
>  runner_destroy_handler(struct wl_client *client, struct wl_resource *resource)
>  {
>  	wl_resource_destroy(resource);
>  }
>  
> -struct test_context {
> -	const struct ivi_controller_interface *controller_interface;
> -	struct wl_resource *runner_resource;
> -};
> -
>  static void
>  runner_run_handler(struct wl_client *client, struct wl_resource *resource,
>  		   const char *test_name)
>  {
>  	struct test_launcher *launcher;
>  	const struct runner_test *t;
> -	struct test_context ctx;
> +
> +	assert(static_context.runner_resource == NULL ||
> +	       static_context.runner_resource == resource);
>  
>  	launcher = wl_resource_get_user_data(resource);
> -	ctx.controller_interface = launcher->controller_interface;
> -	ctx.runner_resource = resource;
> +	static_context.controller_interface = launcher->controller_interface;
> +	static_context.runner_resource = resource;
>  
>  	t = find_runner_test(test_name);
>  	if (!t) {
> @@ -114,7 +129,7 @@ runner_run_handler(struct wl_client *client, struct wl_resource *resource,
>  
>  	weston_log("weston_test_runner.run(\"%s\")\n", test_name);
>  
> -	t->run(&ctx);
> +	t->run(&static_context);
>  
>  	weston_test_runner_send_finished(resource);
>  }
> @@ -139,7 +154,15 @@ bind_runner(struct wl_client *client, void *data,
>  	}
>  
>  	wl_resource_set_implementation(resource, &runner_implementation,
> -				       launcher, NULL);
> +				       launcher, destroy_runner);
> +
> +	if (static_context.runner_resource != NULL) {
> +		weston_log("test FATAL: "
> +			   "attempting to run several tests in parallel.\n");
> +		wl_resource_post_error(resource,
> +				       WESTON_TEST_RUNNER_ERROR_TEST_FAILED,
> +				       "attempt to run parallel tests");
> +	}
>  }
>  
>  static void
> @@ -240,6 +263,8 @@ runner_assert_fail(const char *cond, const char *file, int line,
>  {
>  	weston_log("Assert failure in %s:%d, %s: '%s'\n",
>  		   file, line, func, cond);
> +
> +	assert(ctx->runner_resource);
>  	wl_resource_post_error(ctx->runner_resource,
>  			       WESTON_TEST_RUNNER_ERROR_TEST_FAILED,
>  			       "Assert failure in %s:%d, %s: '%s'\n",
>