Esempio n. 1
0
    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. 2
0
    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. 3
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. 4
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)
Esempio n. 5
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. 6
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)