9
9
from __future__ import absolute_import
13
from dbus.mainloop.glib import DBusGMainLoop
15
# from dbus.mainloop.glib import DBusGMainLoop
28
# Pokemon functions: gotta call 'em all!
29
# If you don't, random glib/gobject/gtk functions will hang...
21
30
glib.threads_init()
22
dbus.mainloop.glib.threads_init()
23
gtk.gdk.threads_init()
26
# Turning run_in_glib_loop into a decorator is left as an exercise for the
31
dbus.glib.threads_init()
32
# dbus.mainloop.glib.threads_init()
33
# gtk.gdk.threads_init()
35
class WorkerThread(threading.Thread):
36
def __init__(self, loop, worker):
37
super(WorkerThread, self).__init__()
45
while self.loop.is_running() is False:
46
# _print("Thread '%i': mainloop not yet running, backing off" % (self.ident, ))
49
# from autopilot.emulators.bamf import Bamf
50
# print Bamf().get_running_applications()
51
self.result = self.worker()
53
self.errors.append(sys.exc_info())
54
# XXX: Not sure if this is needed / desired
57
# print "Quitting main loop now."
58
glib.idle_add(self.loop.quit)
28
63
def run_in_glib_loop(function, *args, **kwargs):
29
# log.info("Running: %r with args: %r and kwargs: %r", function, args, kwargs)
30
loop = gobject.MainLoop()
31
# XXX: I think this has re-entrancy problems. There is parallel code in
32
# testtools somewhere (spinner.py or deferredruntest.py)
36
# This function is run in a worker thread. The GLib main loop is guaranteed to
37
# be started before this function is run.
38
class ThreadTest(threading.Thread):
39
def __init__(self, loop):
40
super(ThreadTest, self).__init__()
44
DBusGMainLoop(set_as_default=True)
46
# import pdb; pdb.set_trace()
47
result.append(function(*args, **kwargs))
49
errors.append(sys.exc_info())
50
# XXX: Not sure if this is needed / desired
55
thread = ThreadTest(loop)
56
# Calling thread.start directly here creates a possible race condition. We
57
# need to be assured that the glib main loop has started by the time the test
58
# is running, so we start the thread from the main loop itself. This waits
59
# 10 mS - it could possibly be set to 0.
60
glib.timeout_add(10, thread.start)
65
loop = glib.MainLoop()
66
# dbus.set_default_main_loop(dbus_loop)
68
thread = WorkerThread(loop, lambda: function(*args, **kwargs))
71
print "Loop finished."
73
# print "Thread joined"
68
74
if thread.is_alive():
69
75
raise RuntimeError("Test %r did not exit after 120 seconds." % function)
71
# assert loop.is_running() == False, "Loop should not be running after thread has quit!"
78
raise thread.errors[0]
78
82
class GlibRunner(testtools.RunTest):