Esempio n. 1
0
def test_sigterm(socket_dir):
    """
    Run a basic mock tests.
    """
    signal_event = multiprocessing.Event()

    def run_server():
        signal_handler = SignalHandler(signal.SIGTERM)
        signal_socket = signal_handler.get_socket()

        class MockServer(Server):
            @rpc_method
            def mock_method(self):
                return {'success': True}

            def signal_handler(self):
                signum = signal_socket.recv().decode()
                if signum == 'SIGTERM':
                    signal_event.set()
                    self.stop()

        server = MockServer(socket_dir=socket_dir)
        server.register(fd=signal_socket, callback=server.signal_handler)

        with signal_handler, server:
            server.run()

    p = multiprocessing.Process(target=run_server)
    p.start()
    mock_client = Client(socket_dir=socket_dir)
    mock_client.call('mock_server', 'mock_method', timeout=3)
    p.terminate()

    assert signal_event.wait(1)
Esempio n. 2
0
def test_server_cache(socket_dir):

    counter = multiprocessing.Value('i')
    counter.value = 0

    def run_server():
        async def coro():
            class MockServer(Server):
                @rpc_method
                async def mock_method(self):
                    await asyncio.sleep(0.1)
                    counter.value += 1
                    return {'success': True}

            await MockServer(socket_dir=socket_dir).run()

        asyncio.run(coro())

    p = multiprocessing.Process(target=run_server, daemon=True)
    p.start()

    # Retry 10 times
    client = Client(socket_dir=socket_dir)
    response = client.call(server='mock_server',
                           method='mock_method',
                           timeout=1)

    assert response['success']

    # Assert method executed only once
    assert counter.value == 1

    p.terminate()
    p.join()
Esempio n. 3
0
def test_server_restart(socket_dir):
    """
    Test whether server works properly on restart.
    """
    number_of_restarts = 5

    def run_server_n_times(n):
        async def coro():
            class MockServer(Server):
                @rpc_method
                async def mock_method(self):
                    return {'success': True}

            server = MockServer(socket_dir=socket_dir)
            for i in range(n):
                with server:
                    await server.run_once()

        asyncio.run(coro())

    p = multiprocessing.Process(target=run_server_n_times,
                                args=[number_of_restarts],
                                daemon=True)
    p.start()

    for i in range(number_of_restarts):
        client = Client(socket_dir=socket_dir)
        response = client.call('mock_server', 'mock_method', timeout=3)
        assert response['success']

    p.terminate()
    p.join()
Esempio n. 4
0
    def run_client():
        client = Client(socket_dir=socket_dir)
        barrier.wait(timeout=1)

        response = client.call(server='mock_server',
                               method='mock_method',
                               timeout=0.1)
        assert response['success']

        barrier.wait(timeout=1)
Esempio n. 5
0
def main():
    client = Client()

    while True:
        timestamp = datetime.datetime.now().strftime('%F_%H-%M-%S-%f')
        response = client.call(server='test_server',
                               method='func',
                               args=('haha', ),
                               kwargs={'kwarg': 'brt'})
        print('RPC response [%s]: %s' % (timestamp, response))
Esempio n. 6
0
 def run_client():
     client = Client(socket_dir=socket_dir)
     response = client.call(server='mock_server',
                            method='mock_method',
                            args=('1', '2'),
                            kwargs={
                                'kwarg1': 3,
                                'kwarg2': 4
                            })
     if response['success']:
         rpc_event.set()
Esempio n. 7
0
def test_server_cache(socket_dir):

    counter = multiprocessing.Value('i')
    counter.value = 0

    class MockServer(Server):
        @rpc_method
        def mock_method(self):
            time.sleep(0.1)
            counter.value += 1
            return {'success': True}

    def run_server():
        MockServer(socket_dir=socket_dir).run()

    multiprocessing.Process(target=run_server, daemon=True).start()

    # Retry 10 times
    client = Client(socket_dir=socket_dir, retry_timeout=0.01)
    client.call(server='mock_server', method='mock_method', timeout=1)

    # Assert method executed only once
    assert counter.value == 1
Esempio n. 8
0
 def run_client():
     client = Client(socket_dir=socket_dir, retry_timeout=0.1)
     response = client.call('mock_server', 'mock_method')
     if response['success']:
         rpc_event.set()
Esempio n. 9
0
 def run_client():
     mock_client = Client(socket_dir=socket_dir)
     response = mock_client.call('mock_server', 'mock_method')
     if response['success']:
         rpc_event.set()
Esempio n. 10
0
 def run_client():
     client = Client(socket_dir=socket_dir)
     try:
         client.call('non_existant', 'non_existant', timeout=0.1)
     except RPCTimeoutError:
         client_event.set()