Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/tests/server_testing.py
Show All 23 Lines | class ServerTestFixtureBaseClass(metaclass=abc.ABCMeta): | ||||
To ensure test isolation, each test will run in a different server | To ensure test isolation, each test will run in a different server | ||||
and a different folder. | and a different folder. | ||||
In order to correctly work, the subclass must call the parents | In order to correctly work, the subclass must call the parents | ||||
class's setUp() and tearDown() methods. | class's setUp() and tearDown() methods. | ||||
""" | """ | ||||
def setUp(self): | def setUp(self): | ||||
super().setUp() | super().setUp() | ||||
self.start_server() | self.start_server() | ||||
def tearDown(self): | def tearDown(self): | ||||
self.stop_server() | self.stop_server() | ||||
super().tearDown() | super().tearDown() | ||||
def url(self): | def url(self): | ||||
return 'http://127.0.0.1:%d/' % self.port | return "http://127.0.0.1:%d/" % self.port | ||||
def process_config(self): | def process_config(self): | ||||
"""Process the server's configuration. Do something useful for | """Process the server's configuration. Do something useful for | ||||
example, pass along the self.config dictionary inside the | example, pass along the self.config dictionary inside the | ||||
self.app. | self.app. | ||||
By default, do nothing. | By default, do nothing. | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | class ServerTestFixture(ServerTestFixtureBaseClass): | ||||
that corresponds to the type of server the tested client needs. | that corresponds to the type of server the tested client needs. | ||||
To ensure test isolation, each test will run in a different server | To ensure test isolation, each test will run in a different server | ||||
and a different folder. | and a different folder. | ||||
In order to correctly work, the subclass must call the parents | In order to correctly work, the subclass must call the parents | ||||
class's setUp() and tearDown() methods. | class's setUp() and tearDown() methods. | ||||
""" | """ | ||||
def process_config(self): | def process_config(self): | ||||
# WSGI app configuration | # WSGI app configuration | ||||
for key, value in self.config.items(): | for key, value in self.config.items(): | ||||
self.app.config[key] = value | self.app.config[key] = value | ||||
def define_worker_function(self): | def define_worker_function(self): | ||||
def worker(app, port): | def worker(app, port): | ||||
# Make Flask 1.0 stop printing its server banner | # Make Flask 1.0 stop printing its server banner | ||||
os.environ['WERKZEUG_RUN_MAIN'] = 'true' | os.environ["WERKZEUG_RUN_MAIN"] = "true" | ||||
return app.run(port=port, use_reloader=False) | return app.run(port=port, use_reloader=False) | ||||
return worker | return worker | ||||
class ServerTestFixtureAsync(ServerTestFixtureBaseClass): | class ServerTestFixtureAsync(ServerTestFixtureBaseClass): | ||||
"""Base class for http client/server async testing (e.g aiohttp). | """Base class for http client/server async testing (e.g aiohttp). | ||||
Mix this in a test class in order to have access to an http server | Mix this in a test class in order to have access to an http server | ||||
running in background. | running in background. | ||||
Note that the subclass should define an application in self.app | Note that the subclass should define an application in self.app | ||||
that corresponds to the type of server the tested client needs. | that corresponds to the type of server the tested client needs. | ||||
To ensure test isolation, each test will run in a different server | To ensure test isolation, each test will run in a different server | ||||
and a different folder. | and a different folder. | ||||
In order to correctly work, the subclass must call the parents | In order to correctly work, the subclass must call the parents | ||||
class's setUp() and tearDown() methods. | class's setUp() and tearDown() methods. | ||||
""" | """ | ||||
def define_worker_function(self): | def define_worker_function(self): | ||||
def worker(app, port): | def worker(app, port): | ||||
return aiohttp.web.run_app(app, port=int(port), | return aiohttp.web.run_app(app, port=int(port), print=lambda *_: None) | ||||
print=lambda *_: None) | |||||
return worker | return worker |