From: David Riebenbauer <davrieb@liegesta.at>
To: Notmuch Mailing List <notmuch@notmuchmail.org>
Subject: [PATCH 3/3] notmuch-deliver: Convert test program to glib main loop.
Date: Sun, 6 Nov 2011 12:23:04 +0100 [thread overview]
Message-ID: <1320578584-6642-4-git-send-email-davrieb@liegesta.at> (raw)
In-Reply-To: <1320578584-6642-1-git-send-email-davrieb@liegesta.at>
This makes reporting of success/failure more robust.
---
contrib/notmuch-deliver/test/notmuch-lock.c | 103 +++++++++++++++++++++------
1 files changed, 81 insertions(+), 22 deletions(-)
diff --git a/contrib/notmuch-deliver/test/notmuch-lock.c b/contrib/notmuch-deliver/test/notmuch-lock.c
index 2303843..78e57b6 100644
--- a/contrib/notmuch-deliver/test/notmuch-lock.c
+++ b/contrib/notmuch-deliver/test/notmuch-lock.c
@@ -14,6 +14,19 @@
#define MAX_UWAIT 600000000 // 10 minutes
+typedef struct ChildFinishedData_
+{
+ GMainLoop *main_loop;
+ int return_val;
+} ChildFinishedData;
+
+typedef struct SpawnChildData_
+{
+ char** new_argv;
+ ChildFinishedData *child_finidshed_data;
+ GMainLoop *main_loop;
+ int return_val;
+} SpawnChildData;
static gint sleep_option = -1;
static GOptionEntry entries[] = {
@@ -88,6 +101,65 @@ get_db_path(void)
return db_path;
}
+static void
+child_finished_cb(GPid pid, gint status, gpointer data)
+{
+ ChildFinishedData *child_finidshed_data = (ChildFinishedData *)data;
+
+ g_printerr("Called child_finished_cb()\n");
+
+ if WIFEXITED(status) {
+ child_finidshed_data->return_val = WEXITSTATUS(status);
+ g_printerr("PID %d exited normally with exit code %d\n", pid, WEXITSTATUS(status));
+ }
+
+ g_main_loop_quit(child_finidshed_data->main_loop);
+}
+
+
+static gboolean
+spawn_child_cb(gpointer data)
+{
+ SpawnChildData *spawn_child_data = (SpawnChildData *)data;
+
+ g_printerr("Called spawn_child_cb()\n");
+
+ gboolean spawn_success = FALSE;
+ GPid child_pid = 0;
+ spawn_success = g_spawn_async(
+ g_get_current_dir(),
+ spawn_child_data->new_argv,
+ NULL,
+ G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN | G_SPAWN_DO_NOT_REAP_CHILD,
+ NULL,
+ NULL,
+ &child_pid,
+ NULL
+ );
+ if (spawn_success) {
+ g_child_watch_add(child_pid, &child_finished_cb, spawn_child_data->child_finidshed_data);
+ }
+ else {
+ g_printerr("faild to spawn child\n");
+ }
+
+
+ return FALSE;
+}
+
+static gboolean
+close_db_cb(gpointer data)
+{
+ notmuch_database_t *db = (notmuch_database_t*)data;
+
+ g_printerr("Called close_db_cb()\n");
+
+ // Close database again
+ notmuch_database_close(db);
+
+ return FALSE;
+}
+
int
main(int argc, char *argv[])
@@ -132,22 +204,6 @@ main(int argc, char *argv[])
if (db == NULL)
return EXIT_FAILURE;
- gboolean spawn_success = FALSE;
- GPid child_pid = 0;
- spawn_success = g_spawn_async(
- g_get_current_dir(),
- new_argv,
- NULL,
- G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN | G_SPAWN_DO_NOT_REAP_CHILD,
- NULL,
- NULL,
- &child_pid,
- NULL
- );
- if (!spawn_success) {
- g_printerr("faild to spawn child\n");
- }
-
// Sleep for some time
if (sleep_option >= 0) {
sleep_time = sleep_option;
@@ -155,13 +211,16 @@ main(int argc, char *argv[])
sleep_time = g_random_int_range(MIN_UWAIT, MAX_UWAIT);
}
g_printerr("Sleeping for %f secs\n", ((double)sleep_time)/(1000*1000));
- usleep(sleep_time);
- // Close database again
- notmuch_database_close(db);
+ GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
+
+ ChildFinishedData child_finidshed_data = {main_loop, 0};
+ SpawnChildData spawn_child_data = {new_argv, &child_finidshed_data, main_loop, 0};
+
+ g_idle_add(&spawn_child_cb, &spawn_child_data);
+ g_timeout_add(sleep_time / 1000, &close_db_cb, db);
+ g_main_loop_run(main_loop);
- int child_status = 0;
- child_pid = waitpid(child_pid, &child_status, 0);
- return WEXITSTATUS(child_status);
+ return child_finidshed_data.return_val;
}
--
1.7.7.1
next prev parent reply other threads:[~2011-11-06 12:23 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-06 11:23 [PATCH 0/3] notmuch-deliver: wait for database to become unlocked and test David Riebenbauer
2011-11-06 11:23 ` [PATCH 1/3] notmuch-deliver: wait for the database to become unlocked David Riebenbauer
2011-11-06 11:23 ` [PATCH 2/3] notmuch-deliver: test wait for database to become available David Riebenbauer
2011-11-06 11:23 ` David Riebenbauer [this message]
2011-11-07 1:32 ` [PATCH 0/3] notmuch-deliver: wait for database to become unlocked and test Ali Polatel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1320578584-6642-4-git-send-email-davrieb@liegesta.at \
--to=davrieb@liegesta.at \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).