--- /dev/null
+Return-Path: <davrieb@liegesta.at>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 1A8FA429E2E\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Nov 2011 04:23:20 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 3stAXfEVxf87 for <notmuch@notmuchmail.org>;\r
+ Sun, 6 Nov 2011 04:23:18 -0800 (PST)\r
+Received: from plum.liegesta.at (plum.liegesta.at [83.169.17.237])\r
+ by olra.theworths.org (Postfix) with ESMTP id 8BA82429E2F\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Nov 2011 04:23:12 -0800 (PST)\r
+Received: from ambiguous-mid.liegesta.at (mk046207255168.a1.net\r
+ [46.207.255.168]) (Authenticated sender: davrieb)\r
+ by plum.liegesta.at (Postfix) with ESMTPA id 0729527B8230;\r
+ Sun, 6 Nov 2011 13:23:11 +0100 (CET)\r
+Received: by ambiguous-mid.liegesta.at (Postfix, from userid 1000)\r
+ id D97FA1FF32; Sun, 6 Nov 2011 12:23:07 +0100 (CET)\r
+From: David Riebenbauer <davrieb@liegesta.at>\r
+To: Notmuch Mailing List <notmuch@notmuchmail.org>\r
+Subject: [PATCH 3/3] notmuch-deliver: Convert test program to glib main loop.\r
+Date: Sun, 6 Nov 2011 12:23:04 +0100\r
+Message-Id: <1320578584-6642-4-git-send-email-davrieb@liegesta.at>\r
+X-Mailer: git-send-email 1.7.7.1\r
+In-Reply-To: <1320578584-6642-1-git-send-email-davrieb@liegesta.at>\r
+References: <1320578584-6642-1-git-send-email-davrieb@liegesta.at>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 06 Nov 2011 12:23:20 -0000\r
+\r
+This makes reporting of success/failure more robust.\r
+---\r
+ contrib/notmuch-deliver/test/notmuch-lock.c | 103 +++++++++++++++++++++------\r
+ 1 files changed, 81 insertions(+), 22 deletions(-)\r
+\r
+diff --git a/contrib/notmuch-deliver/test/notmuch-lock.c b/contrib/notmuch-deliver/test/notmuch-lock.c\r
+index 2303843..78e57b6 100644\r
+--- a/contrib/notmuch-deliver/test/notmuch-lock.c\r
++++ b/contrib/notmuch-deliver/test/notmuch-lock.c\r
+@@ -14,6 +14,19 @@\r
+ #define MAX_UWAIT 600000000 // 10 minutes\r
+ \r
+ \r
++typedef struct ChildFinishedData_\r
++{\r
++ GMainLoop *main_loop;\r
++ int return_val;\r
++} ChildFinishedData;\r
++\r
++typedef struct SpawnChildData_\r
++{\r
++ char** new_argv;\r
++ ChildFinishedData *child_finidshed_data;\r
++ GMainLoop *main_loop;\r
++ int return_val;\r
++} SpawnChildData;\r
+ \r
+ static gint sleep_option = -1;\r
+ static GOptionEntry entries[] = {\r
+@@ -88,6 +101,65 @@ get_db_path(void)\r
+ return db_path;\r
+ }\r
+ \r
++static void\r
++child_finished_cb(GPid pid, gint status, gpointer data)\r
++{\r
++ ChildFinishedData *child_finidshed_data = (ChildFinishedData *)data;\r
++\r
++ g_printerr("Called child_finished_cb()\n");\r
++\r
++ if WIFEXITED(status) {\r
++ child_finidshed_data->return_val = WEXITSTATUS(status);\r
++ g_printerr("PID %d exited normally with exit code %d\n", pid, WEXITSTATUS(status));\r
++ }\r
++\r
++ g_main_loop_quit(child_finidshed_data->main_loop);\r
++}\r
++\r
++\r
++static gboolean\r
++spawn_child_cb(gpointer data)\r
++{\r
++ SpawnChildData *spawn_child_data = (SpawnChildData *)data;\r
++\r
++ g_printerr("Called spawn_child_cb()\n");\r
++\r
++ gboolean spawn_success = FALSE;\r
++ GPid child_pid = 0;\r
++ spawn_success = g_spawn_async(\r
++ g_get_current_dir(),\r
++ spawn_child_data->new_argv,\r
++ NULL,\r
++ G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN | G_SPAWN_DO_NOT_REAP_CHILD,\r
++ NULL,\r
++ NULL,\r
++ &child_pid,\r
++ NULL\r
++ );\r
++ if (spawn_success) {\r
++ g_child_watch_add(child_pid, &child_finished_cb, spawn_child_data->child_finidshed_data);\r
++ }\r
++ else {\r
++ g_printerr("faild to spawn child\n");\r
++ }\r
++\r
++\r
++ return FALSE;\r
++}\r
++\r
++static gboolean\r
++close_db_cb(gpointer data)\r
++{\r
++ notmuch_database_t *db = (notmuch_database_t*)data;\r
++\r
++ g_printerr("Called close_db_cb()\n");\r
++\r
++ // Close database again\r
++ notmuch_database_close(db);\r
++\r
++ return FALSE;\r
++}\r
++\r
+ \r
+ int\r
+ main(int argc, char *argv[])\r
+@@ -132,22 +204,6 @@ main(int argc, char *argv[])\r
+ if (db == NULL)\r
+ return EXIT_FAILURE;\r
+ \r
+- gboolean spawn_success = FALSE;\r
+- GPid child_pid = 0;\r
+- spawn_success = g_spawn_async(\r
+- g_get_current_dir(),\r
+- new_argv,\r
+- NULL,\r
+- G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN | G_SPAWN_DO_NOT_REAP_CHILD,\r
+- NULL,\r
+- NULL,\r
+- &child_pid,\r
+- NULL\r
+- );\r
+- if (!spawn_success) {\r
+- g_printerr("faild to spawn child\n");\r
+- }\r
+-\r
+ // Sleep for some time\r
+ if (sleep_option >= 0) {\r
+ sleep_time = sleep_option;\r
+@@ -155,13 +211,16 @@ main(int argc, char *argv[])\r
+ sleep_time = g_random_int_range(MIN_UWAIT, MAX_UWAIT);\r
+ }\r
+ g_printerr("Sleeping for %f secs\n", ((double)sleep_time)/(1000*1000));\r
+- usleep(sleep_time);\r
+ \r
+- // Close database again\r
+- notmuch_database_close(db);\r
++ GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);\r
++\r
++ ChildFinishedData child_finidshed_data = {main_loop, 0};\r
++ SpawnChildData spawn_child_data = {new_argv, &child_finidshed_data, main_loop, 0};\r
++\r
++ g_idle_add(&spawn_child_cb, &spawn_child_data);\r
++ g_timeout_add(sleep_time / 1000, &close_db_cb, db);\r
++ g_main_loop_run(main_loop);\r
+ \r
+- int child_status = 0;\r
+- child_pid = waitpid(child_pid, &child_status, 0);\r
+ \r
+- return WEXITSTATUS(child_status);\r
++ return child_finidshed_data.return_val;\r
+ }\r
+-- \r
+1.7.7.1\r
+\r