Re: [PATCH v3 3/4] tests: add a test for event loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2016-01-13 at 11:39 +0000, Frediano Ziglio wrote:
> Signed-off-by: Frediano Ziglio <fziglio@xxxxxxxxxx>
> ---
>  server/tests/Makefile.am        |   7 +++
>  server/tests/basic_event_loop.c |   7 +++
>  server/tests/basic_event_loop.h |   1 +
>  server/tests/test_loop.c        | 134
> ++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 149 insertions(+)
>  create mode 100644 server/tests/test_loop.c
> 
> diff --git a/server/tests/Makefile.am b/server/tests/Makefile.am
> index 4532d78..3e3b410 100644
> --- a/server/tests/Makefile.am
> +++ b/server/tests/Makefile.am
> @@ -43,12 +43,14 @@ noinst_PROGRAMS =				\
>  	test_vdagent				\
>  	test_display_width_stride		\
>  	spice-server-replay			\
> +	test_loop				\
>  	stat_test \
>  	$(NULL)
>  
>  TESTS =	\
>  	stat_test				\
>  	test-qxl-parsing			\
> +	test_loop				\
>  	$(NULL)
>  
>  check_PROGRAMS = $(TESTS)
> @@ -152,3 +154,8 @@ test_qxl_parsing_SOURCES =           \
>  	../red-parse-qxl.c      \
>  	../memslot.c            \
>  	$(NULL)
> +
> +test_loop_SOURCES =				\
> +	$(COMMON_BASE)				\
> +	test_loop.c				\
> +	$(NULL)
> diff --git a/server/tests/basic_event_loop.c b/server/tests/basic_event_loop.c
> index 0eac194..ef680ee 100644
> --- a/server/tests/basic_event_loop.c
> +++ b/server/tests/basic_event_loop.c
> @@ -104,3 +104,10 @@ SpiceCoreInterface *basic_event_loop_init(void)
>      core.watch_remove = event_loop_core.watch_remove;
>      return &core;
>  }
> +
> +void basic_event_loop_destroy(void)
> +{
> +    spice_assert(main_context != NULL);
> +    g_main_context_unref(main_context);
> +    main_context = NULL;
> +}
> diff --git a/server/tests/basic_event_loop.h b/server/tests/basic_event_loop.h
> index 2ec9446..593532b 100644
> --- a/server/tests/basic_event_loop.h
> +++ b/server/tests/basic_event_loop.h
> @@ -23,6 +23,7 @@
>  
>  GMainContext *basic_event_loop_get_context(void);
>  SpiceCoreInterface *basic_event_loop_init(void);
> +void basic_event_loop_destroy(void);
>  void basic_event_loop_mainloop(void);
>  
>  #endif // __BASIC_EVENT_LOOP_H__
> diff --git a/server/tests/test_loop.c b/server/tests/test_loop.c
> new file mode 100644
> index 0000000..1dc3923
> --- /dev/null
> +++ b/server/tests/test_loop.c
> @@ -0,0 +1,134 @@
> +/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
> +/*
> +   Copyright (C) 2015 Red Hat, Inc.
> +
> +   This library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   This library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with this library; if not, see <http://www.gnu.org/licenses/
> >.
> +*/
> +
> +/* Test event loop
> + */
> +
> +#include <config.h>
> +
> +#include <string.h>
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <signal.h>
> +#include <unistd.h>
> +#include <pthread.h>
> +#include <glib.h>
> +
> +#include <spice/macros.h>
> +#include <common/log.h>
> +#include "basic_event_loop.h"
> +
> +static SpiceCoreInterface *core = NULL;
> +static GMainLoop *loop = NULL;
> +static pthread_t loop_thread;
> +
> +static void timer_err(void *opaque)
> +{
> +    spice_assert(0);
> +}
> +
> +static SpiceTimer *to_delete_timer = NULL;
> +static void timer_del(void *opaque)
> +{
> +    spice_assert(core);
> +    spice_assert(to_delete_timer);
> +
> +    spice_assert(pthread_equal(loop_thread, pthread_self()));
> +
> +    core->timer_remove(to_delete_timer);
> +    to_delete_timer = NULL;
> +}
> +
> +static void timer_exit(void *opaque)
> +{
> +    spice_assert(loop);
> +
> +    spice_assert(pthread_equal(loop_thread, pthread_self()));
> +
> +    g_main_loop_quit(loop);
> +}
> +
> +static void *loop_func(void *arg)
> +{
> +    loop_thread = pthread_self();
> +
> +    spice_assert(loop);
> +
> +    g_main_loop_run(loop);
> +
> +    return NULL;
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    SpiceTimer *timer, *timers[10];
> +    int i, rc;
> +
> +    memset(timers, 0, sizeof(timers));
> +
> +    core = basic_event_loop_init();
> +
> +    i = 0;
> +
> +    /* add a timer and delete to check is correctly deleted */
> +    timer = core->timer_add(timer_err, NULL);
> +    core->timer_start(timer, 1);
> +    core->timer_remove(timer);
> +
> +    /* create timer, should not be executed */
> +    timer = timers[i++] = core->timer_add(timer_err, NULL);
> +
> +    /* add a timer and cancel to check is not executed */
> +    timer = timers[i++] = core->timer_add(timer_err, NULL);
> +    core->timer_start(timer, 1);
> +    core->timer_cancel(timer);
> +
> +    /* check we can remove timer inside a timer */
> +    timer = to_delete_timer = core->timer_add(timer_del, NULL);
> +    spice_assert(to_delete_timer != NULL);
> +    core->timer_start(timer, 1);
> +
> +    /* create a timer that does something */
> +    timer = timers[i++] = core->timer_add(timer_exit, NULL);
> +    core->timer_start(timer, 10);
> +
> +    /* run the loop */
> +    loop = g_main_loop_new(basic_event_loop_get_context(), FALSE);
> +    alarm(1);
> +    rc = pthread_create(&loop_thread, NULL, loop_func, NULL);
> +    spice_assert(rc == 0);
> +    rc = pthread_join(loop_thread, NULL);
> +    spice_assert(rc == 0);
> +    alarm(0);
> +    g_main_loop_unref(loop);
> +
> +    /* delete executed ? */
> +    spice_assert(to_delete_timer == NULL);
> +
> +    /* cleanup */
> +    for (i = 0; i < G_N_ELEMENTS(timers); ++i) {
> +        if (timers[i]) {
> +            core->timer_remove(timers[i]);
> +            timers[i] = NULL;
> +        }
> +    }
> +
> +    basic_event_loop_destroy();
> +
> +    return 0;
> +}


Acked-by: Jonathon Jongsma <jjongsma@xxxxxxxxxx>
_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
http://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]