Re: [PATCH v4 13/16] add indexopts to notmuch python bindings.
[notmuch-archives.git] / e5 / cb1a5dbc27e923281c69671fa5a01bc5c921ec
1 Return-Path: <davrieb@liegesta.at>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 3DAD5429E29\r
6         for <notmuch@notmuchmail.org>; Sun,  6 Nov 2011 04:23:16 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id wxjSVk2ZQQrN for <notmuch@notmuchmail.org>;\r
16         Sun,  6 Nov 2011 04:23:13 -0800 (PST)\r
17 Received: from plum.liegesta.at (plum.liegesta.at [83.169.17.237])\r
18         by olra.theworths.org (Postfix) with ESMTP id 0560A429E21\r
19         for <notmuch@notmuchmail.org>; Sun,  6 Nov 2011 04:23:12 -0800 (PST)\r
20 Received: from ambiguous-mid.liegesta.at (mk046207255168.a1.net\r
21         [46.207.255.168]) (Authenticated sender: davrieb)\r
22         by plum.liegesta.at (Postfix) with ESMTPA id AC10A27B825A;\r
23         Sun,  6 Nov 2011 13:23:09 +0100 (CET)\r
24 Received: by ambiguous-mid.liegesta.at (Postfix, from userid 1000)\r
25         id 94ECE1FF30; Sun,  6 Nov 2011 12:23:07 +0100 (CET)\r
26 From: David Riebenbauer <davrieb@liegesta.at>\r
27 To: Notmuch Mailing List <notmuch@notmuchmail.org>\r
28 Subject: [PATCH 2/3] notmuch-deliver: test wait for database to become\r
29         available\r
30 Date: Sun,  6 Nov 2011 12:23:03 +0100\r
31 Message-Id: <1320578584-6642-3-git-send-email-davrieb@liegesta.at>\r
32 X-Mailer: git-send-email 1.7.7.1\r
33 In-Reply-To: <1320578584-6642-1-git-send-email-davrieb@liegesta.at>\r
34 References: <1320578584-6642-1-git-send-email-davrieb@liegesta.at>\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.13\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Sun, 06 Nov 2011 12:23:17 -0000\r
48 \r
49 Check if notmuch-deliver really waits for the database to become\r
50 available again.\r
51 \r
52 This is done via a test program that locks the notmuch database, and then\r
53 spawns the program to be tested. The test can be run with make check.\r
54 ---\r
55  contrib/notmuch-deliver/.gitignore          |    2 +\r
56  contrib/notmuch-deliver/Makefile.am         |    2 +-\r
57  contrib/notmuch-deliver/configure.ac        |    1 +\r
58  contrib/notmuch-deliver/test/Makefile.am    |   26 ++++\r
59  contrib/notmuch-deliver/test/nm-test.sh     |    9 ++\r
60  contrib/notmuch-deliver/test/nm-testconfig  |   12 ++\r
61  contrib/notmuch-deliver/test/notmuch-lock.c |  167 +++++++++++++++++++++++++++\r
62  contrib/notmuch-deliver/test/testmail       |    7 +\r
63  8 files changed, 225 insertions(+), 1 deletions(-)\r
64  create mode 100644 contrib/notmuch-deliver/test/Makefile.am\r
65  create mode 100755 contrib/notmuch-deliver/test/nm-test.sh\r
66  create mode 100644 contrib/notmuch-deliver/test/nm-testconfig\r
67  create mode 100644 contrib/notmuch-deliver/test/notmuch-lock.c\r
68  create mode 100644 contrib/notmuch-deliver/test/testmail\r
69 \r
70 diff --git a/contrib/notmuch-deliver/.gitignore b/contrib/notmuch-deliver/.gitignore\r
71 index 6971ef2..91da18b 100644\r
72 --- a/contrib/notmuch-deliver/.gitignore\r
73 +++ b/contrib/notmuch-deliver/.gitignore\r
74 @@ -40,3 +40,5 @@ m4/lt*.m4\r
75  \r
76  maildrop/numlib/config.h\r
77  src/notmuch-deliver\r
78 +test/notmuch-lock\r
79 +test/nm-test-mail\r
80 diff --git a/contrib/notmuch-deliver/Makefile.am b/contrib/notmuch-deliver/Makefile.am\r
81 index 365558a..4392771 100644\r
82 --- a/contrib/notmuch-deliver/Makefile.am\r
83 +++ b/contrib/notmuch-deliver/Makefile.am\r
84 @@ -3,6 +3,6 @@ MAINTAINERCLEANFILES= Makefile.in configure aclocal.m4 \\r
85                       config.h config.h.in INSTALL\r
86  ACLOCAL_AMFLAGS= -I m4\r
87  AUTOMAKE_OPTIONS= dist-bzip2 no-dist-gzip std-options foreign\r
88 -SUBDIRS= maildrop/numlib src .\r
89 +SUBDIRS= maildrop/numlib src test .\r
90  \r
91  doc_DATA= README.mkd\r
92 diff --git a/contrib/notmuch-deliver/configure.ac b/contrib/notmuch-deliver/configure.ac\r
93 index 4deb658..693923f 100644\r
94 --- a/contrib/notmuch-deliver/configure.ac\r
95 +++ b/contrib/notmuch-deliver/configure.ac\r
96 @@ -152,5 +152,6 @@ AC_OUTPUT(\r
97                   Makefile\r
98                   maildrop/numlib/Makefile\r
99                   src/Makefile\r
100 +                 test/Makefile\r
101                   )\r
102  dnl }}}\r
103 diff --git a/contrib/notmuch-deliver/test/Makefile.am b/contrib/notmuch-deliver/test/Makefile.am\r
104 new file mode 100644\r
105 index 0000000..dfa0b45\r
106 --- /dev/null\r
107 +++ b/contrib/notmuch-deliver/test/Makefile.am\r
108 @@ -0,0 +1,26 @@\r
109 +DEFS+= -DGITHEAD=\"$(GITHEAD)\"\r
110 +AM_CFLAGS= @NOTMUCH_DELIVER_CFLAGS@ $(glib_CFLAGS)\r
111 +\r
112 +check_PROGRAMS= notmuch-lock\r
113 +\r
114 +notmuch_lock_SOURCES= notmuch-lock.c\r
115 +notmuch_lock_LDADD= $(glib_LIBS)\r
116 +\r
117 +TESTS = nm-test.sh\r
118 +\r
119 +nm-test.sh: notmuch-lock \\r
120 +            nm-test-mail/INBOX/cur \\r
121 +            nm-test-mail/INBOX/new \\r
122 +            nm-test-mail/INBOX/tmp\r
123 +\r
124 +nm-test-mail/INBOX/cur:\r
125 +       mkdir -p $@\r
126 +\r
127 +nm-test-mail/INBOX/new:\r
128 +       mkdir -p $@\r
129 +\r
130 +nm-test-mail/INBOX/tmp:\r
131 +       mkdir -p $@\r
132 +\r
133 +clean-local:\r
134 +       rm -rf nm-test-mail\r
135 diff --git a/contrib/notmuch-deliver/test/nm-test.sh b/contrib/notmuch-deliver/test/nm-test.sh\r
136 new file mode 100755\r
137 index 0000000..6113211\r
138 --- /dev/null\r
139 +++ b/contrib/notmuch-deliver/test/nm-test.sh\r
140 @@ -0,0 +1,9 @@\r
141 +#!/bin/sh\r
142 +\r
143 +set -e\r
144 +\r
145 +export NOTMUCH_CONFIG=./nm-testconfig\r
146 +\r
147 +notmuch new\r
148 +\r
149 +./notmuch-lock -s 1000000 -- ../src/notmuch-deliver -v INBOX < testmail\r
150 diff --git a/contrib/notmuch-deliver/test/nm-testconfig b/contrib/notmuch-deliver/test/nm-testconfig\r
151 new file mode 100644\r
152 index 0000000..b94e8d0\r
153 --- /dev/null\r
154 +++ b/contrib/notmuch-deliver/test/nm-testconfig\r
155 @@ -0,0 +1,12 @@\r
156 +[database]\r
157 +path=./nm-test-mail\r
158 +\r
159 +[user]\r
160 +name=NotMuch Test\r
161 +primary_email=notmuc@example.com\r
162 +\r
163 +[new]\r
164 +tags=unread;inbox;\r
165 +\r
166 +[maildir]\r
167 +synchronize_flags=true\r
168 diff --git a/contrib/notmuch-deliver/test/notmuch-lock.c b/contrib/notmuch-deliver/test/notmuch-lock.c\r
169 new file mode 100644\r
170 index 0000000..2303843\r
171 --- /dev/null\r
172 +++ b/contrib/notmuch-deliver/test/notmuch-lock.c\r
173 @@ -0,0 +1,167 @@\r
174 +/* vim: set cino=t0 fo=croql sw=4 ts=4 sts=0 noet cin fdm=syntax nolist : */\r
175 +\r
176 +#include <stdlib.h>\r
177 +#include <unistd.h>\r
178 +#include <sys/types.h>\r
179 +#include <sys/wait.h>\r
180 +\r
181 +#include <glib.h>\r
182 +\r
183 +#include <notmuch.h>\r
184 +\r
185 +\r
186 +#define MIN_UWAIT 1000        // 1 millisecond\r
187 +#define MAX_UWAIT 600000000   // 10 minutes\r
188 +\r
189 +\r
190 +\r
191 +static gint sleep_option = -1;\r
192 +static GOptionEntry entries[] = {\r
193 +       { "sleep", 's', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &sleep_option,\r
194 +               "Sleep for N microseconds, while holding the lock to the database", "N" },\r
195 +       {NULL, 0, 0, 0, NULL, NULL, NULL},\r
196 +};\r
197 +\r
198 +\r
199 +\r
200 +static gboolean\r
201 +load_keyfile(const gchar *path, gchar **db_path, gchar ***tags)\r
202 +{\r
203 +       GKeyFile *fd;\r
204 +       GError *error;\r
205 +\r
206 +       fd = g_key_file_new();\r
207 +       error = NULL;\r
208 +       if (!g_key_file_load_from_file(fd, path, G_KEY_FILE_NONE,\r
209 +                               &error)) {\r
210 +               g_printerr("Failed to parse `%s': %s", path,\r
211 +                               error->message);\r
212 +               g_error_free(error);\r
213 +               g_key_file_free(fd);\r
214 +               return FALSE;\r
215 +       }\r
216 +\r
217 +       *db_path = g_key_file_get_string(fd, "database",\r
218 +                       "path", &error);\r
219 +       if (*db_path == NULL) {\r
220 +               g_critical("Failed to parse database.path from `%s': %s", path,\r
221 +                               error->message);\r
222 +               g_error_free(error);\r
223 +               g_key_file_free(fd);\r
224 +               return FALSE;\r
225 +       }\r
226 +\r
227 +       *tags = g_key_file_get_string_list(fd, "new",\r
228 +                       "tags", NULL, NULL);\r
229 +\r
230 +       g_key_file_free(fd);\r
231 +       return TRUE;\r
232 +}\r
233 +\r
234 +\r
235 +\r
236 +static gchar *\r
237 +get_db_path(void)\r
238 +{\r
239 +       gchar *conf_path, *db_path;\r
240 +\r
241 +       // Get location of notmuch config\r
242 +       if (g_getenv("NOTMUCH_CONFIG"))\r
243 +               conf_path = g_strdup(g_getenv("NOTMUCH_CONFIG"));\r
244 +       else if (g_getenv("HOME"))\r
245 +               conf_path = g_build_filename(g_getenv("HOME"),\r
246 +                               ".notmuch-config", NULL);\r
247 +       else {\r
248 +               g_critical("Neither NOTMUCH_CONFIG nor HOME set");\r
249 +               return NULL;\r
250 +       }\r
251 +\r
252 +       // Get location of notmuch database\r
253 +       gchar **conf_tags = NULL;\r
254 +       g_print("Parsing configuration from `%s'\n", conf_path);\r
255 +       if (!load_keyfile(conf_path, &db_path, &conf_tags)) {\r
256 +               g_free(conf_path);\r
257 +               return NULL;\r
258 +       }\r
259 +\r
260 +       g_free(conf_path);\r
261 +       return db_path;\r
262 +}\r
263 +\r
264 +\r
265 +int\r
266 +main(int argc, char *argv[])\r
267 +{\r
268 +       gchar *db_path;\r
269 +       gint32 sleep_time = 0;\r
270 +       notmuch_database_t *db;\r
271 +       GError *error = NULL;\r
272 +       GOptionContext *context;\r
273 +\r
274 +       // parse command line options\r
275 +       gboolean option_parse_success = FALSE;\r
276 +       context = g_option_context_new( "COMMAND [ARGS...]");\r
277 +       g_option_context_add_main_entries(context, entries, NULL);\r
278 +       g_option_context_set_summary(context,\r
279 +                       "Utility to test behaviour of programs while the notmuch database is locked");\r
280 +       option_parse_success = g_option_context_parse(context, &argc, &argv, &error);\r
281 +       g_option_context_free(context);\r
282 +       if (! option_parse_success) {\r
283 +               g_printerr("option parsing failed: %s\n", error->message);\r
284 +               return EXIT_FAILURE;\r
285 +       }\r
286 +\r
287 +       // Check new program args\r
288 +       if ((argc - 1) <= 0) {\r
289 +               g_printerr("no command supplied\n");\r
290 +               return EXIT_FAILURE;\r
291 +       }\r
292 +       char **new_argv = argv + 1;\r
293 +       if (g_strcmp0(new_argv[0], "--") == 0)\r
294 +               new_argv++;\r
295 +\r
296 +       // Get notmuch database path\r
297 +       db_path = get_db_path();\r
298 +       if (db_path == NULL)\r
299 +               return EXIT_FAILURE;\r
300 +\r
301 +       // Open notmuch database\r
302 +       g_printerr("Opening notmuch database `%s'\n", db_path);\r
303 +       db = notmuch_database_open(db_path, NOTMUCH_DATABASE_MODE_READ_WRITE);\r
304 +       g_free(db_path);\r
305 +       if (db == NULL)\r
306 +               return EXIT_FAILURE;\r
307 +\r
308 +       gboolean spawn_success = FALSE;\r
309 +       GPid child_pid = 0;\r
310 +       spawn_success = g_spawn_async(\r
311 +                       g_get_current_dir(),\r
312 +                       new_argv,\r
313 +                       NULL,\r
314 +                       G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN | G_SPAWN_DO_NOT_REAP_CHILD,\r
315 +                       NULL,\r
316 +                       NULL,\r
317 +                       &child_pid,\r
318 +                       NULL\r
319 +                       );\r
320 +       if (!spawn_success) {\r
321 +               g_printerr("faild to spawn child\n");\r
322 +       }\r
323 +\r
324 +       // Sleep for some time\r
325 +       if (sleep_option >= 0) {\r
326 +               sleep_time = sleep_option;\r
327 +       } else {\r
328 +               sleep_time = g_random_int_range(MIN_UWAIT, MAX_UWAIT);\r
329 +       }\r
330 +       g_printerr("Sleeping for %f secs\n", ((double)sleep_time)/(1000*1000));\r
331 +       usleep(sleep_time);\r
332 +\r
333 +       // Close database again\r
334 +       notmuch_database_close(db);\r
335 +\r
336 +       int child_status = 0;\r
337 +       child_pid = waitpid(child_pid, &child_status, 0);\r
338 +\r
339 +       return WEXITSTATUS(child_status);\r
340 +}\r
341 diff --git a/contrib/notmuch-deliver/test/testmail b/contrib/notmuch-deliver/test/testmail\r
342 new file mode 100644\r
343 index 0000000..264518d\r
344 --- /dev/null\r
345 +++ b/contrib/notmuch-deliver/test/testmail\r
346 @@ -0,0 +1,7 @@\r
347 +From: test@example.com\r
348 +To: test@example.com\r
349 +Subject: testmail\r
350 +Message-Id: <20111022050326.653C3201BF@example.com>\r
351 +Date: Sat, 22 Oct 2011 07:03:26 +0200 (CEST)\r
352 +\r
353 +testmail\r
354 -- \r
355 1.7.7.1\r
356 \r