aboutsummaryrefslogtreecommitdiff
path: root/tests/vhost-user-test.c
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2019-02-14 18:35:54 +0100
committerMichael S. Tsirkin <mst@redhat.com>2019-02-21 12:28:01 -0500
commit7d0ca3e747ce3b595fcae554b04fccbffd0c04f0 (patch)
treeea6a22db84a0344756a824fcc91dd72fb9b448d8 /tests/vhost-user-test.c
parent299e6f19b3e285f3ab00b780e3b48f5e58b5d4ed (diff)
downloadqemu-7d0ca3e747ce3b595fcae554b04fccbffd0c04f0.zip
qemu-7d0ca3e747ce3b595fcae554b04fccbffd0c04f0.tar.gz
qemu-7d0ca3e747ce3b595fcae554b04fccbffd0c04f0.tar.bz2
vhost-user-test: create a main loop per TestServer
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 <marcandre.lureau@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Message-Id: <1543851204-41186-13-git-send-email-pbonzini@redhat.com> Message-Id: <1550165756-21617-8-git-send-email-pbonzini@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Diffstat (limited to 'tests/vhost-user-test.c')
-rw-r--r--tests/vhost-user-test.c53
1 files changed, 27 insertions, 26 deletions
diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index cdbdf3d..527d3b3 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,17 @@ 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);
+ }
+
qemu_chr_fe_deinit(&server->chr, true);
for (i = 0; i < server->fds_num; i++) {
@@ -562,14 +578,9 @@ static gboolean _test_server_free(TestServer *server)
g_assert(server->bus);
qpci_free_pc(server->bus);
+ g_main_loop_unref(server->loop);
+ g_main_context_unref(server->context);
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",