Fix tests on Twisted trunk. (#16528)
Twisted trunk makes a change to the `TLSMemoryBIOFactory` where
the underlying protocol is changed from `TLSMemoryBIOProtocol` to
`BufferingTLSTransport` to improve performance of TLS code (see
https://github.com/twisted/twisted/issues/11989).
In order to properly hook this code up in tests we need to pass the test
reactor's clock into `TLSMemoryBIOFactory` to avoid the global (trial)
reactor being used by default.
Twisted does something similar internally for tests:
157cd8e659/src/twisted/web/test/test_agent.py (L871-L874)
This commit is contained in:
@@ -43,9 +43,11 @@ from typing import (
|
||||
from unittest.mock import Mock
|
||||
|
||||
import attr
|
||||
from incremental import Version
|
||||
from typing_extensions import ParamSpec
|
||||
from zope.interface import implementer
|
||||
|
||||
import twisted
|
||||
from twisted.internet import address, tcp, threads, udp
|
||||
from twisted.internet._resolver import SimpleResolverComplexifier
|
||||
from twisted.internet.defer import Deferred, fail, maybeDeferred, succeed
|
||||
@@ -474,6 +476,16 @@ class ThreadedMemoryReactorClock(MemoryReactorClock):
|
||||
return fail(DNSLookupError("OH NO: unknown %s" % (name,)))
|
||||
return succeed(lookups[name])
|
||||
|
||||
# In order for the TLS protocol tests to work, modify _get_default_clock
|
||||
# on newer Twisted versions to use the test reactor's clock.
|
||||
#
|
||||
# This is *super* dirty since it is never undone and relies on the next
|
||||
# test to overwrite it.
|
||||
if twisted.version > Version("Twisted", 23, 8, 0):
|
||||
from twisted.protocols import tls
|
||||
|
||||
tls._get_default_clock = lambda: self # type: ignore[attr-defined]
|
||||
|
||||
self.nameResolver = SimpleResolverComplexifier(FakeResolver())
|
||||
super().__init__()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user