[Twisted-Python] Global reactor unit tests in the Twisted test suite
mostawesomedude at gmail.com
Tue Nov 1 13:23:05 EDT 2011
On Tue, Nov 1, 2011 at 10:06 AM, Phil Mayers <p.mayers at imperial.ac.uk> wrote:
> On 01/11/11 16:58, Itamar Turner-Trauring wrote:
>>> I find the "pass reactor as 1st argument to everything" API pattern
>>> messy. I'm sure there's a good reason. What is it?
>> 1. Supporting multiple reactors.
> Interesting. I had assumed that was a "never on the cards" option. Would
> it be possible to "nest" the reactors, or would they have to run in
> separate threads? Or is this speculative?
>> 2. Unit testing: if you have an explicit object, you can replace it more
>> easily with a fake.
> Ok. I realise it's a bit late in the day at this point, but I do wonder
> if something like:
> def method(arg, reactor=DefaultReactor):
> ...with DefaultReactor being some kind of lazy singleton or weak
> reference might not have been a more friendly pattern to API users.
> As it is, I might bind a keystroke to "reactor, " ;o)
Singletons are an anti-pattern. Because the reactor is a singleton:
* We can't have multiple reactors instantiated
* We can't restart reactors
* We can't isolate certain reactor-dependent unit tests
* We have to install reactors, which requires careful ordering of imports
In Bravo, Exocet is used to set up a pseudo-sandbox. If reactor were
not a singleton, I could isolate people's usage of Twisted calls, and
force them to proxy everything. Instead, I have to grant them access
to the reactor, because it's shared global state. Is this horribly
important? No, but it does prohibit more effective sandboxing and
Exocet needs to keep a list of modules which are so fucked that they
must be allowed to touch sys.modules to work correctly. There are two
things in that list: os.path and twisted.internet.reactor. Also, the
reactor only works if it's imported before Exocet. While Exocet's not
exactly a bastion of sanity, this doesn't speak well of the magic that
the reactor goes through in order to advertise itself globally.
Those are my reasons. They're not superb reasons from a
getting-shit-done point of view, but from a reducing-cyclopean-horrors
point of view, I think they're worth considering.
When the facts change, I change my mind. What do you do, sir? ~ Keynes
<MostAwesomeDude at gmail.com>
More information about the Twisted-Python