test: add test for the loop

This commit is contained in:
Wim Taymans 2022-02-08 11:08:16 +01:00
parent c474846c42
commit 5ac5ebfe19
2 changed files with 112 additions and 0 deletions

View file

@ -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',

104
test/test-loop.c Normal file
View file

@ -0,0 +1,104 @@
/* PipeWire
*
* Copyright © 2022 Wim Taymans <wim.taymans@gmail.com>
*
* 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 <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/eventfd.h>
#include "pwtest.h"
#include <pipewire/pipewire.h>
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;
}