pass a reactor into SynapseSite (#9874)

This commit is contained in:
Richard van der Hoff 2021-04-23 17:06:47 +01:00 committed by GitHub
parent e83627926f
commit 59d24c5bef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 24 deletions

1
changelog.d/9874.misc Normal file
View file

@ -0,0 +1 @@
Pass a reactor into `SynapseSite` to make testing easier.

View file

@ -367,6 +367,7 @@ class GenericWorkerServer(HomeServer):
listener_config, listener_config,
root_resource, root_resource,
self.version_string, self.version_string,
reactor=self.get_reactor(),
), ),
reactor=self.get_reactor(), reactor=self.get_reactor(),
) )

View file

@ -126,19 +126,20 @@ class SynapseHomeServer(HomeServer):
else: else:
root_resource = OptionsResource() root_resource = OptionsResource()
root_resource = create_resource_tree(resources, root_resource) site = SynapseSite(
"synapse.access.%s.%s" % ("https" if tls else "http", site_tag),
site_tag,
listener_config,
create_resource_tree(resources, root_resource),
self.version_string,
reactor=self.get_reactor(),
)
if tls: if tls:
ports = listen_ssl( ports = listen_ssl(
bind_addresses, bind_addresses,
port, port,
SynapseSite( site,
"synapse.access.https.%s" % (site_tag,),
site_tag,
listener_config,
root_resource,
self.version_string,
),
self.tls_server_context_factory, self.tls_server_context_factory,
reactor=self.get_reactor(), reactor=self.get_reactor(),
) )
@ -148,13 +149,7 @@ class SynapseHomeServer(HomeServer):
ports = listen_tcp( ports = listen_tcp(
bind_addresses, bind_addresses,
port, port,
SynapseSite( site,
"synapse.access.http.%s" % (site_tag,),
site_tag,
listener_config,
root_resource,
self.version_string,
),
reactor=self.get_reactor(), reactor=self.get_reactor(),
) )
logger.info("Synapse now listening on TCP port %d", port) logger.info("Synapse now listening on TCP port %d", port)

View file

@ -19,8 +19,9 @@ from typing import Optional, Tuple, Type, Union
import attr import attr
from zope.interface import implementer from zope.interface import implementer
from twisted.internet.interfaces import IAddress from twisted.internet.interfaces import IAddress, IReactorTime
from twisted.python.failure import Failure from twisted.python.failure import Failure
from twisted.web.resource import IResource
from twisted.web.server import Request, Site from twisted.web.server import Request, Site
from synapse.config.server import ListenerConfig from synapse.config.server import ListenerConfig
@ -485,21 +486,39 @@ class _XForwardedForAddress:
class SynapseSite(Site): class SynapseSite(Site):
""" """
Subclass of a twisted http Site that does access logging with python's Synapse-specific twisted http Site
standard logging
This does two main things.
First, it replaces the requestFactory in use so that we build SynapseRequests
instead of regular t.w.server.Requests. All of the constructor params are really
just parameters for SynapseRequest.
Second, it inhibits the log() method called by Request.finish, since SynapseRequest
does its own logging.
""" """
def __init__( def __init__(
self, self,
logger_name, logger_name: str,
site_tag, site_tag: str,
config: ListenerConfig, config: ListenerConfig,
resource, resource: IResource,
server_version_string, server_version_string,
*args, reactor: IReactorTime,
**kwargs,
): ):
Site.__init__(self, resource, *args, **kwargs) """
Args:
logger_name: The name of the logger to use for access logs.
site_tag: A tag to use for this site - mostly in access logs.
config: Configuration for the HTTP listener corresponding to this site
resource: The base of the resource tree to be used for serving requests on
this site
server_version_string: A string to present for the Server header
reactor: reactor to be used to manage connection timeouts
"""
Site.__init__(self, resource, reactor=reactor)
self.site_tag = site_tag self.site_tag = site_tag

View file

@ -349,6 +349,7 @@ class BaseMultiWorkerStreamTestCase(unittest.HomeserverTestCase):
config=worker_hs.config.server.listeners[0], config=worker_hs.config.server.listeners[0],
resource=resource, resource=resource,
server_version_string="1", server_version_string="1",
reactor=self.reactor,
) )
if worker_hs.config.redis.redis_enabled: if worker_hs.config.redis.redis_enabled:

View file

@ -202,6 +202,7 @@ class OptionsResourceTests(unittest.TestCase):
parse_listener_def({"type": "http", "port": 0}), parse_listener_def({"type": "http", "port": 0}),
self.resource, self.resource,
"1.0", "1.0",
reactor=self.reactor,
) )
# render the request and return the channel # render the request and return the channel

View file

@ -247,6 +247,7 @@ class HomeserverTestCase(TestCase):
config=self.hs.config.server.listeners[0], config=self.hs.config.server.listeners[0],
resource=self.resource, resource=self.resource,
server_version_string="1", server_version_string="1",
reactor=self.reactor,
) )
from tests.rest.client.v1.utils import RestHelper from tests.rest.client.v1.utils import RestHelper