Exemplo n.º 1
0
def bench_ping_pong_nodegroup():
    # compare Qt4 mainloop of NodeGroup vs Host main loop which is infinite loop (fastest possible)
    for name, class_ in [ ('Host', Host), ('NodeGroup', NodeGroup)]:
        addr = 'tcp://127.0.0.1:*'
        process  = ProcessSpawner(class_,  name, addr)
        client = RPCClient(name, process.addr)
        
        N =1000
        
        t1 = time.time()
        for i in range(N):
            client.ping()
        t2 = time.time()
        print(name, ' : sync ping per second', N/(t2-t1))

        t1 = time.time()
        rets = []
        for i in range(N):
            rets.append(client.ping(_sync=False))
        for ret in rets:
            ret.result()
        t2 = time.time()
        print(name, ' : async ping per second', N/(t2-t1))
        
        client.close()
Exemplo n.º 2
0
def test_rpc():
    previsous_level = logging.getLogger().level
    logging.getLogger().level=logging.INFO
    
    class Server1(RPCServer):
        def add(self, a, b):
            return a + b
        def sleep(self, t):
            time.sleep(t)

    server = Server1(name='some_server', addr='tcp://*:5152')
    serve_thread = threading.Thread(target=server.run_forever, daemon=True)
    serve_thread.start()
    
    client = RPCClient('some_server', 'tcp://localhost:5152')
    # atexit.register(client.close)
    
    # test call / sync return
    assert client.add(7, 5) == 12

    # test async return
    fut = client.sleep(0.1, _sync=False)
    assert not fut.done()
    assert fut.result() is None

    # Test remote exception raising
    try:
        client.add(7, 'x')
    except RemoteCallException as err:
        if err.type_str != 'TypeError':
            raise
    else:
        raise AssertionError('should have raised TypeError')

    try:
        client.fn()
    except RemoteCallException as err:
        if err.type_str != 'AttributeError':
            raise
    else:
        raise AssertionError('should have raised AttributeError')

    # test timeouts
    try:
        client.sleep(0.2, _timeout=0.01)
    except TimeoutError:
        pass
    else:
        raise AssertionError('should have raised TimeoutError')

    # test result order
    a = client.add(1, 2, _sync=False)
    b = client.add(3, 4, _sync=False)
    assert b.result() == 7
    assert a.result() == 3

    # test multiple clients per server
    client2 = RPCClient('some_server', 'tcp://localhost:5152')
    
    a = client2.add(1, 2, _sync=False)
    b = client.add(3, 4, _sync=False)
    c = client2.add(5, 6, _sync=False)
    assert b.result() == 7
    assert a.result() == 3
    assert c.result() == 11

    # test multiple clients sharing one socket
    server2 = Server1(name='some_server2', addr='tcp://*:5153')
    serve_thread2 = threading.Thread(target=server2.run_forever, daemon=True)
    serve_thread2.start()
    
    client3 = RPCClient('some_server2', 'tcp://localhost:5153',
                        rpc_socket=client2._rpc_socket)
    
    a = client2.add(1, 2, _sync=False)
    b = client3.add(3, 4, _sync=False)
    assert b.result() == 7
    assert a.result() == 3
    
    client.close()
    serve_thread.join()
    
    client3.close()
    serve_thread2.join()
    
    logging.getLogger().level=previsous_level