Ejemplo n.º 1
0
    def test_terminate(self):
        """Test different ways of sending a termination message to server:
        http-request, plain-text and json-rpc."""
        async def terminate_server(termination_request, expected_response):
            reader, writer = await asyncio_connect('127.0.0.1', TEST_PORT)
            writer.write(termination_request)
            data = await reader.read(500)
            writer.close()
            if sys.version_info >= (3, 7): await writer.wait_closed()
            assert data.find(expected_response) >= 0, str(data)

        p = None
        try:
            # plain text stop request
            p = spawn_tcp_server('127.0.0.1', TEST_PORT,
                                 (compiler_dummy, set()))
            asyncio_run(
                terminate_server(
                    STOP_SERVER_REQUEST_BYTES,
                    b'DHParser server at 127.0.0.1:%i stopped!' % TEST_PORT))
            assert asyncio_run(has_server_stopped('127.0.0.1', TEST_PORT))

            # http stop request
            p = spawn_tcp_server('127.0.0.1', TEST_PORT,
                                 (compiler_dummy, set()))
            asyncio_run(
                terminate_server(
                    b'GET ' + STOP_SERVER_REQUEST_BYTES + b' HTTP',
                    b'DHParser server at 127.0.0.1:%i stopped!' % TEST_PORT))
            assert asyncio_run(has_server_stopped('127.0.0.1', TEST_PORT))

            # json_rpc stop request
            p = spawn_tcp_server('127.0.0.1', TEST_PORT,
                                 (compiler_dummy, set()))
            jsonrpc = json.dumps({
                "jsonrpc": "2.0",
                "method": STOP_SERVER_REQUEST_BYTES.decode(),
                'id': 1
            })
            asyncio_run(
                terminate_server(
                    jsonrpc.encode(),
                    b'DHParser server at 127.0.0.1:%i stopped!' % TEST_PORT))
            assert asyncio_run(has_server_stopped('127.0.0.1', TEST_PORT))
        finally:
            stop_tcp_server('127.0.0.1', TEST_PORT)
            if p is not None:
                p.join()
Ejemplo n.º 2
0
 def start_server(self):
     stop_tcp_server('127.0.0.1', TEST_PORT)
     if self.p is not None:
         self.p.join()
     self.lsp = LSP()
     lsp_table = gen_lsp_table(self.lsp, prefix='lsp_')
     self.p = spawn_tcp_server('127.0.0.1', TEST_PORT,
                               (lsp_table, frozenset(), frozenset()))
Ejemplo n.º 3
0
    def test_spawn(self):
        spawn_tcp_server('127.0.0.1', TEST_PORT)

        async def identify():
            try:
                reader, writer = await asyncio_connect('127.0.0.1', TEST_PORT)
                writer.write(IDENTIFY_REQUEST.encode())
                data = await reader.read(500)
                await writer.drain()
                writer.close()
                if sys.version_info >= (3, 7): await writer.wait_closed()
                return data.decode()
            except ConnectionRefusedError:
                return ''

        result = asyncio_run(identify())
        assert result.startswith('DHParser'), result
Ejemplo n.º 4
0
    def test_server_process(self):
        """Basic Test of server module."""
        async def compile_remote(src):
            reader, writer = await asyncio_connect('127.0.0.1', TEST_PORT)
            writer.write(src.encode())
            data = await reader.read(500)
            writer.close()
            if sys.version_info >= (3, 7): await writer.wait_closed()
            assert data.decode() == "Test", data.decode()

        p = None
        try:
            p = spawn_tcp_server('127.0.0.1', TEST_PORT,
                                 (compiler_dummy, set()))
            asyncio_run(compile_remote('Test'))
        finally:
            stop_tcp_server('127.0.0.1', TEST_PORT)
            if p is not None:
                p.join()
Ejemplo n.º 5
0
    def test_service_call(self):
        async def identify_server():
            main_reader, main_writer = await asyncio_connect(
                '127.0.0.1', TEST_PORT)
            main_writer.write(IDENTIFY_REQUEST.encode())
            data = await main_reader.read(500)
            assert b'already connected' not in data

            service_reader, service_writer = await asyncio_connect(
                '127.0.0.1', TEST_PORT)
            service_writer.write(IDENTIFY_REQUEST.encode())
            data = await service_reader.read(500)
            assert b'already connected' in data
            await asyncio.sleep(0.01)
            assert service_reader.at_eof()
            service_writer.close()
            if sys.version_info >= (3, 7): await service_writer.wait_closed()

            service_reader, service_writer = await asyncio_connect(
                '127.0.0.1', TEST_PORT)
            service_writer.write(json_rpc('identify', {}).encode())
            data = await service_reader.read(500)
            assert b'already connected' in data
            await asyncio.sleep(0.01)
            assert service_reader.at_eof()
            service_writer.close()
            if sys.version_info >= (3, 7): await service_writer.wait_closed()

            main_writer.close()
            if sys.version_info >= (3, 7): await main_writer.wait_closed()

        p = None
        try:
            p = spawn_tcp_server('127.0.0.1', TEST_PORT)
            asyncio_run(identify_server())
        finally:
            stop_tcp_server('127.0.0.1', TEST_PORT)
            if p is not None:
                p.join()
Ejemplo n.º 6
0
    def test_identify(self):
        """Test server's 'identify/'-command."""
        async def send_request(request):
            reader, writer = await asyncio_connect('127.0.0.1', TEST_PORT)
            writer.write(
                request.encode() if isinstance(request, str) else request)
            data = await reader.read(500)
            await writer.drain()
            writer.close()
            if sys.version_info >= (3, 7): await writer.wait_closed()
            return data.decode()

        p = None
        try:
            from timeit import timeit
            p = spawn_tcp_server('127.0.0.1', TEST_PORT, compiler_dummy)
            result = asyncio_run(send_request(IDENTIFY_REQUEST))
            assert isinstance(result,
                              str) and result.startswith('DHParser'), result
        finally:
            stop_tcp_server('127.0.0.1', TEST_PORT)
            if p is not None:
                p.join()
Ejemplo n.º 7
0
    def test_long_running_task(self):
        """Test, whether delegation of (long-running) tasks to
        processes or threads works."""
        sequence = []
        if self.spawn:
            SLOW, FAST = 0.1, 0.01
        else:
            SLOW, FAST = 0.02, 0.001

        async def run_tasks():
            def extract_result(data: bytes):
                header, data, backlog = split_header(data)
                return json.loads(data.decode())['result']

            reader, writer = await asyncio_connect('127.0.0.1', TEST_PORT)
            sequence.append(SLOW)
            sequence.append(FAST)
            writer.write(json_rpc('long_running', {'duration': SLOW}).encode())
            writer.write(json_rpc('long_running', {'duration': FAST}).encode())
            await writer.drain()
            sequence.append(extract_result(await reader.read(500)))
            sequence.append(extract_result(await reader.read(500)))
            writer.close()
            if sys.version_info >= (3, 7): await writer.wait_closed()

        if sys.version_info >= (3, 6):
            p = None
            try:
                p = spawn_tcp_server(
                    '127.0.0.1', TEST_PORT,
                    (long_running, frozenset(['long_running']), frozenset(),
                     connection_cb_dummy, 'Long-Running-Test', False))
                asyncio_run(run_tasks())
                assert sequence == [SLOW, FAST, FAST, SLOW], str(sequence)
            finally:
                stop_tcp_server('127.0.0.1', TEST_PORT)
                if p is not None:
                    p.join()
                sequence = []
            p = None
            try:
                p = spawn_tcp_server(
                    '127.0.0.1', TEST_PORT,
                    (long_running, frozenset(), frozenset(['long_running']),
                     connection_cb_dummy, 'Long-Running-Test', False))
                asyncio_run(run_tasks())
                assert sequence == [SLOW, FAST, FAST, SLOW], str(sequence)
            finally:
                stop_tcp_server('127.0.0.1', TEST_PORT)
                if p is not None:
                    p.join()
                sequence = []
        p = None
        try:
            p = spawn_tcp_server(
                '127.0.0.1', TEST_PORT,
                (long_running, frozenset(), frozenset(), connection_cb_dummy,
                 'Long-Running-Test', False))
            asyncio_run(run_tasks())
            assert sequence.count(SLOW) == 2 and sequence.count(FAST) == 2
        finally:
            stop_tcp_server('127.0.0.1', TEST_PORT)
            if p is not None:
                p.join()
            sequence = []