Skip to content

Commit

Permalink
When using ReactorBuilder, twisted.internet.reactor points to the tem…
Browse files Browse the repository at this point in the history
…porary reactor.
  • Loading branch information
pythonspeed committed Sep 18, 2023
1 parent d760cde commit 0ee143a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
15 changes: 14 additions & 1 deletion src/twisted/internet/test/reactormixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def tearDown(self):
% (process.reapProcessHandlers,)
)

def unbuildReactor(self, reactor):
def unbuildReactor(self, reactor, global_reactor_was_munged=True):
"""
Clean up any resources which may have been allocated for the given
reactor by its creation or by a test which used it.
Expand Down Expand Up @@ -247,6 +247,13 @@ def unbuildReactor(self, reactor):
for c in calls:
c.cancel()

if global_reactor_was_munged:
# Restore the original reactor state:
from twisted.internet import reactor as globalReactor

globalReactor.__dict__ = reactor._originalReactorDict
globalReactor.__class__ = reactor._originalReactorClass

def buildReactor(self):
"""
Create and return a reactor using C{self.reactorFactory}.
Expand All @@ -268,6 +275,12 @@ def buildReactor(self):
)
try:
reactor = self.reactorFactory()
reactor._originalReactorDict = globalReactor.__dict__
reactor._originalReactorClass = globalReactor.__class__
# Make twisted.internet.reactor point to the new reactor,
# temporarily; this is undone in unbuildReactor().
globalReactor.__dict__ = reactor.__dict__
globalReactor.__class__ = reactor.__class__
except BaseException:
# Unfortunately, not all errors which result in a reactor
# being unusable are detectable without actually
Expand Down
12 changes: 6 additions & 6 deletions src/twisted/internet/test/test_gireactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_gApplicationActivate(self) -> None:
L{Gio.Application} instances can be registered with a gireactor.
"""
reactor = gireactor.GIReactor(useGtk=False)
self.addCleanup(self.unbuildReactor, reactor)
self.addCleanup(self.unbuildReactor, reactor, False)

Check warning on line 107 in src/twisted/internet/test/test_gireactor.py

View check run for this annotation

Codecov / codecov/patch

src/twisted/internet/test/test_gireactor.py#L107

Added line #L107 was not covered by tests
app = Gio.Application(
application_id="com.twistedmatrix.trial.gireactor",
flags=Gio.ApplicationFlags.FLAGS_NONE,
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_gtkApplicationActivate(self) -> None:
L{Gtk.Application} instances can be registered with a gtk3reactor.
"""
reactor = gireactor.GIReactor()
self.addCleanup(self.unbuildReactor, reactor)
self.addCleanup(self.unbuildReactor, reactor, False)

Check warning on line 142 in src/twisted/internet/test/test_gireactor.py

View check run for this annotation

Codecov / codecov/patch

src/twisted/internet/test/test_gireactor.py#L142

Added line #L142 was not covered by tests
app = Gtk.Application(
application_id="com.twistedmatrix.trial.gtk3reactor",
flags=Gio.ApplicationFlags.FLAGS_NONE,
Expand All @@ -152,7 +152,7 @@ def test_portable(self) -> None:
registration at this time.
"""
reactor = gireactor.PortableGIReactor()
self.addCleanup(self.unbuildReactor, reactor)
self.addCleanup(self.unbuildReactor, reactor, False)

Check warning on line 155 in src/twisted/internet/test/test_gireactor.py

View check run for this annotation

Codecov / codecov/patch

src/twisted/internet/test/test_gireactor.py#L155

Added line #L155 was not covered by tests
app = Gio.Application(
application_id="com.twistedmatrix.trial.gireactor",
flags=Gio.ApplicationFlags.FLAGS_NONE,
Expand All @@ -165,7 +165,7 @@ def test_noQuit(self) -> None:
allow registration.
"""
reactor = gireactor.GIReactor(useGtk=False)
self.addCleanup(self.unbuildReactor, reactor)
self.addCleanup(self.unbuildReactor, reactor, False)

Check warning on line 168 in src/twisted/internet/test/test_gireactor.py

View check run for this annotation

Codecov / codecov/patch

src/twisted/internet/test/test_gireactor.py#L168

Added line #L168 was not covered by tests
# An app with no "quit" method:
app = object()
exc = self.assertRaises(RuntimeError, reactor.registerGApplication, app)
Expand All @@ -177,7 +177,7 @@ def test_cantRegisterAfterRun(self) -> None:
already started.
"""
reactor = gireactor.GIReactor(useGtk=False)
self.addCleanup(self.unbuildReactor, reactor)
self.addCleanup(self.unbuildReactor, reactor, False)

Check warning on line 180 in src/twisted/internet/test/test_gireactor.py

View check run for this annotation

Codecov / codecov/patch

src/twisted/internet/test/test_gireactor.py#L180

Added line #L180 was not covered by tests
app = Gio.Application(
application_id="com.twistedmatrix.trial.gireactor",
flags=Gio.ApplicationFlags.FLAGS_NONE,
Expand All @@ -200,7 +200,7 @@ def test_cantRegisterTwice(self) -> None:
It is not possible to register more than one C{Application}.
"""
reactor = gireactor.GIReactor(useGtk=False)
self.addCleanup(self.unbuildReactor, reactor)
self.addCleanup(self.unbuildReactor, reactor, False)

Check warning on line 203 in src/twisted/internet/test/test_gireactor.py

View check run for this annotation

Codecov / codecov/patch

src/twisted/internet/test/test_gireactor.py#L203

Added line #L203 was not covered by tests
app = Gio.Application(
application_id="com.twistedmatrix.trial.gireactor",
flags=Gio.ApplicationFlags.FLAGS_NONE,
Expand Down
11 changes: 0 additions & 11 deletions src/twisted/internet/test/test_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
ITLSTransport,
)
from twisted.internet.protocol import ClientFactory, Protocol, ServerFactory
from twisted.internet.task import Cooperator
from twisted.internet.test.connectionmixins import (
BrokenContextFactory,
ConnectionTestsMixin,
Expand Down Expand Up @@ -390,16 +389,6 @@ class AbortSSLConnectionTests(
requiredInterfaces = (IReactorSSL,)
endpoints = SSLCreator()

def buildReactor(self):
reactor = ReactorBuilder.buildReactor(self)
from twisted.internet import _producer_helpers

# Patch twisted.protocols.tls to use this reactor, until we get
# around to fixing #5206, or the TLS code uses an explicit reactor:
cooperator = Cooperator(scheduler=lambda x: reactor.callLater(0.00001, x))
self.patch(_producer_helpers, "cooperate", cooperator.cooperate)
return reactor

def setUp(self):
if FILETYPE_PEM is None:
raise SkipTest("OpenSSL not available.")
Expand Down
Empty file.

0 comments on commit 0ee143a

Please sign in to comment.