/usr/share/pyshared/kombu/tests/test_utils.py is in python-kombu 1.4.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | import pickle
import sys
from kombu.tests.utils import unittest
if sys.version_info >= (3, 0):
from io import StringIO, BytesIO
else:
from StringIO import StringIO, StringIO as BytesIO # noqa
from kombu import utils
from kombu.utils.functional import wraps
from kombu.tests.utils import redirect_stdouts, mask_modules, skip_if_module
partition = utils._compat_partition
rpartition = utils._compat_rpartition
class OldString(object):
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
def split(self, *args, **kwargs):
return self.value.split(*args, **kwargs)
def rsplit(self, *args, **kwargs):
return self.value.rsplit(*args, **kwargs)
class test_utils(unittest.TestCase):
def test_maybe_list(self):
self.assertEqual(utils.maybe_list(None), [])
self.assertEqual(utils.maybe_list(1), [1])
self.assertEqual(utils.maybe_list([1, 2, 3]), [1, 2, 3])
def assert_partition(self, p, t=str):
self.assertEqual(p(t("foo.bar.baz"), "."),
("foo", ".", "bar.baz"))
self.assertEqual(p(t("foo"), "."),
("foo", "", ""))
self.assertEqual(p(t("foo."), "."),
("foo", ".", ""))
self.assertEqual(p(t(".bar"), "."),
("", ".", "bar"))
self.assertEqual(p(t("."), "."),
('', ".", ''))
def assert_rpartition(self, p, t=str):
self.assertEqual(p(t("foo.bar.baz"), "."),
("foo.bar", ".", "baz"))
self.assertEqual(p(t("foo"), "."),
("", "", "foo"))
self.assertEqual(p(t("foo."), "."),
("foo", ".", ""))
self.assertEqual(p(t(".bar"), "."),
("", ".", "bar"))
self.assertEqual(p(t("."), "."),
('', ".", ''))
def test_compat_partition(self):
self.assert_partition(partition)
def test_compat_rpartition(self):
self.assert_rpartition(rpartition)
def test_partition(self):
self.assert_partition(utils.partition)
def test_rpartition(self):
self.assert_rpartition(utils.rpartition)
def test_partition_oldstr(self):
self.assert_partition(utils.partition, OldString)
def test_rpartition_oldstr(self):
self.assert_rpartition(utils.rpartition, OldString)
class test_UUID(unittest.TestCase):
def test_uuid4(self):
self.assertNotEqual(utils.uuid4(),
utils.uuid4())
def test_uuid(self):
i1 = utils.uuid()
i2 = utils.uuid()
self.assertIsInstance(i1, str)
self.assertNotEqual(i1, i2)
@skip_if_module('__pypy__')
def test_uuid_without_ctypes(self):
old_utils = sys.modules.pop("kombu.utils")
@mask_modules("ctypes")
def with_ctypes_masked():
from kombu.utils import ctypes, uuid
self.assertIsNone(ctypes)
tid = uuid()
self.assertTrue(tid)
self.assertIsInstance(tid, basestring)
try:
with_ctypes_masked()
finally:
sys.modules["celery.utils"] = old_utils
class test_Misc(unittest.TestCase):
def test_kwdict(self):
def f(**kwargs):
return kwargs
kw = {u"foo": "foo",
u"bar": "bar"}
self.assertTrue(f(**utils.kwdict(kw)))
class MyStringIO(StringIO):
def close(self):
pass
class MyBytesIO(BytesIO):
def close(self):
pass
class test_emergency_dump_state(unittest.TestCase):
@redirect_stdouts
def test_dump(self, stdout, stderr):
fh = MyBytesIO()
utils.emergency_dump_state({"foo": "bar"}, open_file=lambda n, m: fh)
self.assertDictEqual(pickle.loads(fh.getvalue()), {"foo": "bar"})
self.assertTrue(stderr.getvalue())
self.assertFalse(stdout.getvalue())
@redirect_stdouts
def test_dump_second_strategy(self, stdout, stderr):
fh = MyStringIO()
def raise_something(*args, **kwargs):
raise KeyError("foo")
utils.emergency_dump_state({"foo": "bar"}, open_file=lambda n, m: fh,
dump=raise_something)
self.assertIn("'foo': 'bar'", fh.getvalue())
self.assertTrue(stderr.getvalue())
self.assertFalse(stdout.getvalue())
_tried_to_sleep = [None]
def insomnia(fun):
@wraps(fun)
def _inner(*args, **kwargs):
_tried_to_sleep[0] = None
def mysleep(i):
_tried_to_sleep[0] = i
prev_sleep = utils.sleep
utils.sleep = mysleep
try:
return fun(*args, **kwargs)
finally:
utils.sleep = prev_sleep
return _inner
class test_retry_over_time(unittest.TestCase):
@insomnia
def test_simple(self):
index = [0]
class Predicate(Exception):
pass
def myfun():
sleepvals = {0: None,
1: 2.0,
2: 4.0,
3: 6.0,
4: 8.0,
5: 10.0,
6: 12.0,
7: 14.0,
8: 16.0,
9: 16.0}
self.assertEqual(_tried_to_sleep[0], sleepvals[index[0]])
if index[0] < 9:
raise Predicate()
return 42
def errback(exc, interval):
index[0] += 1
x = utils.retry_over_time(myfun, Predicate, errback=errback,
interval_max=14)
self.assertEqual(x, 42)
_tried_to_sleep[0] = None
index[0] = 0
self.assertRaises(Predicate,
utils.retry_over_time, myfun, Predicate,
max_retries=1, errback=errback, interval_max=14)
|