[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 7/9] vhost-user-test: create a main loop per TestSer
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PATCH 7/9] vhost-user-test: create a main loop per TestServer |
Date: |
Thu, 14 Feb 2019 18:35:54 +0100 |
This makes the tests more independent and removes the need to defer
test_server_free
via an idle event source.
Reviewed-by: Marc-André Lureau <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
Message-Id: <address@hidden>
---
tests/vhost-user-test.c | 53 +++++++++++++++++++++++++------------------------
1 file changed, 27 insertions(+), 26 deletions(-)
diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index cdbdf3d..33030e0 100644
--- a/tests/vhost-user-test.c
+++ b/tests/vhost-user-test.c
@@ -146,6 +146,9 @@ typedef struct TestServer {
int fds_num;
int fds[VHOST_MEMORY_MAX_NREGIONS];
VhostUserMemory memory;
+ GMainContext *context;
+ GMainLoop *loop;
+ GThread *thread;
GMutex data_mutex;
GCond data_cond;
int log_fd;
@@ -495,6 +498,12 @@ static TestServer *test_server_new(const gchar *name)
{
TestServer *server = g_new0(TestServer, 1);
+ server->context = g_main_context_new();
+ server->loop = g_main_loop_new(server->context, FALSE);
+
+ /* run the main loop thread so the chardev may operate */
+ server->thread = g_thread_new(NULL, thread_function, server->loop);
+
server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
server->chr_name = g_strdup_printf("chr-%s", name);
@@ -524,13 +533,13 @@ static void test_server_create_chr(TestServer *server,
const gchar *opt)
Chardev *chr;
chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
- chr = qemu_chr_new(server->chr_name, chr_path, NULL);
+ chr = qemu_chr_new(server->chr_name, chr_path, server->context);
g_free(chr_path);
g_assert_nonnull(chr);
qemu_chr_fe_init(&server->chr, chr, &error_abort);
qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
- chr_event, NULL, server, NULL, true);
+ chr_event, NULL, server, server->context, true);
}
static void test_server_listen(TestServer *server)
@@ -538,10 +547,19 @@ static void test_server_listen(TestServer *server)
test_server_create_chr(server, ",server,nowait");
}
-static gboolean _test_server_free(TestServer *server)
+static void test_server_free(TestServer *server)
{
int i;
+ /* finish the helper thread and dispatch pending sources */
+ g_main_loop_quit(server->loop);
+ g_thread_join(server->thread);
+ while (g_main_context_pending(NULL)) {
+ g_main_context_iteration(NULL, TRUE);
+ }
+ g_main_loop_unref(server->loop);
+ g_main_context_unref(server->context);
+
qemu_chr_fe_deinit(&server->chr, true);
for (i = 0; i < server->fds_num; i++) {
@@ -563,13 +581,6 @@ static gboolean _test_server_free(TestServer *server)
qpci_free_pc(server->bus);
g_free(server);
-
- return FALSE;
-}
-
-static void test_server_free(TestServer *server)
-{
- g_idle_add((GSourceFunc)_test_server_free, server);
}
static void wait_for_log_fd(TestServer *s)
@@ -728,7 +739,7 @@ static void test_migrate(void)
sizeof(TestMigrateSource));
((TestMigrateSource *)source)->src = s;
((TestMigrateSource *)source)->dest = dest;
- g_source_attach(source, NULL);
+ g_source_attach(source, s->context);
/* slow down migration to have time to fiddle with log */
/* TODO: qtest could learn to break on some places */
@@ -825,6 +836,7 @@ connect_thread(gpointer data)
static void test_reconnect_subprocess(void)
{
TestServer *s = test_server_new("reconnect");
+ GSource *src;
char *cmd;
g_thread_new("connect", connect_thread, s);
@@ -842,7 +854,10 @@ static void test_reconnect_subprocess(void)
/* reconnect */
s->fds_num = 0;
s->rings = 0;
- g_idle_add(reconnect_cb, s);
+ src = g_idle_source_new();
+ g_source_set_callback(src, reconnect_cb, s, NULL);
+ g_source_attach(src, s->context);
+ g_source_unref(src);
g_assert(wait_for_fds(s));
wait_for_rings_started(s, 2);
@@ -974,8 +989,6 @@ int main(int argc, char **argv)
const char *hugefs;
int ret;
char template[] = "/tmp/vhost-test-XXXXXX";
- GMainLoop *loop;
- GThread *thread;
g_test_init(&argc, &argv, NULL);
@@ -997,10 +1010,6 @@ int main(int argc, char **argv)
}
#endif
- loop = g_main_loop_new(NULL, FALSE);
- /* run the main loop thread so the chardev may operate */
- thread = g_thread_new(NULL, thread_function, loop);
-
if (qemu_memfd_check(0)) {
qtest_add_data_func("/vhost-user/read-guest-mem/memfd",
GINT_TO_POINTER(TEST_MEMFD_YES),
@@ -1028,14 +1037,6 @@ int main(int argc, char **argv)
/* cleanup */
- /* finish the helper thread and dispatch pending sources */
- g_main_loop_quit(loop);
- g_thread_join(thread);
- while (g_main_context_pending(NULL)) {
- g_main_context_iteration (NULL, TRUE);
- }
- g_main_loop_unref(loop);
-
ret = rmdir(tmpfs);
if (ret != 0) {
g_test_message("unable to rmdir: path (%s): %s\n",
--
1.8.3.1
- [Qemu-devel] [PATCH v2 0/9] vhost: enable for all targets, Paolo Bonzini, 2019/02/14
- [Qemu-devel] [PATCH 3/9] vhost: restrict Linux dependency to kernel vhost, Paolo Bonzini, 2019/02/14
- [Qemu-devel] [PATCH 4/9] vhost-user: support cross-endian vnet headers, Paolo Bonzini, 2019/02/14
- [Qemu-devel] [PATCH 9/9] vhost-user-test: create a temporary directory per TestServer, Paolo Bonzini, 2019/02/14
- [Qemu-devel] [PATCH 8/9] vhost-user-test: small changes to init_hugepagefs, Paolo Bonzini, 2019/02/14
- [Qemu-devel] [PATCH 2/9] vhost-net-user: add stubs for when no virtio-net device is present, Paolo Bonzini, 2019/02/14
- [Qemu-devel] [PATCH 6/9] vhost-net: revamp configure logic, Paolo Bonzini, 2019/02/14
- [Qemu-devel] [PATCH 5/9] vhost-net: compile it on all targets that have virtio-net., Paolo Bonzini, 2019/02/14
- [Qemu-devel] [PATCH 1/9] vhost-net: move stubs to a separate file, Paolo Bonzini, 2019/02/14
- [Qemu-devel] [PATCH 7/9] vhost-user-test: create a main loop per TestServer,
Paolo Bonzini <=
- Re: [Qemu-devel] [PATCH v2 0/9] vhost: enable for all targets, Michael S. Tsirkin, 2019/02/14
- Re: [Qemu-devel] [PATCH v2 0/9] vhost: enable for all targets, Michael S. Tsirkin, 2019/02/14