def serve_thrift(handler, server_span_observer=None): # create baseplate root baseplate = Baseplate() if server_span_observer: class TestBaseplateObserver(BaseplateObserver): def on_server_span_created(self, context, server_span): server_span.register(server_span_observer) baseplate.register(TestBaseplateObserver()) # set up the server's processor logger = mock.Mock(spec=logging.Logger) edge_context_factory = make_edge_context_factory() processor = TestService.Processor(handler) processor = baseplateify_processor(processor, logger, baseplate, edge_context_factory) # bind a server socket on an available port server_bind_endpoint = config.Endpoint("127.0.0.1:0") listener = make_listener(server_bind_endpoint) server = make_server( {"max_concurrency": "100", "stop_timeout": "1 millisecond"}, listener, processor ) # figure out what port the server ended up on server_address = listener.getsockname() server.endpoint = config.Endpoint(f"{server_address[0]}:{server_address[1]}") # run the server until our caller is done with it server_greenlet = gevent.spawn(server.serve_forever) try: yield server finally: server_greenlet.kill()
def http_server(gevent_socket): class HttpServer: def __init__(self, address): self.url = f"http://{address[0]}:{address[1]}/" self.requests = [] def handle_request(self, request): self.requests.append(request) return HTTPNoContent() server_bind_endpoint = config.Endpoint("127.0.0.1:0") listener = make_listener(server_bind_endpoint) server_address = listener.getsockname() http_server = HttpServer(server_address) baseplate = Baseplate() trust_handler = StaticTrustHandler(trust_headers=True) baseplate_configurator = BaseplateConfigurator( baseplate, header_trust_handler=trust_handler) configurator = Configurator() configurator.include(baseplate_configurator.includeme) configurator.add_route("test_view", "/") configurator.add_view(http_server.handle_request, route_name="test_view", renderer="json") wsgi_app = configurator.make_wsgi_app() server = make_server({"stop_timeout": "1 millisecond"}, listener, wsgi_app) server_greenlet = gevent.spawn(server.serve_forever) try: yield http_server finally: server_greenlet.kill()
def test_manually_bound(self, mocket, fcntl): listener = server.make_listener(EXAMPLE_ENDPOINT) self.assertEqual(mocket.call_args, mock.call(socket.AF_INET, socket.SOCK_STREAM)) self.assertEqual(listener, mocket.return_value) self.assertEqual(listener.bind.call_args, mock.call(("127.0.0.1", 1234)))
def test_manually_bound(self, mocket, fcntl): listener = server.make_listener(EXAMPLE_ENDPOINT) self.assertEqual(mocket.call_args, mock.call(socket.AF_INET, socket.SOCK_STREAM)) self.assertEqual(listener, mocket.return_value) self.assertEqual(listener.bind.call_args, mock.call( ("127.0.0.1", 1234)))
def test_einhorn_managed(self, is_worker, get_socket): is_worker.return_value = True listener = server.make_listener(EXAMPLE_ENDPOINT) self.assertEqual(listener, get_socket.return_value)
def test_einhorn_managed(self, fromfd): listener = server.make_listener(EXAMPLE_ENDPOINT) self.assertEqual(listener, fromfd.return_value) self.assertEqual(fromfd.call_count, 1) self.assertEqual(fromfd.call_args, mock.call(123, socket.AF_INET, socket.SOCK_STREAM))