Beispiel #1
0
    def test_end_to_end(self):
        def do_test(client, server, server_serve):
            url = 'inproc://%s' % uuid.uuid4()
            server.socket.listen(url)
            client.socket.dial(url)

            server_task = tasks.spawn(server_serve)

            client_task = tasks.spawn(client.m.greet(name='world'))

            with self.assertRaises(kernels.KernelTimeout):
                kernels.run(timeout=0.005)

            self.assertTrue(client_task.is_completed())
            self.assertEqual(client_task.get_result_nonblocking(),
                             'Hello, world')

            self.assertFalse(server_task.is_completed())

            server.socket.close()
            kernels.run(timeout=1)

            self.assertTrue(server_task.is_completed())
            self.assertIsNone(server_task.get_result_nonblocking())

        app = TestApplication()
        with servers.Server(app, Request, Response, WIRE_DATA) as server:
            with clients.Client(Request, Response, WIRE_DATA) as client:
                do_test(client, server, server.serve)

        app = TestApplication()
        server = servers.Server(app, Request, Response, WIRE_DATA)
        with clients.Client(Request, Response, WIRE_DATA) as client:
            with server:
                do_test(client, server, server.serve)
Beispiel #2
0
def make_server(
    exit_stack: asyncs.LABELS.exit_stack,
    create_engine,
    publisher,
    agent_queue: g1.asyncs.agents.parts.LABELS.agent_queue,
    shutdown_queue: g1.asyncs.agents.parts.LABELS.shutdown_queue,
):
    server = exit_stack.enter_context(
        servers.DatabaseServer(engine=create_engine(), publisher=publisher))
    agent_queue.spawn(server.serve)
    shutdown_queue.put_nonblocking(server.shutdown)
    return g1_servers.Server(
        server,
        interfaces.DatabaseRequest,
        interfaces.DatabaseResponse,
        capnps.WIRE_DATA,
        warning_level_exc_types=(
            interfaces.InvalidRequestError,
            interfaces.KeyNotFoundError,
            interfaces.LeaseNotFoundError,
            interfaces.TransactionNotFoundError,
            interfaces.TransactionTimeoutError,
        ),
        invalid_request_error=interfaces.InvalidRequestError(),
        internal_server_error=interfaces.InternalError(),
    )
Beispiel #3
0
def make_server():
    return servers.Server(
        Calculator(),
        CalculatorRequest,
        CalculatorResponse,
        jsons.JsonWireData(),
    )
Beispiel #4
0
 def test_only_one_error(self):
     request_type, response_type = \
         reqrep.generate_interface_types(TestOnlyOneError)
     server = servers.Server(
         TestOnlyOneError(),
         request_type,
         response_type,
         WIRE_DATA,
     )
     self.assertEqual(
         server._declared_error_types,
         {InternalServerError: 'internal_server_error'},
     )
Beispiel #5
0
    def test_serve(self):
        server = servers.Server(
            TestApplication(),
            Request,
            Response,
            WIRE_DATA,
            invalid_request_error=InvalidRequestError(),
            internal_server_error=InternalServerError(),
        )

        wire_request = WIRE_DATA.to_lower(
            Request(args=Request.m.greet(name='world')))
        self.assertEqual(
            WIRE_DATA.to_upper(
                Response,
                kernels.run(server._serve(wire_request)),
            ),
            Response(result=Response.Result(greet='Hello, world')),
        )

        with self.assertLogs(servers.__name__, level='DEBUG') as cm:
            self.assertEqual(
                kernels.run(server._serve(b'')),
                server._invalid_request_error_wire,
            )
        self.assertRegex('\n'.join(cm.output), r'to_upper error: ')

        wire_request = WIRE_DATA.to_lower(Request(args=Request.m.f()))
        with self.assertLogs(servers.__name__, level='DEBUG') as cm:
            self.assertEqual(
                kernels.run(server._serve(wire_request)),
                server._invalid_request_error_wire,
            )
        self.assertRegex('\n'.join(cm.output), r'unknown method: f: ')

        wire_request = WIRE_DATA.to_lower(Request(args=Request.m.g()))
        with self.assertLogs(servers.__name__, level='DEBUG') as cm:
            self.assertEqual(
                kernels.run(server._serve(wire_request)),
                server._internal_server_error_wire,
            )
        self.assertRegex('\n'.join(cm.output), r'to_lower error: ')

        wire_request = WIRE_DATA.to_lower(Request(args=Request.m.h()))
        with self.assertLogs(servers.__name__, level='DEBUG') as cm:
            self.assertEqual(
                kernels.run(server._serve(wire_request)),
                server._internal_server_error_wire,
            )
        self.assertRegex('\n'.join(cm.output), r'server error: ')