Exemplo n.º 1
0
class TestUnixClient(unittest.TestCase):
    """Functional test for UnixClient."""
    def setUp(self):
        self.path = "test"
        self.ssock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self.ssock.bind(self.path)
        self.ssock.listen(5)
      
        self.factory = MockFactory()
        self.factory.protocol = MockProtocol
        self.client = UnixClient(loop, self.factory, self.path)

        self._connected = False

    def tearDown(self):
        self.ssock.close()
        os.remove(self.path)
        self.client = None
        self.factory = None
        self.ssock = None
    
        self._connected = False
    
    def connected(self, protocol):
        self.assertTrue(isinstance(protocol, MockProtocol))
        self.protocol = protocol
        self._connected = True

    def test_connect(self):
        d = self.client.connect()
        self.assertTrue(isinstance(d, Deferred))
        d.add_callback(self.connected)
        (csock, addr) = self.ssock.accept()

    def test_lose_connection(self):
        d = self.client.connect()
        self.assertTrue(isinstance(d, Deferred))
        d.add_callback(self.connected)
        (csock, addr) = self.ssock.accept()
        d.result()
        self.protocol.lose_connection()
     
    def test_interrupt(self):
        d = self.client.connect()
        self.assertTrue(isinstance(d, Deferred))
        d.add_callback(self.connected)
        (csock, addr) = self.ssock.accept()
        d.result()
        self.client._interrupt(None, None)
        self.assertTrue(self.client.connection is None)

    def test_disconnect(self):
        d = self.client.connect()
        self.assertTrue(isinstance(d, Deferred))
        d.add_callback(self.connected)
        (csock, addr) = self.ssock.accept()
        d.result()
        self.client.disconnect()
        self.assertTrue(self.client.connection is None)
Exemplo n.º 2
0
class ServiceProxy(object):
    """Proxy to a service."""

    def __init__(self, loop, path):
        self.loop = loop
        self.path = path
        self.proxy = None

    def connect(self):
        self.factory = MsgPackProtocolFactory()
        self.client = UnixClient(self.loop, self.factory, self.path)
        d = self.client.connect()
        d.add_callback(self.connected)
        return d

    def connected(self, result=None):
        d = self.factory.proxy(0)
        d.add_callback(self.set_proxy)

    def set_proxy(self, proxy):
        self.proxy = proxy

    def call(self, method, *args):
        return self.proxy.call(method, *args)

    def notify(self, method, *args):
        return self.proxy.notify(method, *args)

    def begin_call(self, method, *args):
        return self.proxy.begin_call(method, *args)
Exemplo n.º 3
0
class ServiceProxy(object):
    """Proxy to a service."""
    def __init__(self, loop, path):
        self.loop = loop
        self.path = path
        self.proxy = None

    def connect(self):
        self.factory = MsgPackProtocolFactory()
        self.client = UnixClient(self.loop, self.factory, self.path)
        d = self.client.connect()
        d.add_callback(self.connected)
        return d

    def connected(self, result=None):
        d = self.factory.proxy(0)
        d.add_callback(self.set_proxy)

    def set_proxy(self, proxy):
        self.proxy = proxy

    def call(self, method, *args):
        return self.proxy.call(method, *args)

    def notify(self, method, *args):
        return self.proxy.notify(method, *args)

    def begin_call(self, method, *args):
        return self.proxy.begin_call(method, *args)
Exemplo n.º 4
0
def main():
    path = 'adder_socket'
    loop = pyev.default_loop()

    sigwatcher = pyev.Signal(
        signal.SIGINT, loop,
        lambda watcher, events: watcher.loop.unloop(pyev.EVUNLOOP_ALL))
    sigwatcher.start()

    p = Process(loop, server_main, loop, 'adder_socket')
    p.start()

    pickle_factory = PickleProtocolFactory()
    pickle_client = UnixClient(loop, pickle_factory, path)

    retries = 10
    while retries:
        try:
            pickle_client.connect().result()
            retries = 0
        except Exception as e:
            time.sleep(0.1)
            retries -= 1

    proxy = pickle_factory.proxy(0).result()

    start = time.time()
    s = 0
    for i in range(10000):
        s = proxy.call('add', 1, s)
    stop = time.time()

    logger.info(
        'pickle-rpc took {} seconds to perform {} calls, {} calls per second',
        stop - start, s, s / (stop - start))

    start = time.time()
    for i in range(10000):
        proxy.notify('add', 1, s)
    proxy.call('add', 1, s)
    stop = time.time()

    logger.info(
        'pickle-rpc took {} seconds to perform {} notifications, {} notifies per second',
        stop - start, 10000, 10000 / (stop - start))

    msgpack_factory = MsgPackProtocolFactory()
    msgpack_client = UnixClient(loop, msgpack_factory, path + '_mp')

    retries = 10
    while retries:
        try:
            msgpack_client.connect().result()
            retries = 0
        except Exception as e:
            time.sleep(0.1)
            retries -= 1

    proxy = msgpack_factory.proxy(0).result()

    start = time.time()
    s = 0
    for i in range(10000):
        s = proxy.call('add', 1, s)
    stop = time.time()

    logger.info(
        'msgpack-rpc took {} seconds to perform {} calls, {} calls per second',
        stop - start, s, s / (stop - start))

    start = time.time()
    for i in range(10000):
        proxy.notify('add', 1, s)
    proxy.call('add', 1, s)
    stop = time.time()

    logger.info(
        'msgpack-rpc took {} seconds to perform {} notifications, {} notifies per second',
        stop - start, 10000, 10000 / (stop - start))

    p.stop()
Exemplo n.º 5
0
def main():
    path = 'adder_socket'
    loop = pyev.default_loop()

    sigwatcher = pyev.Signal(signal.SIGINT, loop, lambda watcher, events: watcher.loop.unloop(pyev.EVUNLOOP_ALL))
    sigwatcher.start()
    
    p = Process(loop, server_main, loop, 'adder_socket')
    p.start()

    pickle_factory = PickleProtocolFactory()
    pickle_client = UnixClient(loop, pickle_factory, path)

    retries = 10 
    while retries:
        try:
            pickle_client.connect().result()
            retries = 0
        except Exception as e:
            time.sleep(0.1)
            retries -= 1

    proxy = pickle_factory.proxy(0).result()
    
    start = time.time()
    s = 0
    for i in range(10000):
        s = proxy.call('add', 1, s)
    stop = time.time()

    logger.info('pickle-rpc took {} seconds to perform {} calls, {} calls per second', stop-start, s, s/(stop-start))

    start = time.time()
    for i in range(10000):
        proxy.notify('add', 1, s)
    proxy.call('add', 1, s)
    stop = time.time()

    logger.info('pickle-rpc took {} seconds to perform {} notifications, {} notifies per second', stop-start, 10000, 10000/(stop-start))

    msgpack_factory = MsgPackProtocolFactory()
    msgpack_client = UnixClient(loop, msgpack_factory, path + '_mp')

    retries = 10 
    while retries:
        try:
            msgpack_client.connect().result()
            retries = 0
        except Exception as e:
            time.sleep(0.1)
            retries -= 1

    proxy = msgpack_factory.proxy(0).result()
    
    start = time.time()
    s = 0
    for i in range(10000):
        s = proxy.call('add', 1, s)
    stop = time.time()

    logger.info('msgpack-rpc took {} seconds to perform {} calls, {} calls per second', stop-start, s, s/(stop-start))

    start = time.time()
    for i in range(10000):
        proxy.notify('add', 1, s)
    proxy.call('add', 1, s)
    stop = time.time()

    logger.info('msgpack-rpc took {} seconds to perform {} notifications, {} notifies per second', stop-start, 10000, 10000/(stop-start))

    p.stop()