From 5ac5ebfe19618f303c2bb4f0f9295baba0299961 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 8 Feb 2022 11:08:16 +0100 Subject: [PATCH] test: add test for the loop --- test/meson.build | 8 ++++ test/test-loop.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 test/test-loop.c diff --git a/test/meson.build b/test/meson.build index 20bdcb210..74908d587 100644 --- a/test/meson.build +++ b/test/meson.build @@ -75,6 +75,14 @@ test('test-client', link_with: pwtest_lib) ) +test('test-loop', + executable('test-loop', + 'test-loop.c', + include_directories: pwtest_inc, + dependencies: [ spa_dep ], + link_with: pwtest_lib) +) + test('test-context', executable('test-context', 'test-context.c', diff --git a/test/test-loop.c b/test/test-loop.c new file mode 100644 index 000000000..32c5c0a0d --- /dev/null +++ b/test/test-loop.c @@ -0,0 +1,104 @@ +/* PipeWire + * + * Copyright © 2022 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "pwtest.h" + +#include + +struct obj { + int x; + struct spa_source source; +}; + +struct data { + struct pw_main_loop *ml; + struct pw_loop *l; + struct obj *a, *b; +}; + +static void on_event(struct spa_source *source) +{ + struct data *d = source->data; + + pw_loop_remove_source(d->l, &d->a->source); + pw_loop_remove_source(d->l, &d->b->source); + close(d->a->source.fd); + close(d->b->source.fd); + free(d->a); + free(d->b); + + pw_main_loop_quit(d->ml); +} + + +PWTEST(pwtest_loop_destroy2) +{ + struct data data; + + pw_init(0, NULL); + + data.ml = pw_main_loop_new(NULL); + pwtest_ptr_notnull(data.ml); + + data.l = pw_main_loop_get_loop(data.ml); + pwtest_ptr_notnull(data.l); + + data.a = calloc(1, sizeof(*data.a)); + data.b = calloc(1, sizeof(*data.b)); + + data.a->source.func = on_event; + data.a->source.fd = eventfd(0, 0); + data.a->source.mask = SPA_IO_IN; + data.a->source.data = &data; + data.b->source.func = on_event; + data.b->source.fd = eventfd(0, 0); + data.b->source.mask = SPA_IO_IN; + data.b->source.data = &data; + + pw_loop_add_source(data.l, &data.a->source); + pw_loop_add_source(data.l, &data.b->source); + + write(data.a->source.fd, &(uint64_t){1}, sizeof(uint64_t)); + write(data.b->source.fd, &(uint64_t){1}, sizeof(uint64_t)); + + pw_main_loop_run(data.ml); + pw_main_loop_destroy(data.ml); + + pw_deinit(); + + return PWTEST_PASS; +} + +PWTEST_SUITE(support) +{ + pwtest_add(pwtest_loop_destroy2, PWTEST_NOARG); + + return PWTEST_PASS; +}