[patch] [python] Add regression test for main loop failure handling

Simon McVittie simon.mcvittie at collabora.co.uk
Tue Jan 16 11:55:13 PST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

- From 47860b5c25c823587e4e06636acc926b252fe114 Mon Sep 17 00:00:00 2001
From: Simon McVittie <simon.mcvittie at collabora.co.uk>
Date: Tue, 16 Jan 2007 19:52:45 +0000
Subject: [PATCH] Add regression test for main loop failure handling (a main loop that never works)

Also run tests with DBUS_FATAL_WARNINGS and ulimit -c unlimited so they dump
core at the slightest provocation.
- ---
 Makefile.am                     |   11 ++++-
 test/dbus_py_test.c             |  101 +++++++++++++++++++++++++++++++++++++++
 test/run-test.sh                |    6 ++
 test/test-unusable-main-loop.py |   17 +++++++
 4 files changed, 134 insertions(+), 1 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 6f01a1e..43264c1 100644
- --- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,7 @@ EXTRA_DIST = API_CHANGES.txt dbus-python.pc.in HACKING.txt \
 	     test/test-service.py \
 	     test/test-signals.py \
 	     test/test-standalone.py \
+	     test/test-unusable-main-loop.py \
 	     test/TestSuitePythonService.service.in \
 	     test/tmp-session-bus.conf.in
 # miss out the gconf examples for now - they don't work
@@ -47,7 +48,8 @@ nobase_python_PYTHON = dbus_bindings.py \
 
 # === Python C extensions ===
 
- -pyexec_LTLIBRARIES = _dbus_bindings.la _dbus_glib_bindings.la
+pyexec_LTLIBRARIES = _dbus_bindings.la _dbus_glib_bindings.la \
+		     dbus_py_test.la
 _dbus_bindings_la_CPPFLAGS = -I$(srcdir)/include \
 			     $(DBUS_CFLAGS) \
 			     $(PYTHON_INCLUDES)
@@ -91,6 +93,13 @@ _dbus_glib_bindings_la_LDFLAGS = -module -avoid-version \
 _dbus_glib_bindings_la_SOURCES = _dbus_glib_bindings/module.c \
 				 include/dbus-python.h
 
+dbus_py_test_la_CPPFLAGS = -I$(top_srcdir)/include $(DBUS_CFLAGS) \
+			   $(PYTHON_INCLUDES)
+dbus_py_test_la_LDFLAGS = -module -avoid-version \
+			  $(DBUS_LIBS)
+dbus_py_test_la_SOURCES = test/dbus_py_test.c \
+			  include/dbus-python.h
+
 # === Tests ===
 
 PWD = `pwd`
diff --git a/test/dbus_py_test.c b/test/dbus_py_test.c
new file mode 100644
index 0000000..7ef63c4
- --- /dev/null
+++ b/test/dbus_py_test.c
@@ -0,0 +1,101 @@
+/* Test fixtures for dbus-python, based on _dbus_glib_bindings. */
+
+#include <Python.h>
+#include "dbus-python.h"
+
+#if defined(__GNUC__)
+#   if __GNUC__ >= 3
+#       define UNUSED __attribute__((__unused__))
+#   else
+#       define UNUSED /*nothing*/
+#   endif
+#else
+#   define UNUSED /*nothing*/
+#endif
+
+static dbus_bool_t
+dbus_py_test_set_up_conn(DBusConnection *conn UNUSED, void *data UNUSED)
+{
+    PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
+    return 0;
+}
+
+static dbus_bool_t
+dbus_py_test_set_up_srv(DBusServer *srv UNUSED, void *data UNUSED)
+{
+    PyErr_SetString(PyExc_ValueError, "Dummy error from UnusableMainLoop");
+    return 0;
+}
+
+static void
+dbus_py_test_free(void *data UNUSED)
+{
+}
+
+static PyObject *
+dbus_test_native_mainloop(void)
+{
+    PyObject *loop = DBusPyNativeMainLoop_New4(dbus_py_test_set_up_conn,
+                                               dbus_py_test_set_up_srv,
+                                               dbus_py_test_free,
+                                               NULL);
+    return loop;
+}
+
+static PyObject *
+UnusableMainLoop (PyObject *always_null UNUSED, PyObject *args, PyObject *kwargs)
+{
+    PyObject *mainloop, *function, *result;
+    int set_as_default = 0;
+    static char *argnames[] = {"set_as_default", NULL};
+
+    if (PyTuple_Size(args) != 0) {
+        PyErr_SetString(PyExc_TypeError, "UnusableMainLoop() takes no "
+                                         "positional arguments");
+        return NULL;
+    }
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", argnames,
+                                     &set_as_default)) {
+        return NULL;
+    }
+
+    mainloop = dbus_test_native_mainloop();
+    if (mainloop && set_as_default) {
+        if (!_dbus_bindings_module) {
+            PyErr_SetString(PyExc_ImportError, "_dbus_bindings not imported");
+            Py_DECREF(mainloop);
+            return NULL;
+        }
+        function = PyObject_GetAttrString(_dbus_bindings_module,
+                                          "set_default_main_loop");
+        if (!function) {
+            Py_DECREF(mainloop);
+            return NULL;
+        }
+        result = PyObject_CallFunctionObjArgs(function, mainloop, NULL);
+        Py_DECREF(function);
+        if (!result) {
+            Py_DECREF(mainloop);
+            return NULL;
+        }
+    }
+    return mainloop;
+}
+
+static PyMethodDef module_functions[] = {
+    {"UnusableMainLoop", (PyCFunction)UnusableMainLoop,
+     METH_VARARGS|METH_KEYWORDS, "Return a main loop that fails to attach"},
+    {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC
+initdbus_py_test(void)
+{
+    PyObject *this_module;
+
+    if (import_dbus_bindings("dbus_py_test") < 0) return;
+    this_module = Py_InitModule3 ("dbus_py_test", module_functions, "");
+    if (!this_module) return;
+}
+
+/* vim:set ft=c cino< sw=4 sts=4 et: */
diff --git a/test/run-test.sh b/test/run-test.sh
index 0cc5a7d..6058afa 100755
- --- a/test/run-test.sh
+++ b/test/run-test.sh
@@ -1,5 +1,8 @@
 #! /bin/bash
 
+export DBUS_FATAL_WARNINGS=1
+ulimit -c unlimited
+
 function die() 
 {
     if ! test -z "$DBUS_SESSION_BUS_PID" ; then
@@ -37,6 +40,9 @@ fi
 echo "running test-standalone.py"
 $PYTHON "$DBUS_TOP_SRCDIR"/test/test-standalone.py || die "test-standalone.py failed"
 
+echo "running test-unusable-main-loop.py"
+$PYTHON "$DBUS_TOP_SRCDIR"/test/test-unusable-main-loop.py || die "... failed"
+
 echo "running the examples"
 
 $PYTHON "$DBUS_TOP_SRCDIR"/examples/example-service.py &
diff --git a/test/test-unusable-main-loop.py b/test/test-unusable-main-loop.py
new file mode 100644
index 0000000..8bd8e99
- --- /dev/null
+++ b/test/test-unusable-main-loop.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+
+import dbus
+
+from dbus_py_test import UnusableMainLoop
+
+def main():
+    UnusableMainLoop(set_as_default=True)
+    try:
+        bus = dbus.SessionBus()
+    except ValueError, e:
+        print "Correctly got ValueError from UnusableMainLoop"
+    else:
+        raise AssertionError("Expected ValueError from UnusableMainLoop")
+
+if __name__ == '__main__':
+    main()
- -- 
1.4.4.4

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: OpenPGP key: http://www.pseudorandom.co.uk/2003/contact/ or pgp.net

iD8DBQFFrS2hWSc8zVUw7HYRAp1SAJ9uCHxPreScfIgA1dAHuyVUeEISzQCgmOe2
iDif4NAZvAPnFdfJvlH3jOo=
=tk7m
-----END PGP SIGNATURE-----


More information about the dbus mailing list