Esempio n. 1
0
class ZmqREQREPTwoFactoryConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.req_rep} with REQ/REP in two factories.
    """

    REQUEST_COUNT = 10000

    def setUp(self):
        self.factory1 = ZmqFactory()
        self.factory2 = ZmqFactory()
        c = ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:7859")
        self.c1 = ZmqRequestConnection(self.factory1, c, identity=b'master')
        b = ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:7859")
        self.c2 = ZmqReplyConnection(self.factory2, b, identity=b'slave')
        self.c1.d = defer.Deferred()

    def tearDown(self):
        self.factory2.shutdown()
        self.factory1.shutdown()

    def test_start(self):
        for _ in range(self.REQUEST_COUNT):
            reactor.callLater(0, self.c1.send, b'req')
        reactor.callLater(0, self.c1.send, b'stop')

        def checkResults(_):
            self.failUnlessEqual(self.c1.message_count, 3 * self.REQUEST_COUNT)
            self.failUnlessEqual(self.c2.message_count, self.REQUEST_COUNT)

        return self.c1.d.addCallback(checkResults)
Esempio n. 2
0
class ZmqRouterDealerTwoFactoryConnectionTestCase(unittest.TestCase):
    """
    Test case for L{txzmq.req_rep} with ROUTER/DEALER in two factories.
    """

    REQUEST_COUNT = 10000

    def setUp(self):
        self.factory1 = ZmqFactory()
        dealer_endpoint = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#7")
        self.dealer = ZmqTestDealerConnection(self.factory1, dealer_endpoint,
                                              identity='dealer')
        self.dealer.d = defer.Deferred()

        self.factory2 = ZmqFactory()
        router_endpoint = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#7")
        self.router = ZmqTestRouterConnection(self.factory2, router_endpoint,
                                              identity='router')

    def tearDown(self):
        self.factory2.shutdown()
        self.factory1.shutdown()

    def test_start(self):
        for _ in range(self.REQUEST_COUNT):
            reactor.callLater(0, self.dealer.sendMsg, 'req')
        reactor.callLater(0, self.dealer.sendMsg, 'stop')

        def checkResults(_):
            self.failUnlessEqual(self.dealer.message_count,
                                 3 * self.REQUEST_COUNT)
            self.failUnlessEqual(self.router.message_count, self.REQUEST_COUNT)

        return self.dealer.d.addCallback(checkResults)
Esempio n. 3
0
class ZmqREQREPTwoFactoryConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.req_rep} with REQ/REP in two factories.
    """

    REQUEST_COUNT = 10000

    def setUp(self):
        self.factory1 = ZmqFactory()
        self.factory2 = ZmqFactory()
        c = ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:7859")
        self.c1 = ZmqRequestConnection(self.factory1, c, identity=b'master')
        b = ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:7859")
        self.c2 = ZmqReplyConnection(self.factory2, b, identity=b'slave')
        self.c1.d = defer.Deferred()

    def tearDown(self):
        self.factory2.shutdown()
        self.factory1.shutdown()

    def test_start(self):
        for _ in range(self.REQUEST_COUNT):
            reactor.callLater(0, self.c1.send, b'req')
        reactor.callLater(0, self.c1.send, b'stop')

        def checkResults(_):
            self.failUnlessEqual(self.c1.message_count, 3 * self.REQUEST_COUNT)
            self.failUnlessEqual(self.c2.message_count, self.REQUEST_COUNT)

        return self.c1.d.addCallback(checkResults)
Esempio n. 4
0
 def setUp(self):
     self.factory1 = ZmqFactory()
     self.factory2 = ZmqFactory()
     c = ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:7859")
     self.c1 = ZmqRequestConnection(self.factory1, c, identity=b'master')
     b = ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:7859")
     self.c2 = ZmqReplyConnection(self.factory2, b, identity=b'slave')
     self.c1.d = defer.Deferred()
Esempio n. 5
0
 def setUp(self):
     self.factory1 = ZmqFactory()
     self.factory2 = ZmqFactory()
     c = ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:7859")
     self.c1 = ZmqRequestConnection(self.factory1, c, identity=b'master')
     b = ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:7859")
     self.c2 = ZmqReplyConnection(self.factory2, b, identity=b'slave')
     self.c1.d = defer.Deferred()
Esempio n. 6
0
class ZmqFactoryTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.factory.Factory}.
    """

    def setUp(self):
        self.factory = ZmqFactory()

    def test_shutdown(self):
        self.factory.shutdown()
Esempio n. 7
0
    def setUp(self):
        self.factory1 = ZmqFactory()
        dealer_endpoint = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#7")
        self.dealer = ZmqTestDealerConnection(self.factory1, dealer_endpoint,
                                              identity='dealer')
        self.dealer.d = defer.Deferred()

        self.factory2 = ZmqFactory()
        router_endpoint = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#7")
        self.router = ZmqTestRouterConnection(self.factory2, router_endpoint,
                                              identity='router')
Esempio n. 8
0
class BaseTestCase(unittest.TestCase):
    """
    This should be subclassed by the other test cases in this module.
    """
    def setUp(self):
        self.factory = ZmqFactory()
        self.factory.testMessage = ""
        ZmqPubConnection.allowLoopbackMulticast = True

    def tearDown(self):
        del ZmqPubConnection.allowLoopbackMulticast
        self.factory.shutdown()
Esempio n. 9
0
class BaseTestCase(unittest.TestCase):
    """
    This should be subclassed by the other test cases in this module.
    """
    def setUp(self):
        self.factory = ZmqFactory()
        self.factory.testMessage = ""
        ZmqPubConnection.allowLoopbackMulticast = True

    def tearDown(self):
        del ZmqPubConnection.allowLoopbackMulticast
        self.factory.shutdown()
    def setUp(self):
        self.factory1 = ZmqFactory()
        dealer_endpoint = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#7")
        self.dealer = ZmqTestDealerConnection(self.factory1,
                                              dealer_endpoint,
                                              identity='dealer')
        self.dealer.d = defer.Deferred()

        self.factory2 = ZmqFactory()
        router_endpoint = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#7")
        self.router = ZmqTestRouterConnection(self.factory2,
                                              router_endpoint,
                                              identity='router')
Esempio n. 11
0
    def setUp(self):
        self.factory = ZmqFactory()
        ZmqXREQConnection.identity = 'client'
        self.r = ZmqTestXREPConnection(
                ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3"))
        self.r.listen(self.factory)
        self.s = ZmqXREQConnection(
                ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3"))
        self.s.connect(self.factory)
        self.count = 0

        def get_next_id():
            self.count += 1
            return 'msg_id_%d' % (self.count,)

        self.s._getNextId = get_next_id
Esempio n. 12
0
 def setUp(self):
     self.factory = ZmqFactory()
     self.r = ZmqTestREPConnection(self.factory,
             ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3"))
     self.s = ZmqREQConnection(self.factory,
             ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3"),
             identity='client')
Esempio n. 13
0
    def setUp(self):
        self.factory = ZmqFactory()
        ZmqXREQConnection.identity = "client"
        self.r = ZmqTestXREPConnection(ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3"))
        self.r.listen(self.factory)
        self.s = ZmqXREQConnection(ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3"))
        self.s.connect(self.factory)
        self.count = 0

        def get_next_id():
            self.count += 1
            return "msg_id_%d" % (self.count,)

        self.s._getNextId = get_next_id
class ZmqRouterDealerTwoFactoryConnectionTestCase(unittest.TestCase):
    """
    Test case for L{txzmq.req_rep} with ROUTER/DEALER in two factories.
    """

    REQUEST_COUNT = 10000

    def setUp(self):
        self.factory1 = ZmqFactory()
        dealer_endpoint = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#7")
        self.dealer = ZmqTestDealerConnection(self.factory1,
                                              dealer_endpoint,
                                              identity='dealer')
        self.dealer.d = defer.Deferred()

        self.factory2 = ZmqFactory()
        router_endpoint = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#7")
        self.router = ZmqTestRouterConnection(self.factory2,
                                              router_endpoint,
                                              identity='router')

    def tearDown(self):
        self.factory2.shutdown()
        self.factory1.shutdown()

    def test_start(self):
        for _ in xrange(self.REQUEST_COUNT):
            reactor.callLater(0, self.dealer.sendMsg, 'req')
        reactor.callLater(0, self.dealer.sendMsg, 'stop')

        def checkResults(_):
            self.failUnlessEqual(self.dealer.message_count,
                                 3 * self.REQUEST_COUNT)
            self.failUnlessEqual(self.router.message_count, self.REQUEST_COUNT)

        return self.dealer.d.addCallback(checkResults)
Esempio n. 15
0
 def setUp(self):
     self.factory = ZmqFactory()
Esempio n. 16
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """
    def setUp(self):
        self.factory = ZmqFactory()

    def tearDown(self):
        self.factory.shutdown()

    def test_interfaces(self):
        ziv.verifyClass(IReadDescriptor, ZmqConnection)
        ziv.verifyClass(IFileDescriptor, ZmqConnection)

    def test_init(self):
        receiver = ZmqTestReceiver(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        sender = ZmqTestSender(
            self.factory, ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))
        # XXX perform some actual checks here

    def test_repr(self):
        expected = ("ZmqTestReceiver(ZmqFactory(), "
                    "(ZmqEndpoint(type='bind', address='inproc://#1'),))")
        r = ZmqTestReceiver(ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        r.connect(self.factory)
        self.failUnlessEqual(expected, repr(r))

    def test_send_recv(self):
        r = ZmqTestReceiver(ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        r.listen(self.factory)
        s = ZmqTestSender(ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))
        s.connect(self.factory)

        s.send('abcd')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['abcd']]
            self.failUnlessEqual(result, expected,
                                 "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_tcp(self):
        r = ZmqTestReceiver(
            ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5555"))
        r.listen(self.factory)
        s = ZmqTestSender(
            ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5555"))
        s.connect(self.factory)

        for i in xrange(100):
            s.send(str(i))

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = map(lambda i: [str(i)], xrange(100))
            self.failUnlessEqual(result, expected,
                                 "Messages should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_tcp_large(self):
        r = ZmqTestReceiver(
            ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5555"))
        r.listen(self.factory)
        s = ZmqTestSender(
            ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5555"))
        s.connect(self.factory)
        s.send(["0" * 10000, "1" * 10000])

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [["0" * 10000, "1" * 10000]]
            self.failUnlessEqual(result, expected,
                                 "Messages should have been received")

        return _wait(0.01).addCallback(check)

    def test_connect_success(self):
        def fakeConnectOrBind(ignored):
            self.factory.testMessage = "Fake success!"

        def check(ignored):
            self.assertEqual(self.factory.testMessage, "Fake success!")

        s = ZmqTestSender(ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))
        self.patch(s, '_connectOrBind', fakeConnectOrBind)
        d = s.connect(self.factory)
        d.addCallback(check)
        return d

    def test_connect_fail(self):
        def fakeConnectOrBind(factory):
            raise Exception("ohnoz!")

        def check(error):
            self.assertEqual(str(error), "exceptions.Exception: ohnoz!")

        s = ZmqTestSender(ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))
        self.patch(s, '_connectOrBind', fakeConnectOrBind)
        failure = s.connect(self.factory)
        d = self.assertFailure(failure, exceptions.ConnectionError)
        d.addCallback(check)
        return d

    def test_listen_success(self):
        def fakeConnectOrBind(ignored):
            self.factory.testMessage = "Fake success!"

        def check(ignored):
            self.assertEqual(self.factory.testMessage, "Fake success!")

        s = ZmqTestReceiver(ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        self.patch(s, '_connectOrBind', fakeConnectOrBind)
        d = s.listen(self.factory)
        d.addCallback(check)
        return d

    def test_listen_fail(self):
        def fakeConnectOrBind(factory):
            raise Exception("ohnoz!")

        def check(error):
            self.assertEqual(str(error), "exceptions.Exception: ohnoz!")

        s = ZmqTestReceiver(ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        self.patch(s, '_connectOrBind', fakeConnectOrBind)
        failure = s.listen(self.factory)
        d = self.assertFailure(failure, exceptions.ListenError)
        d.addCallback(check)
        return d
Esempio n. 17
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.

    In ZeroMQ 2.x, subscription is handled on receiving side:
    incoming messages are simply filtered, that's why connection.subscribe
    works immediately.

    In ZeroMQ 3.x, subscription is handled on publisher side:
    subscriber sends message to the publisher and publisher adjusts
    filtering on its side. So connection.subscribe doesn't start filtering
    immediately, it takes some time for messages to pass through the channel.
    """

    def setUp(self):
        self.factory = ZmqFactory()
        ZmqPubConnection.allowLoopbackMulticast = True

    def tearDown(self):
        del ZmqPubConnection.allowLoopbackMulticast
        self.factory.shutdown()

    def test_send_recv(self):
        r = ZmqTestSubConnection(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "ipc://test-sock"))
        s = ZmqPubConnection(
            self.factory, ZmqEndpoint(ZmqEndpointType.connect,
                                      "ipc://test-sock"))

        r.subscribe('tag')

        def publish(ignore):
            s.publish('xyz', 'different-tag')
            s.publish('abcd', 'tag1')
            s.publish('efgh', 'tag2')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['tag1', 'abcd'], ['tag2', 'efgh']]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.01).addCallback(publish) \
            .addCallback(lambda _: _wait(0.01)).addCallback(check)

    def test_send_recv_pgm(self):
        r = ZmqTestSubConnection(self.factory, ZmqEndpoint(
            ZmqEndpointType.bind, "epgm://127.0.0.1;239.192.1.1:5556"))

        s = ZmqPubConnection(self.factory, ZmqEndpoint(
            ZmqEndpointType.connect, "epgm://127.0.0.1;239.192.1.1:5556"))

        r.subscribe('tag')

        def publish(ignore):
            s.publish('xyz', 'different-tag')
            s.publish('abcd', 'tag1')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['tag1', 'abcd']]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.2).addCallback(publish) \
            .addCallback(lambda _: _wait(0.2)).addCallback(check)

    def test_send_recv_multiple_endpoints(self):
        r = ZmqTestSubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5556"))
        r.addEndpoints([ZmqEndpoint(ZmqEndpointType.bind,
                                    "inproc://endpoint")])
        s1 = ZmqPubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5556"))
        s2 = ZmqPubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect, "inproc://endpoint"))

        r.subscribe('')

        def publish(ignore):
            s1.publish('111', 'tag1')
            s2.publish('222', 'tag2')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['tag1', '111'], ['tag2', '222']]
            self.failUnlessEqual(
                sorted(result), expected, "Message should have been received")

        return _wait(0.1).addCallback(publish) \
            .addCallback(lambda _: _wait(0.1)).addCallback(check)

    if not _detect_epgm():
        test_send_recv_pgm.skip = "epgm:// not available"
Esempio n. 18
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """
    
    def make_one(self, *args, **kwargs):
        from txzmq.pair import ZmqPairConnection
        return ZmqPairConnection(self.factory, *args, **kwargs)
    
    def setUp(self):
        from txzmq.factory import ZmqFactory
        self.factory = ZmqFactory()

    def tearDown(self):
        self.factory.shutdown()

    def test_send_recv(self):
        from txzmq.test import _wait
        
        a_result = []
        def a_msg_func(msg):
            a_result.append(msg)
        
        b_result = []
        def b_msg_func(msg):
            b_result.append(msg)
            
        a = self.make_one(callback=a_msg_func)
        a.bind("inproc://#1")
        
        b = self.make_one(callback=b_msg_func)
        b.connect("inproc://#1")
        b.send(['b2a'])
        
        def check(ignore):
            expected = [['a2b']]
            self.assertEqual(b_result, expected)

        def check_and_send(ignore):
            expected = [['b2a']]
            self.assertEqual(a_result, expected)
            a.send(['a2b'])
            return _wait(0.1).addCallback(check)

        return _wait(0.1).addCallback(check_and_send)
    
    def test_send_lot(self):
        from txzmq.test import _wait
        
        a_result = []
        def a_msg_func(msg):
            a_result.append(msg)
        
        b_result = []
        def b_msg_func(msg):
            b_result.append(msg)
            
        a = self.make_one(callback=a_msg_func)
        a.bind("inproc://#1")
        
        b = self.make_one(callback=b_msg_func)
        b.connect("inproc://#1")
        
        a2b_msgs = [['a2b %d' % i] for i in range(100)]
        for m in a2b_msgs:
            a.send(m)
        b2a_msgs = [['b2a %d' % i] for i in range(100)]
        for m in b2a_msgs:
            b.send(m)
        
        def check(ignore):
            self.assertEqual(a_result, b2a_msgs)
            self.assertEqual(b_result, a2b_msgs)

        return _wait(0.1).addCallback(check)
Esempio n. 19
0
class ZmqREQREPConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.req_rep.ZmqREPConnection}.
    """

    def setUp(self):
        self.factory = ZmqFactory()
        b = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3")
        self.r = ZmqTestREPConnection(self.factory, b)
        c = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3")
        self.s = ZmqREQConnection(self.factory, c, identity=b'client')

    def tearDown(self):
        self.factory.shutdown()

    def test_getNextId(self):
        self.failUnlessEqual([], self.s._uuids)
        id1 = self.s._getNextId()
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE - 1, len(self.s._uuids))
        self.failUnlessIsInstance(id1, binary_string_type)

        id2 = self.s._getNextId()
        self.failUnlessIsInstance(id2, binary_string_type)

        self.failIfEqual(id1, id2)

        ids = [self.s._getNextId() for _ in range(1000)]
        self.failUnlessEqual(len(ids), len(set(ids)))

    def test_releaseId(self):
        self.s._releaseId(self.s._getNextId())
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))

    def test_send_recv(self):
        self.count = 0

        def get_next_id():
            self.count += 1
            return b'msg_id_' + str(self.count).encode()

        self.s._getNextId = get_next_id

        self.s.sendMsg(b'aaa', b'aab')
        self.s.sendMsg(b'bbb')

        def check(ignore):
            result = getattr(self.r, 'messages', [])
            expected = [[b'msg_id_1', (b'aaa', b'aab')],
                        [b'msg_id_2', (b'bbb',)]]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_reply(self):
        d = self.s.sendMsg(b'aaa')

        def check_response(response):
            self.assertEqual(response, [b'aaa'])

        d.addCallback(check_response)
        return d

    def test_lot_send_recv_reply(self):
        deferreds = []
        for i in range(10):
            msg_id = "msg_id_%d" % (i,)
            d = self.s.sendMsg(b'aaa')

            def check_response(response, msg_id):
                self.assertEqual(response, [b'aaa'])

            d.addCallback(check_response, msg_id)
            deferreds.append(d)
        return defer.DeferredList(deferreds, fireOnOneErrback=True)

    def test_cleanup_requests(self):
        """The request dict is cleanedup properly."""
        def check(ignore):
            self.assertEqual(self.s._requests, {})
            self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))

        return self.s.sendMsg(b'aaa').addCallback(check)

    def test_cancel(self):
        d = self.s.sendMsg(b'aaa')
        d.cancel()

        def check_requests(_):
            self.assertEqual(self.s._requests, {})
            self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE,
                                 len(self.s._uuids) + 1)

        return d.addCallbacks(lambda _: self.fail("Should have errored"),
                              lambda fail: fail.trap(
                              "twisted.internet.defer.CancelledError")) \
            .addCallback(check_requests) \
            .addCallback(lambda _: _wait(0.01))

    def test_send_timeout_ok(self):
        return self.s.sendMsg(b'aaa', timeout=0.1).addCallback(
            lambda response: self.assertEquals(response, [b'aaa'])
        )

    def test_send_timeout_fail(self):
        b = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#4")
        ZmqSlowREPConnection(self.factory, b)
        c = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#4")
        s = ZmqREQConnection(self.factory, c, identity=b'client2')

        return s.sendMsg(b'aaa', timeout=0.05) \
            .addCallbacks(lambda _: self.fail("Should timeout"),
                          lambda fail: fail.trap(ZmqRequestTimeoutError)) \
            .addCallback(lambda _: _wait(0.1))
Esempio n. 20
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """
    
    def make_push(self, *args, **kwargs):
        from txzmq.pushpull import ZmqPushConnection
        return ZmqPushConnection(self.factory, *args, **kwargs)

    def make_pull(self, *args, **kwargs):
        from txzmq.pushpull import ZmqPullConnection
        return ZmqPullConnection(self.factory, *args, **kwargs)
    
    def setUp(self):
        from txzmq.factory import ZmqFactory
        self.factory = ZmqFactory()

    def tearDown(self):
        self.factory.shutdown()

    def test_send_recv(self):
        from txzmq.test import _wait
        
        result = []
        def msg_func(msg):
            result.append(msg)
        
        pull = self.make_pull(callback=msg_func)
        pull.bind("inproc://#1")
        
        push = self.make_push()
        push.connect("inproc://#1")
        push.send(['abcd'])

        def check(ignore):
            expected = [['abcd']]
            self.assertEqual(result, expected)

        return _wait(0.1).addCallback(check)
    
    def test_send_to_multiple_nodes(self):
        from txzmq.test import _wait
        
        result = []
        def msg_func(msg):
            result.append(msg)
        
        pull1 = self.make_pull(callback=msg_func)
        pull1.bind("inproc://#1")
        
        pull2 = self.make_pull(callback=msg_func)
        pull2.bind("inproc://#2")
        
        pull3 = self.make_pull(callback=msg_func)
        pull3.bind("inproc://#3")
        
        push = self.make_push()
        push.connect("inproc://#1")
        push.connect("inproc://#2")
        push.connect("inproc://#3")
        
        for i in range(1, 6+1):
            push.send(['msg%d' % i])
            
        def check(ignore):
            for i in range(1, 6+1):
                self.assertIn(['msg1'], result)

        return _wait(0.1).addCallback(check)
Esempio n. 21
0
class ZmqREQREPConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.req_rep.ZmqREPConnection}.
    """

    def setUp(self):
        self.factory = ZmqFactory()
        self.r = ZmqTestREPConnection(self.factory,
                ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3"))
        self.s = ZmqREQConnection(self.factory,
                ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3"),
                identity='client')

    def tearDown(self):
        self.factory.shutdown()

    def test_getNextId(self):
        self.failUnlessEqual([], self.s._uuids)
        id1 = self.s._getNextId()
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE - 1, len(self.s._uuids))
        self.failUnlessIsInstance(id1, str)

        id2 = self.s._getNextId()
        self.failUnlessIsInstance(id2, str)

        self.failIfEqual(id1, id2)

        ids = [self.s._getNextId() for _ in range(1000)]
        self.failUnlessEqual(len(ids), len(set(ids)))

    def test_releaseId(self):
        self.s._releaseId(self.s._getNextId())
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))

    def test_send_recv(self):
        self.count = 0

        def get_next_id():
            self.count += 1
            return 'msg_id_%d' % (self.count,)

        self.s._getNextId = get_next_id

        self.s.sendMsg('aaa', 'aab')
        self.s.sendMsg('bbb')

        def check(ignore):
            result = getattr(self.r, 'messages', [])
            expected = [['msg_id_1', ('aaa', 'aab')], ['msg_id_2', ('bbb',)]]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_reply(self):
        d = self.s.sendMsg('aaa')

        def check_response(response):
            self.assertEqual(response, ['aaa'])

        d.addCallback(check_response)
        return d

    def test_lot_send_recv_reply(self):
        deferreds = []
        for i in range(10):
            msg_id = "msg_id_%d" % (i,)
            d = self.s.sendMsg('aaa')

            def check_response(response, msg_id):
                self.assertEqual(response, ['aaa'])

            d.addCallback(check_response, msg_id)
            deferreds.append(d)
        return defer.DeferredList(deferreds, fireOnOneErrback=True)

    def test_cleanup_requests(self):
        """The request dict is cleanedup properly."""
        def check(ignore):
            self.assertEqual(self.s._requests, {})
            self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))

        return self.s.sendMsg('aaa').addCallback(check)
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """

    def setUp(self):
        self.factory = ZmqFactory()

    def tearDown(self):
        self.factory.shutdown()

    def test_interfaces(self):
        ziv.verifyClass(IReadDescriptor, ZmqConnection)
        ziv.verifyClass(IFileDescriptor, ZmqConnection)

    def test_init(self):
        receiver = ZmqTestReceiver(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        sender = ZmqTestSender(
            self.factory, ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))
        # XXX perform some actual checks here

    def test_repr(self):
        expected = ("ZmqTestReceiver(ZmqFactory(), "
                    "(ZmqEndpoint(type='bind', address='inproc://#1'),))")
        r = ZmqTestReceiver(
            ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        r.connect(self.factory)
        self.failUnlessEqual(expected, repr(r))

    def test_send_recv(self):
        r = ZmqTestReceiver(
            ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        r.listen(self.factory)
        s = ZmqTestSender(
            ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))
        s.connect(self.factory)

        s.send('abcd')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['abcd']]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_tcp(self):
        r = ZmqTestReceiver(
            ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5555"))
        r.listen(self.factory)
        s = ZmqTestSender(
            ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5555"))
        s.connect(self.factory)

        for i in xrange(100):
            s.send(str(i))

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = map(lambda i: [str(i)], xrange(100))
            self.failUnlessEqual(
                result, expected, "Messages should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_tcp_large(self):
        r = ZmqTestReceiver(
            ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5555"))
        r.listen(self.factory)
        s = ZmqTestSender(
            ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5555"))
        s.connect(self.factory)
        s.send(["0" * 10000, "1" * 10000])

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [["0" * 10000, "1" * 10000]]
            self.failUnlessEqual(
                result, expected, "Messages should have been received")

        return _wait(0.01).addCallback(check)

    def test_connect_success(self):

        def fakeConnectOrBind(ignored):
            self.factory.testMessage = "Fake success!"

        def check(ignored):
            self.assertEqual(self.factory.testMessage, "Fake success!")

        s = ZmqTestSender(
            ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))
        self.patch(s, '_connectOrBind', fakeConnectOrBind)
        d = s.connect(self.factory)
        d.addCallback(check)
        return d

    def test_connect_fail(self):

        def fakeConnectOrBind(factory):
            raise Exception("ohnoz!")

        def check(error):
            self.assertEqual(str(error), "exceptions.Exception: ohnoz!")

        s = ZmqTestSender(
            ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))
        self.patch(s, '_connectOrBind', fakeConnectOrBind)
        failure = s.connect(self.factory)
        d = self.assertFailure(failure, exceptions.ConnectionError)
        d.addCallback(check)
        return d

    def test_listen_success(self):

        def fakeConnectOrBind(ignored):
            self.factory.testMessage = "Fake success!"

        def check(ignored):
            self.assertEqual(self.factory.testMessage, "Fake success!")

        s = ZmqTestReceiver(
            ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        self.patch(s, '_connectOrBind', fakeConnectOrBind)
        d = s.listen(self.factory)
        d.addCallback(check)
        return d

    def test_listen_fail(self):

        def fakeConnectOrBind(factory):
            raise Exception("ohnoz!")

        def check(error):
            self.assertEqual(str(error), "exceptions.Exception: ohnoz!")

        s = ZmqTestReceiver(
            ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        self.patch(s, '_connectOrBind', fakeConnectOrBind)
        failure = s.listen(self.factory)
        d = self.assertFailure(failure, exceptions.ListenError)
        d.addCallback(check)
        return d
Esempio n. 23
0
 def setUp(self):
     self.factory = ZmqFactory()
     ZmqPubConnection.allowLoopbackMulticast = True
Esempio n. 24
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.

    In ZeroMQ 2.x, subscription is handled on receiving side:
    incoming messages are simply filtered, that's why connection.subscribe
    works immediately.

    In ZeroMQ 3.x, subscription is handled on publisher side:
    subscriber sends message to the publisher and publisher adjusts
    filtering on its side. So connection.subscribe doesn't start filtering
    immediately, it takes some time for messages to pass through the channel.
    """
    def setUp(self):
        self.factory = ZmqFactory()
        ZmqPubConnection.allowLoopbackMulticast = True

    def tearDown(self):
        del ZmqPubConnection.allowLoopbackMulticast
        self.factory.shutdown()

    def test_send_recv(self):
        r = ZmqTestSubConnection(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "ipc://test-sock"))
        s = ZmqPubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect, "ipc://test-sock"))

        r.subscribe('tag')

        def publish(ignore):
            s.publish('xyz', 'different-tag')
            s.publish('abcd', 'tag1')
            s.publish('efgh', 'tag2')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['tag1', 'abcd'], ['tag2', 'efgh']]
            self.failUnlessEqual(result, expected,
                                 "Message should have been received")

        return _wait(0.01).addCallback(publish) \
            .addCallback(lambda _: _wait(0.01)).addCallback(check)

    def test_send_recv_pgm(self):
        r = ZmqTestSubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.bind,
                        "epgm://127.0.0.1;239.192.1.1:5556"))

        s = ZmqPubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect,
                        "epgm://127.0.0.1;239.192.1.1:5556"))

        r.subscribe('tag')

        def publish(ignore):
            s.publish('xyz', 'different-tag')
            s.publish('abcd', 'tag1')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['tag1', 'abcd']]
            self.failUnlessEqual(result, expected,
                                 "Message should have been received")

        return _wait(0.2).addCallback(publish) \
            .addCallback(lambda _: _wait(0.2)).addCallback(check)

    def test_send_recv_multiple_endpoints(self):
        r = ZmqTestSubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5556"))
        r.addEndpoints(
            [ZmqEndpoint(ZmqEndpointType.bind, "inproc://endpoint")])
        s1 = ZmqPubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5556"))
        s2 = ZmqPubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect, "inproc://endpoint"))

        r.subscribe('')

        def publish(ignore):
            s1.publish('111', 'tag1')
            s2.publish('222', 'tag2')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['tag1', '111'], ['tag2', '222']]
            self.failUnlessEqual(sorted(result), expected,
                                 "Message should have been received")

        return _wait(0.1).addCallback(publish) \
            .addCallback(lambda _: _wait(0.1)).addCallback(check)

    if not _detect_epgm():
        test_send_recv_pgm.skip = "epgm:// not available"
Esempio n. 25
0
 def setUp(self):
     self.factory = ZmqFactory()
     b = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3")
     self.r = ZmqTestREPConnection(self.factory, b)
     c = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3")
     self.s = ZmqREQConnection(self.factory, c, identity=b'client')
Esempio n. 26
0
class ZmqREQREPConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.req_rep.ZmqREPConnection}.
    """

    def setUp(self):
        self.factory = ZmqFactory()
        b = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3")
        self.r = ZmqTestREPConnection(self.factory, b)
        c = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3")
        self.s = ZmqREQConnection(self.factory, c, identity=b'client')

    def tearDown(self):
        self.factory.shutdown()

    def test_getNextId(self):
        self.failUnlessEqual([], self.s._uuids)
        id1 = self.s._getNextId()
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE - 1, len(self.s._uuids))
        self.failUnlessIsInstance(id1, binary_string_type)

        id2 = self.s._getNextId()
        self.failUnlessIsInstance(id2, binary_string_type)

        self.failIfEqual(id1, id2)

        ids = [self.s._getNextId() for _ in range(1000)]
        self.failUnlessEqual(len(ids), len(set(ids)))

    def test_releaseId(self):
        self.s._releaseId(self.s._getNextId())
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))

    def test_send_recv(self):
        self.count = 0

        def get_next_id():
            self.count += 1
            return b'msg_id_' + str(self.count).encode()

        self.s._getNextId = get_next_id

        self.s.sendMsg(b'aaa', b'aab')
        self.s.sendMsg(b'bbb')

        def check(ignore):
            result = getattr(self.r, 'messages', [])
            expected = [[b'msg_id_1', (b'aaa', b'aab')],
                        [b'msg_id_2', (b'bbb',)]]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_reply(self):
        d = self.s.sendMsg(b'aaa')

        def check_response(response):
            self.assertEqual(response, [b'aaa'])

        d.addCallback(check_response)
        return d

    def test_lot_send_recv_reply(self):
        deferreds = []
        for i in range(10):
            msg_id = "msg_id_%d" % (i,)
            d = self.s.sendMsg(b'aaa')

            def check_response(response, msg_id):
                self.assertEqual(response, [b'aaa'])

            d.addCallback(check_response, msg_id)
            deferreds.append(d)
        return defer.DeferredList(deferreds, fireOnOneErrback=True)

    def test_cleanup_requests(self):
        """The request dict is cleanedup properly."""
        def check(ignore):
            self.assertEqual(self.s._requests, {})
            self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))

        return self.s.sendMsg(b'aaa').addCallback(check)

    def test_cancel(self):
        d = self.s.sendMsg(b'aaa')
        d.cancel()

        def check_requests(_):
            self.assertEqual(self.s._requests, {})
            self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE,
                                 len(self.s._uuids) + 1)

        return d.addCallbacks(lambda _: self.fail("Should have errored"),
                              lambda fail: fail.trap(
                              "twisted.internet.defer.CancelledError")) \
            .addCallback(check_requests) \
            .addCallback(lambda _: _wait(0.01))

    def test_cancel_with_timeout(self):
        d = self.s.sendMsg(b'aaa', timeout=10.0)
        d.cancel()

        def check_requests(_):
            self.assertEqual(self.s._requests, {})
            self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE,
                                 len(self.s._uuids) + 1)

        return d.addCallbacks(lambda _: self.fail("Should have errored"),
                              lambda fail: fail.trap(
                              "twisted.internet.defer.CancelledError")) \
            .addCallback(check_requests) \
            .addCallback(lambda _: _wait(0.01))

    def test_send_timeout_ok(self):
        return self.s.sendMsg(b'aaa', timeout=0.1).addCallback(
            lambda response: self.assertEquals(response, [b'aaa'])
        )

    def test_send_timeout_fail(self):
        b = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#4")
        ZmqSlowREPConnection(self.factory, b)
        c = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#4")
        s = ZmqREQConnection(self.factory, c, identity=b'client2')

        return s.sendMsg(b'aaa', timeout=0.05) \
            .addCallbacks(lambda _: self.fail("Should timeout"),
                          lambda fail: fail.trap(ZmqRequestTimeoutError)) \
            .addCallback(lambda _: _wait(0.1))
Esempio n. 27
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """
    
    def make_pub(self, *args, **kwargs):
        from txzmq.pubsub import ZmqPubConnection
        return ZmqPubConnection(self.factory, *args, **kwargs)

    def make_sub(self, *args, **kwargs):
        from txzmq.pubsub import ZmqSubConnection
        return ZmqSubConnection(self.factory, *args, **kwargs)

    def setUp(self):
        from txzmq.factory import ZmqFactory
        self.factory = ZmqFactory()

    def tearDown(self):
        self.factory.shutdown()

    def test_pub_sub(self):
        import zmq
        from txzmq.test import _wait
        
        result = []
        def msg_func(msg):
            result.append(msg)
        
        sub = self.make_sub(callback=msg_func)
        sub.bind('inproc://#1')
        sub.setsockopt(zmq.SUBSCRIBE, '')
        
        pub = self.make_pub()
        pub.connect('inproc://#1')
        
        pub.send(['a', 'b', 'c'])
        pub.send(['1', '2', '3'])
        
        def check(ignore):
            expected = [['a', 'b', 'c'], ['1', '2', '3']]
            self.assertEqual(result, expected)

        return _wait(0.1).addCallback(check)
    
    def test_pub_sub_with_filter(self):
        import zmq
        from txzmq.test import _wait
        
        result = []
        def msg_func(msg):
            result.append(msg)
        
        sub = self.make_sub(callback=msg_func)
        sub.bind('inproc://#1')
        sub.setsockopt(zmq.SUBSCRIBE, 'a')
        
        pub = self.make_pub()
        pub.connect('inproc://#1')
        
        pub.send(['a', 'data1'])
        pub.send(['b', 'data2'])
        pub.send(['a', 'data3'])
        pub.send(['a', 'data4'])
        
        def check(ignore):
            expected = [['a', 'data1'], ['a', 'data3'], ['a', 'data4']]
            self.assertEqual(result, expected)

        return _wait(0.1).addCallback(check)
Esempio n. 28
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """

    def setUp(self):
        self.factory = ZmqFactory()
        ZmqPubConnection.allowLoopbackMulticast = True

    def tearDown(self):
        del ZmqPubConnection.allowLoopbackMulticast
        self.factory.shutdown()

    def test_send_recv(self):
        r = ZmqTestSubConnection(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "ipc://test-sock"))
        s = ZmqPubConnection(
            self.factory, ZmqEndpoint(ZmqEndpointType.connect,
            "ipc://test-sock"))

        r.subscribe('tag')
        s.publish('xyz', 'different-tag')
        s.publish('abcd', 'tag1')
        s.publish('efgh', 'tag2')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['tag1', 'abcd'], ['tag2', 'efgh']]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_pgm(self):
        r = ZmqTestSubConnection(self.factory, ZmqEndpoint(
            ZmqEndpointType.bind, "epgm://127.0.0.1;239.192.1.1:5556"))
        s = ZmqPubConnection(self.factory, ZmqEndpoint(
            ZmqEndpointType.connect, "epgm://127.0.0.1;239.192.1.1:5556"))

        r.subscribe('tag')
        s.publish('xyz', 'different-tag')
        s.publish('abcd', 'tag1')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['tag1', 'abcd']]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.2).addCallback(check)

    def test_send_recv_multiple_endpoints(self):
        r = ZmqTestSubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5556"))
        r.addEndpoints(
                [ZmqEndpoint(ZmqEndpointType.bind, "inproc://endpoint")])
        s1 = ZmqPubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5556"))
        s2 = ZmqPubConnection(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect, "inproc://endpoint"))

        r.subscribe('')
        s1.publish('111', 'tag1')
        s2.publish('222', 'tag2')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['tag1', '111'], ['tag2', '222']]
            self.failUnlessEqual(
                sorted(result), expected, "Message should have been received")

        return _wait(0.2).addCallback(check)
Esempio n. 29
0
 def _test():
     factory = ZmqFactory()
     factory.reactor = reactor
     factory.registerForShutdown()
     reactor.stop()
Esempio n. 30
0
class ZmqREQREPConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.req_rep.ZmqREPConnection}.
    """
    def setUp(self):
        self.factory = ZmqFactory()
        b = ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3")
        self.r = ZmqTestREPConnection(self.factory, b)
        c = ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3")
        self.s = ZmqREQConnection(self.factory, c, identity='client')

    def tearDown(self):
        self.factory.shutdown()

    def test_getNextId(self):
        self.failUnlessEqual([], self.s._uuids)
        id1 = self.s._getNextId()
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE - 1, len(self.s._uuids))
        self.failUnlessIsInstance(id1, str)

        id2 = self.s._getNextId()
        self.failUnlessIsInstance(id2, str)

        self.failIfEqual(id1, id2)

        ids = [self.s._getNextId() for _ in range(1000)]
        self.failUnlessEqual(len(ids), len(set(ids)))

    def test_releaseId(self):
        self.s._releaseId(self.s._getNextId())
        self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))

    def test_send_recv(self):
        self.count = 0

        def get_next_id():
            self.count += 1
            return 'msg_id_%d' % (self.count, )

        self.s._getNextId = get_next_id

        self.s.sendMsg('aaa', 'aab')
        self.s.sendMsg('bbb')

        def check(ignore):
            result = getattr(self.r, 'messages', [])
            expected = [['msg_id_1', ('aaa', 'aab')], ['msg_id_2', ('bbb', )]]
            self.failUnlessEqual(result, expected,
                                 "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_reply(self):
        d = self.s.sendMsg('aaa')

        def check_response(response):
            self.assertEqual(response, ['aaa'])

        d.addCallback(check_response)
        return d

    def test_lot_send_recv_reply(self):
        deferreds = []
        for i in range(10):
            msg_id = "msg_id_%d" % (i, )
            d = self.s.sendMsg('aaa')

            def check_response(response, msg_id):
                self.assertEqual(response, ['aaa'])

            d.addCallback(check_response, msg_id)
            deferreds.append(d)
        return defer.DeferredList(deferreds, fireOnOneErrback=True)

    def test_cleanup_requests(self):
        """The request dict is cleanedup properly."""
        def check(ignore):
            self.assertEqual(self.s._requests, {})
            self.failUnlessEqual(self.s.UUID_POOL_GEN_SIZE, len(self.s._uuids))

        return self.s.sendMsg('aaa').addCallback(check)
Esempio n. 31
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """

    def setUp(self):
        self.factory = ZmqFactory()
        ZmqXREQConnection.identity = 'client'
        self.r = ZmqTestXREPConnection(
                ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3"))
        self.r.listen(self.factory)
        self.s = ZmqXREQConnection(
                ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3"))
        self.s.connect(self.factory)
        self.count = 0

        def get_next_id():
            self.count += 1
            return 'msg_id_%d' % (self.count,)

        self.s._getNextId = get_next_id

    def tearDown(self):
        ZmqXREQConnection.identity = None
        self.factory.shutdown()

    def test_send_recv(self):
        self.s.sendMsg('aaa', 'aab')
        self.s.sendMsg('bbb')

        def check(ignore):
            result = getattr(self.r, 'messages', [])
            expected = [['msg_id_1', ('aaa', 'aab')], ['msg_id_2', ('bbb',)]]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_reply(self):
        d = self.s.sendMsg('aaa')

        def check_response(response):
            self.assertEqual(response, ['aaa'])

        d.addCallback(check_response)
        return d

    def test_lot_send_recv_reply(self):
        deferreds = []
        for i in range(10):
            msg_id = "msg_id_%d" % (i,)
            d = self.s.sendMsg('aaa')

            def check_response(response, msg_id):
                self.assertEqual(response, ['aaa'])

            d.addCallback(check_response, msg_id)
            deferreds.append(d)
        return defer.DeferredList(deferreds)

    def xtest_cleanup_requests(self):
        """The request dict is cleanedup properly."""
        def check(ignore):
            self.assertEqual(self.s._requests, {})

        return self.s.sendMsg('aaa').addCallback(check)
Esempio n. 32
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """

    def setUp(self):
        self.factory = ZmqFactory()
        ZmqXREQConnection.identity = "client"
        self.r = ZmqTestXREPConnection(ZmqEndpoint(ZmqEndpointType.bind, "ipc://#3"))
        self.r.listen(self.factory)
        self.s = ZmqXREQConnection(ZmqEndpoint(ZmqEndpointType.connect, "ipc://#3"))
        self.s.connect(self.factory)
        self.count = 0

        def get_next_id():
            self.count += 1
            return "msg_id_%d" % (self.count,)

        self.s._getNextId = get_next_id

    def tearDown(self):
        ZmqXREQConnection.identity = None
        self.factory.shutdown()

    def test_send_recv(self):
        self.s.sendMsg("aaa", "aab")
        self.s.sendMsg("bbb")

        def check(ignore):
            result = getattr(self.r, "messages", [])
            expected = [["msg_id_1", ("aaa", "aab")], ["msg_id_2", ("bbb",)]]
            self.failUnlessEqual(result, expected, "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_reply(self):
        d = self.s.sendMsg("aaa")

        def check_response(response):
            self.assertEqual(response, ["aaa"])

        d.addCallback(check_response)
        return d

    def test_lot_send_recv_reply(self):
        deferreds = []
        for i in range(10):
            msg_id = "msg_id_%d" % (i,)
            d = self.s.sendMsg("aaa")

            def check_response(response, msg_id):
                self.assertEqual(response, ["aaa"])

            d.addCallback(check_response, msg_id)
            deferreds.append(d)
        return defer.DeferredList(deferreds)

    def xtest_cleanup_requests(self):
        """The request dict is cleanedup properly."""

        def check(ignore):
            self.assertEqual(self.s._requests, {})

        return self.s.sendMsg("aaa").addCallback(check)
Esempio n. 33
0
 def setUp(self):
     self.factory = ZmqFactory()
     self.factory.testMessage = ""
     ZmqPubConnection.allowLoopbackMulticast = True
Esempio n. 34
0
 def setUp(self):
     from txzmq.factory import ZmqFactory
     self.factory = ZmqFactory()
Esempio n. 35
0
 def setUp(self):
     self.factory = ZmqFactory()
     self.factory.testMessage = ""
     ZmqPubConnection.allowLoopbackMulticast = True
Esempio n. 36
0
 def setUp(self):
     self.factory = ZmqFactory()
Esempio n. 37
0
 def _test():
     factory = ZmqFactory()
     factory.reactor = reactor
     factory.registerForShutdown()
     reactor.stop()
Esempio n. 38
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """

    def setUp(self):
        self.factory = ZmqFactory()

    def tearDown(self):
        self.factory.shutdown()

    def test_interfaces(self):
        ziv.verifyClass(IReadDescriptor, ZmqConnection)
        ziv.verifyClass(IFileDescriptor, ZmqConnection)

    def test_init(self):
        ZmqTestReceiver(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        ZmqTestSender(
            self.factory, ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))

    def test_addEndpoints(self):
        r = ZmqTestReceiver(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        r.addEndpoints([ZmqEndpoint(ZmqEndpointType.bind, "inproc://#2"),
                        ZmqEndpoint(ZmqEndpointType.bind, "inproc://#3")])

        s = ZmqTestSender(
            self.factory, ZmqEndpoint(ZmqEndpointType.connect, "inproc://#3"))

        s.send('abcd')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['abcd']]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_repr(self):
        expected = ("ZmqTestReceiver(ZmqFactory(), "
                    "[ZmqEndpoint(type='bind', address='inproc://#1')])")
        result = ZmqTestReceiver(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        self.failUnlessEqual(expected, repr(result))

    def test_send_recv(self):
        r = ZmqTestReceiver(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        s = ZmqTestSender(
            self.factory, ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))

        s.send('abcd')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['abcd']]
            self.failUnlessEqual(
                result, expected, "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_tcp(self):
        r = ZmqTestReceiver(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind,
                                      "tcp://127.0.0.1:5555"))
        s = ZmqTestSender(
            self.factory, ZmqEndpoint(ZmqEndpointType.connect,
                                      "tcp://127.0.0.1:5555"))

        for i in xrange(100):
            s.send(str(i))

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = map(lambda i: [str(i)], xrange(100))
            self.failUnlessEqual(
                result, expected, "Messages should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_tcp_large(self):
        r = ZmqTestReceiver(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind,
                                      "tcp://127.0.0.1:5555"))
        s = ZmqTestSender(
            self.factory, ZmqEndpoint(ZmqEndpointType.connect,
                                      "tcp://127.0.0.1:5555"))

        s.send(["0" * 10000, "1" * 10000])

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [["0" * 10000, "1" * 10000]]
            self.failUnlessEqual(
                result, expected, "Messages should have been received")

        return _wait(0.01).addCallback(check)
Esempio n. 39
0
 def setUp(self):
     self.factory = ZmqFactory()
     ZmqPubConnection.allowLoopbackMulticast = True
Esempio n. 40
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """
    
    def make_one(self, *args, **kwargs):
        from txzmq.connection import ZmqConnection
        return ZmqConnection(self.factory, *args, **kwargs)

    def setUp(self):
        from txzmq.factory import ZmqFactory
        self.factory = ZmqFactory()

    def tearDown(self):
        self.factory.shutdown()

    def test_interfaces(self):
        from zope.interface import verify as ziv
        from twisted.internet.interfaces import IFileDescriptor, IReadDescriptor
        from txzmq.connection import ZmqConnection
        ziv.verifyClass(IReadDescriptor, ZmqConnection)
        ziv.verifyClass(IFileDescriptor, ZmqConnection)

    def test_send_recv(self):
        import zmq
        from txzmq.test import _wait
        
        result = []
        def msg_func(msg):
            result.append(msg)
        
        r = self.make_one(self.factory.context.socket(zmq.PULL), msg_func)
        r.bind("inproc://#1")
        
        s = self.make_one(self.factory.context.socket(zmq.PUSH))
        s.connect("inproc://#1")
        s.send(['abcd'])

        def check(ignore):
            expected = [['abcd']]
            self.assertEqual(result, expected)

        return _wait(0.1).addCallback(check)

    def test_send_recv_tcp(self):
        import zmq
        from txzmq.test import _wait
        
        result = []
        def msg_func(msg):
            result.append(msg)
        
        r = self.make_one(self.factory.context.socket(zmq.PULL), msg_func)
        r.bind("tcp://127.0.0.1:5555")
        
        s = self.make_one(self.factory.context.socket(zmq.PUSH))
        s.connect("tcp://127.0.0.1:5555")
        
        msgs = [[str(i)] for i in xrange(100)]
        for m in msgs:
            s.send(m)

        def check(ignore):
            self.assertEqual(result, msgs)

        return _wait(0.1).addCallback(check)
Esempio n. 41
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """
    
    def make_req(self, *args, **kwargs):
        from txzmq.reqrep import ZmqReqConnection
        return ZmqReqConnection(self.factory, *args, **kwargs)

    def make_rep(self, *args, **kwargs):
        from txzmq.reqrep import ZmqRepConnection
        return ZmqRepConnection(self.factory, *args, **kwargs)

    def setUp(self):
        from txzmq.factory import ZmqFactory
        self.factory = ZmqFactory()

    def tearDown(self):
        self.factory.shutdown()

    def test_send_recv(self):
        from txzmq.test import _wait
        
        result = []
        def msg_func(msg_id, msg_parts):
            result.append(msg_parts)
        
        rep = self.make_rep(callback=msg_func)
        rep.bind('inproc://#1')
            
        req = self.make_req()
        req.connect('inproc://#1')
        req.request(['hello', 'baby'])

        def check(ignore):
            expected = [['hello', 'baby']]
            self.assertEqual(result, expected)

        return _wait(0.1).addCallback(check)

    def test_send_recv_reply(self):
        from twisted.internet import reactor
        from twisted.internet import defer
        
        def reply_func(msg_id, msg_parts):
            rep.reply(msg_id, ['reply'] + msg_parts)
        
        rep = self.make_rep(callback=reply_func)
        rep.bind('inproc://#1')
            
        req = self.make_req()
        req.connect('inproc://#1')
        
        d = req.request(['hello', 'baby'])
        
        def timeout():
            if not d.called:
                defer.timeout(d)
        call_id = reactor.callLater(0.1, timeout)
        
        def check(msg):
            expected = ['reply', 'hello', 'baby']
            self.assertEqual(msg, expected)
            if not call_id.called:
                call_id.cancel()
        d.addCallback(check)
            
        return d
    
    def test_request_with_timeout(self):
        rep = self.make_rep(callback=lambda msg_id, msg_parts: None)
        rep.bind('inproc://no-reply')
        # connect to a non-exist 
        req = self.make_req()
        req.connect('inproc://no-reply')

        from twisted.internet import task
        clock = task.Clock()
                
        d = req.request(['a', 'b', 'c'], timeout=5, call_later=clock.callLater)
        def check_timeout(failure):
            from twisted.internet.defer import TimeoutError
            self.assertEqual(failure.type, TimeoutError)
        d.addBoth(check_timeout)
        
        clock.advance(11)
        return d
    
    def test_request_with_timeout_not_trigged(self):
        from twisted.internet import reactor
        from twisted.internet import defer
        
        def reply_func(msg_id, msg_parts):
            rep.reply(msg_id, ['reply'] + msg_parts)
        
        rep = self.make_rep(callback=reply_func)
        rep.bind('inproc://#1')
            
        req = self.make_req()
        req.connect('inproc://#1')
        
        d = req.request(['hello', 'baby'], timeout=10)
        
        def timeout():
            if not d.called:
                defer.timeout(d)
        call_id = reactor.callLater(0.1, timeout)
        
        def check(msg):
            expected = ['reply', 'hello', 'baby']
            self.assertEqual(msg, expected)
            if not call_id.called:
                call_id.cancel()
        d.addCallback(check)
        return d
            
    def test_read_signal_up_when_send_bug(self):
        """Reference to https://github.com/smira/txZMQ/pull/3
        
        """
        import zmq
        import threading
        from twisted.internet import reactor
        from twisted.internet import defer
        
        rep = zmq.Context.instance().socket(zmq.REP)
        rep.bind('tcp://127.0.0.1:5555')
        def reply():
            for _ in range(2):
                msgs = rep.recv_multipart()
                rep.send_multipart(['reply'] + msgs)
        thread = threading.Thread(target=reply)
        thread.start()
            
        req = self.make_req()
        req.connect('tcp://127.0.0.1:5555')
        
        req.request(['a'])
        # wait until the request is replied
        # then FD will be notified with read signal, if we don't check
        # socket events in send() and try to read something, then
        # we have no second chance to be notified, then the reading
        # is stopped. If we do read in send, then it will make sure
        # the event is reset by reading, FD can then be notified later
        while True:
            events = req.socket.getsockopt(zmq.EVENTS)
            if (events & zmq.POLLIN) == zmq.POLLIN:
                break
        
        d = req.request(['hello', 'baby'])
        
        def timeout():
            if not d.called:
                defer.timeout(d)
        call_id = reactor.callLater(1, timeout)
        
        def check(msg):
            expected = ['reply', 'hello', 'baby']
            self.assertEqual(msg, expected)
            if not call_id.called:
                call_id.cancel()
        d.addCallback(check)
            
        return d
    
    def test_routing_info_clean_up(self):
        def reply_func(msg_id, msg_parts):
            rep.reply(msg_id, ['reply'] + msg_parts)
        
        rep = self.make_rep(callback=reply_func)
        rep.bind('inproc://#1')
        
        req = self.make_req()
        req.connect('inproc://#1')
        d = req.request(['hello', 'baby'], timeout=10)
        def callback(result):
            self.assertNot(rep._routing_info)
            return result
        d.addCallback(callback)
        return d
    
    def test_timeout_clalls_clean_up(self):
        def reply_func(msg_id, msg_parts):
            rep.reply(msg_id, ['reply'] + msg_parts)
        
        rep = self.make_rep(callback=reply_func)
        rep.bind('inproc://#1')
        
        req = self.make_req()
        req.connect('inproc://#1')
        d = req.request(['hello', 'baby'], timeout=10)
        def callback(result):
            self.assertNot(req._timeout_calls)
            return result
        d.addCallback(callback)
        return d
    
    def test_requests_clean_up(self):
        def reply_func(msg_id, msg_parts):
            rep.reply(msg_id, ['reply'] + msg_parts)
        
        rep = self.make_rep(callback=reply_func)
        rep.bind('inproc://#1')
        
        req = self.make_req()
        req.connect('inproc://#1')
        d = req.request(['hello', 'baby'], timeout=10)
        def callback(result):
            self.assertNot(req._requests)
            return result
        d.addCallback(callback)
        return d
    
    def test_deprecated_class_name(self):
        import warnings
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            
            from txzmq import ZmqXREQConnection
            ZmqXREQConnection(self.factory)
            
            self.assertEqual(len(w), 1)
            self.assert_(issubclass(w[-1].category, DeprecationWarning))
            self.assertIn("deprecated", str(w[-1].message).lower())
            
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            
            from txzmq import ZmqXREPConnection
            ZmqXREPConnection(self.factory, lambda: None)
            
            self.assertEqual(len(w), 1)
            self.assert_(issubclass(w[-1].category, DeprecationWarning))
            self.assertIn("deprecated", str(w[-1].message).lower())
Esempio n. 42
0
class ZmqConnectionTestCase(unittest.TestCase):
    """
    Test case for L{zmq.twisted.connection.Connection}.
    """
    def setUp(self):
        self.factory = ZmqFactory()

    def tearDown(self):
        self.factory.shutdown()

    def test_interfaces(self):
        ziv.verifyClass(IReadDescriptor, ZmqConnection)
        ziv.verifyClass(IFileDescriptor, ZmqConnection)

    def test_init(self):
        ZmqTestReceiver(self.factory,
                        ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        ZmqTestSender(self.factory,
                      ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))

    def test_addEndpoints(self):
        r = ZmqTestReceiver(self.factory,
                            ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        r.addEndpoints([
            ZmqEndpoint(ZmqEndpointType.bind, "inproc://#2"),
            ZmqEndpoint(ZmqEndpointType.bind, "inproc://#3")
        ])

        s = ZmqTestSender(self.factory,
                          ZmqEndpoint(ZmqEndpointType.connect, "inproc://#3"))

        s.send('abcd')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['abcd']]
            self.failUnlessEqual(result, expected,
                                 "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_repr(self):
        expected = ("ZmqTestReceiver(ZmqFactory(), "
                    "[ZmqEndpoint(type='bind', address='inproc://#1')])")
        result = ZmqTestReceiver(
            self.factory, ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        self.failUnlessEqual(expected, repr(result))

    def test_send_recv(self):
        r = ZmqTestReceiver(self.factory,
                            ZmqEndpoint(ZmqEndpointType.bind, "inproc://#1"))
        s = ZmqTestSender(self.factory,
                          ZmqEndpoint(ZmqEndpointType.connect, "inproc://#1"))

        s.send('abcd')

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [['abcd']]
            self.failUnlessEqual(result, expected,
                                 "Message should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_tcp(self):
        r = ZmqTestReceiver(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5555"))
        s = ZmqTestSender(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5555"))

        for i in xrange(100):
            s.send(str(i))

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = map(lambda i: [str(i)], xrange(100))
            self.failUnlessEqual(result, expected,
                                 "Messages should have been received")

        return _wait(0.01).addCallback(check)

    def test_send_recv_tcp_large(self):
        r = ZmqTestReceiver(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.bind, "tcp://127.0.0.1:5555"))
        s = ZmqTestSender(
            self.factory,
            ZmqEndpoint(ZmqEndpointType.connect, "tcp://127.0.0.1:5555"))

        s.send(["0" * 10000, "1" * 10000])

        def check(ignore):
            result = getattr(r, 'messages', [])
            expected = [["0" * 10000, "1" * 10000]]
            self.failUnlessEqual(result, expected,
                                 "Messages should have been received")

        return _wait(0.01).addCallback(check)