[Twisted-Python] Using a custom reactor in twisted trial for test cases?
Crispin Wellington
cwellington at ccg.murdoch.edu.au
Fri Oct 30 03:38:12 MDT 2009
Hi there,
I am using twisted trial to run test cases for an application. The
application however uses stackless python and has a custom stackless
reactor. I implemented this reactor like this...
-------------------- stacklessreactor.py -----------------------
# Use epoll() as our base reactor
from twisted.internet.epollreactor import EPollReactor as StacklessBaseReactor
import stackless
# seconds between running the greenthreads. 0.0 for flat out 100% CPU
STACKLESS_MAX_PUMP_RATE = 0.1
class StacklessReactor(StacklessBaseReactor):
"""This reactor does the stackless greenthread pumping in the main thread, interwoven with the reactor pump"""
def doIteration(self, timeout):
"""Calls the base reactors doIteration, and then fires off all the stackless threads"""
if timeout > STACKLESS_MAX_PUMP_RATE:
timeout = STACKLESS_MAX_PUMP_RATE
stackless.schedule()
return StacklessBaseReactor.doIteration(self,timeout)
def install():
"""
Install the stackless() reactor.
"""
p = StacklessReactor()
from twisted.internet.main import installReactor
installReactor(p)
-------------------------------------------------------------------
And I install this as my reactor in my application with...
import stacklessreactor
stacklessreactor.install()
...placed right at the top of my .tac python file. And this all works.
Running the app with twistd, the custom reactor is installed and is used
as the reactor for the app.
Now however, I come to write tests and run them with trial. I *need* the
tests to be run under the stackless reactor or things simply wont work
(a lot of the code I need to test are stackless tasklets).
When I go "/usr/local/stackless/bin/trial --help-reactors" I get the
following list:
kqueue kqueue(2)-based reactor.
win32 Win32 WaitForMultipleObjects-based reactor.
epoll epoll(4)-based reactor.
iocp Win32 IO Completion Ports-based reactor.
gtk Gtk1 integration reactor.
cf CoreFoundation integration reactor.
gtk2 Gtk2 integration reactor.
default The best reactor for the current platform.
debug-gui Semi-functional debugging/introspection reactor.
poll poll(2)-based reactor.
glib2 GLib2 event-loop integration reactor.
select select(2)-based reactor.
wx wxPython integration reactor.
qt QT integration reactor
One of these I can use by passing in --reactor=name.
So the question is, is there a way of getting the trial framework to use
my custom reactor? Is there a way to get my reactor into that list
somehow? Is this not a supported feature of trial?
And... if this isn't a supported feature, what is the best way to get a
TestCase that will run under that reactor?
Look forward to any help people can offer me.
With kind regards
Crispin Wellington
More information about the Twisted-Python
mailing list