Example #1
0
def _redis_modules():

    class ConnectionError(Exception):
        pass

    class AuthenticationError(Exception):
        pass

    class InvalidData(Exception):
        pass

    class InvalidResponse(Exception):
        pass

    class ResponseError(Exception):
        pass

    exceptions = types.ModuleType(bytes_if_py2('redis.exceptions'))
    exceptions.ConnectionError = ConnectionError
    exceptions.AuthenticationError = AuthenticationError
    exceptions.InvalidData = InvalidData
    exceptions.InvalidResponse = InvalidResponse
    exceptions.ResponseError = ResponseError

    class Redis(object):
        pass

    myredis = types.ModuleType(bytes_if_py2('redis'))
    myredis.exceptions = exceptions
    myredis.Redis = Redis

    return myredis, exceptions
Example #2
0
    def test_wraps(self):
        with patch('kombu.utils.debug.get_logger') as get_logger:
            logger = get_logger.return_value = Mock()

            W = Logwrapped(Mock(), 'kombu.test')
            get_logger.assert_called_with('kombu.test')
            assert W.instance is not None
            assert W.logger is logger

            W.instance.__repr__ = lambda s: bytes_if_py2('foo')
            assert repr(W) == 'foo'
            W.instance.some_attr = 303
            assert W.some_attr == 303

            W.instance.some_method.__name__ = bytes_if_py2('some_method')
            W.some_method(1, 2, kw=1)
            W.instance.some_method.assert_called_with(1, 2, kw=1)

            W.some_method()
            W.instance.some_method.assert_called_with()

            W.some_method(kw=1)
            W.instance.some_method.assert_called_with(kw=1)

            W.ident = 'ident'
            W.some_method(kw=1)
            logger.debug.assert_called()
            assert 'ident' in logger.debug.call_args[0][0]

            assert dir(W) == dir(W.instance)
Example #3
0
    def test_wraps(self):
        with patch('kombu.utils.debug.get_logger') as get_logger:
            logger = get_logger.return_value = Mock()

            W = Logwrapped(Mock(), 'kombu.test')
            get_logger.assert_called_with('kombu.test')
            self.assertIsNotNone(W.instance)
            self.assertIs(W.logger, logger)

            W.instance.__repr__ = lambda s: bytes_if_py2('foo')
            self.assertEqual(repr(W), 'foo')
            W.instance.some_attr = 303
            self.assertEqual(W.some_attr, 303)

            W.instance.some_method.__name__ = bytes_if_py2('some_method')
            W.some_method(1, 2, kw=1)
            W.instance.some_method.assert_called_with(1, 2, kw=1)

            W.some_method()
            W.instance.some_method.assert_called_with()

            W.some_method(kw=1)
            W.instance.some_method.assert_called_with(kw=1)

            W.ident = 'ident'
            W.some_method(kw=1)
            logger.debug.assert_called()
            self.assertIn('ident', logger.debug.call_args[0][0])

            self.assertEqual(dir(W), dir(W.instance))
Example #4
0
    def _configure_entity_tablenames(self, opts):
        self.queue_tablename = opts.get(
            bytes_if_py2('queue_tablename'), bytes_if_py2('kombu_queue'))
        self.message_tablename = opts.get(
            bytes_if_py2('message_tablename'), bytes_if_py2('kombu_message'))

        #
        # Define the model definitions.  This registers the declarative
        # classes with the active SQLAlchemy metadata object.  This *must* be
        # done prior to the ``create_engine`` call.
        #
        self.queue_cls and self.message_cls
Example #5
0
    def test_call_repeatedly(self):
        t = Timer()
        try:
            t.schedule.enter_after = Mock()

            myfun = Mock()
            myfun.__name__ = bytes_if_py2('myfun')
            t.call_repeatedly(0.03, myfun)

            assert t.schedule.enter_after.call_count == 1
            args1, _ = t.schedule.enter_after.call_args_list[0]
            sec1, tref1, _ = args1
            assert sec1 == 0.03
            tref1()

            assert t.schedule.enter_after.call_count == 2
            args2, _ = t.schedule.enter_after.call_args_list[1]
            sec2, tref2, _ = args2
            assert sec2 == 0.03
            tref2.canceled = True
            tref2()

            assert t.schedule.enter_after.call_count == 2
        finally:
            t.stop()
Example #6
0
    def test_call_repeatedly(self):
        t = Timer()
        try:
            t.schedule.enter_after = Mock()

            myfun = Mock()
            myfun.__name__ = bytes_if_py2("myfun")
            t.call_repeatedly(0.03, myfun)

            self.assertEqual(t.schedule.enter_after.call_count, 1)
            args1, _ = t.schedule.enter_after.call_args_list[0]
            sec1, tref1, _ = args1
            self.assertEqual(sec1, 0.03)
            tref1()

            self.assertEqual(t.schedule.enter_after.call_count, 2)
            args2, _ = t.schedule.enter_after.call_args_list[1]
            sec2, tref2, _ = args2
            self.assertEqual(sec2, 0.03)
            tref2.canceled = True
            tref2()

            self.assertEqual(t.schedule.enter_after.call_count, 2)
        finally:
            t.stop()
Example #7
0
 def test_detect_environment_no_eventlet_or_gevent(self):
     try:
         sys.modules['eventlet'] = types.ModuleType(
             bytes_if_py2('eventlet'))
         sys.modules['eventlet.patcher'] = types.ModuleType(
             bytes_if_py2('patcher'))
         assert compat._detect_environment() == 'default'
     finally:
         sys.modules.pop('eventlet.patcher', None)
         sys.modules.pop('eventlet', None)
     compat._detect_environment()
     try:
         sys.modules['gevent'] = types.ModuleType(bytes_if_py2('gevent'))
         assert compat._detect_environment() == 'default'
     finally:
         sys.modules.pop('gevent', None)
     compat._detect_environment()
Example #8
0
def test_generate_oid():
    from uuid import NAMESPACE_OID
    from kombu.five import bytes_if_py2

    instance = Mock()

    args = (1, 1001, 2001, id(instance))
    ent = bytes_if_py2('%x-%x-%x-%x' % args)

    with patch('kombu.common.uuid3') as mock_uuid3, \
            patch('kombu.common.uuid5') as mock_uuid5:
        mock_uuid3.side_effect = ValueError
        mock_uuid3.return_value = 'uuid3-6ba7b812-9dad-11d1-80b4'
        mock_uuid5.return_value = 'uuid5-6ba7b812-9dad-11d1-80b4'
        oid = generate_oid(1, 1001, 2001, instance)
        mock_uuid5.assert_called_once_with(NAMESPACE_OID, ent)
        assert oid == 'uuid5-6ba7b812-9dad-11d1-80b4'
Example #9
0
def test_generate_oid():
    from uuid import NAMESPACE_OID
    from kombu.five import bytes_if_py2

    instance = Mock()

    args = (1, 1001, 2001, id(instance))
    ent = bytes_if_py2('%x-%x-%x-%x' % args)

    with patch('kombu.common.uuid3') as mock_uuid3, \
            patch('kombu.common.uuid5') as mock_uuid5:
        mock_uuid3.side_effect = ValueError
        mock_uuid3.return_value = 'uuid3-6ba7b812-9dad-11d1-80b4'
        mock_uuid5.return_value = 'uuid5-6ba7b812-9dad-11d1-80b4'
        oid = generate_oid(1, 1001, 2001, instance)
        mock_uuid5.assert_called_once_with(NAMESPACE_OID, ent)
        assert oid == 'uuid5-6ba7b812-9dad-11d1-80b4'
Example #10
0
File: curl.py Project: Scalr/kombu
    import pycurl  # noqa
except ImportError:  # pragma: no cover
    pycurl = Curl = METH_TO_CURL = None  # noqa
else:
    from pycurl import Curl  # noqa

    METH_TO_CURL = {  # noqa
        'GET': pycurl.HTTPGET,
        'POST': pycurl.POST,
        'PUT': pycurl.UPLOAD,
        'HEAD': pycurl.NOBODY,
    }

__all__ = ['CurlClient']

DEFAULT_USER_AGENT = bytes_if_py2('Mozilla/5.0 (compatible; pycurl)')
EXTRA_METHODS = frozenset(['DELETE', 'OPTIONS', 'PATCH'])


class CurlClient(BaseClient):
    """Curl HTTP Client."""

    Curl = Curl

    def __init__(self, hub=None, max_clients=10):
        if pycurl is None:
            raise ImportError('The curl client requires the pycurl library.')
        hub = hub or get_event_loop()
        super(CurlClient, self).__init__(hub)
        self.max_clients = max_clients
Example #11
0
 def message_cls(self):
     return self._declarative_cls(bytes_if_py2('Message'), MessageBase,
                                  {'__tablename__': self.message_tablename})
Example #12
0
 def message_cls(self):
     return self._declarative_cls(
         bytes_if_py2('Message'),
         MessageBase,
         {'__tablename__': self.message_tablename}
     )
Example #13
0
 def queue_cls(self):
     return self._declarative_cls(
         bytes_if_py2('Queue'),
         QueueBase,
         {'__tablename__': self.queue_tablename}
     )
Example #14
0
File: url.py Project: braz/kombu
from __future__ import absolute_import, unicode_literals

from collections import Mapping
from functools import partial

try:
    from urllib.parse import parse_qsl, quote, unquote, urlparse
except ImportError:
    from urllib import quote, unquote                  # noqa
    from urlparse import urlparse, parse_qsl    # noqa

from kombu.five import bytes_if_py2, string_t

from .compat import NamedTuple

safequote = partial(quote, safe=bytes_if_py2(''))


urlparts = NamedTuple('urlparts', [
    ('scheme', str),
    ('hostname', str),
    ('port', int),
    ('username', str),
    ('password', str),
    ('path', str),
    ('query', Mapping),
])


def parse_url(url):
    # type: (str) -> Dict
Example #15
0
 def queue_cls(self):
     return self._declarative_cls(bytes_if_py2('Queue'), QueueBase,
                                  {'__tablename__': self.queue_tablename})
Example #16
0
    import pycurl  # noqa
except ImportError:  # pragma: no cover
    pycurl = Curl = METH_TO_CURL = None  # noqa
else:
    from pycurl import Curl  # noqa

    METH_TO_CURL = {  # noqa
        'GET': pycurl.HTTPGET,
        'POST': pycurl.POST,
        'PUT': pycurl.UPLOAD,
        'HEAD': pycurl.NOBODY,
    }

__all__ = ('CurlClient', )

DEFAULT_USER_AGENT = bytes_if_py2('Mozilla/5.0 (compatible; pycurl)')
EXTRA_METHODS = frozenset(['DELETE', 'OPTIONS', 'PATCH'])


class CurlClient(BaseClient):
    """Curl HTTP Client."""

    Curl = Curl

    def __init__(self, hub=None, max_clients=10):
        if pycurl is None:
            raise ImportError('The curl client requires the pycurl library.')
        hub = hub or get_event_loop()
        super(CurlClient, self).__init__(hub)
        self.max_clients = max_clients
Example #17
0
    from urllib.parse import parse_qsl, quote, unquote, urlparse
except ImportError:
    from urllib import quote, unquote                  # noqa
    from urlparse import urlparse, parse_qsl    # noqa
try:
    import ssl
    ssl_available = True
except ImportError:  # pragma: no cover
    ssl_available = False

from kombu.five import bytes_if_py2, string_t

from .compat import NamedTuple
from ..log import get_logger

safequote = partial(quote, safe=bytes_if_py2(''))
logger = get_logger(__name__)


urlparts = NamedTuple('urlparts', [
    ('scheme', str),
    ('hostname', str),
    ('port', int),
    ('username', str),
    ('password', str),
    ('path', str),
    ('query', Mapping),
])


def parse_url(url):