test_datastructures.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. from __future__ import absolute_import
  2. import pickle
  3. import sys
  4. from billiard.einfo import ExceptionInfo
  5. from time import time
  6. from celery.datastructures import (
  7. LimitedSet,
  8. AttributeDict,
  9. DictAttribute,
  10. ConfigurationView,
  11. DependencyGraph,
  12. )
  13. from celery.five import items
  14. from celery.tests.case import Case, Mock, WhateverIO, SkipTest, patch
  15. class Object(object):
  16. pass
  17. class test_DictAttribute(Case):
  18. def test_get_set_keys_values_items(self):
  19. x = DictAttribute(Object())
  20. x['foo'] = 'The quick brown fox'
  21. self.assertEqual(x['foo'], 'The quick brown fox')
  22. self.assertEqual(x['foo'], x.obj.foo)
  23. self.assertEqual(x.get('foo'), 'The quick brown fox')
  24. self.assertIsNone(x.get('bar'))
  25. with self.assertRaises(KeyError):
  26. x['bar']
  27. x.foo = 'The quick yellow fox'
  28. self.assertEqual(x['foo'], 'The quick yellow fox')
  29. self.assertIn(
  30. ('foo', 'The quick yellow fox'),
  31. list(x.items()),
  32. )
  33. self.assertIn('foo', list(x.keys()))
  34. self.assertIn('The quick yellow fox', list(x.values()))
  35. def test_setdefault(self):
  36. x = DictAttribute(Object())
  37. self.assertEqual(x.setdefault('foo', 'NEW'), 'NEW')
  38. self.assertEqual(x.setdefault('foo', 'XYZ'), 'NEW')
  39. def test_contains(self):
  40. x = DictAttribute(Object())
  41. x['foo'] = 1
  42. self.assertIn('foo', x)
  43. self.assertNotIn('bar', x)
  44. def test_items(self):
  45. obj = Object()
  46. obj.attr1 = 1
  47. x = DictAttribute(obj)
  48. x['attr2'] = 2
  49. self.assertEqual(x['attr1'], 1)
  50. self.assertEqual(x['attr2'], 2)
  51. class test_ConfigurationView(Case):
  52. def setUp(self):
  53. self.view = ConfigurationView({'changed_key': 1,
  54. 'both': 2},
  55. [{'default_key': 1,
  56. 'both': 1}])
  57. def test_setdefault(self):
  58. self.assertEqual(self.view.setdefault('both', 36), 2)
  59. self.assertEqual(self.view.setdefault('new', 36), 36)
  60. def test_get(self):
  61. self.assertEqual(self.view.get('both'), 2)
  62. sp = object()
  63. self.assertIs(self.view.get('nonexisting', sp), sp)
  64. def test_update(self):
  65. changes = dict(self.view.changes)
  66. self.view.update(a=1, b=2, c=3)
  67. self.assertDictEqual(self.view.changes,
  68. dict(changes, a=1, b=2, c=3))
  69. def test_contains(self):
  70. self.assertIn('changed_key', self.view)
  71. self.assertIn('default_key', self.view)
  72. self.assertNotIn('new', self.view)
  73. def test_repr(self):
  74. self.assertIn('changed_key', repr(self.view))
  75. self.assertIn('default_key', repr(self.view))
  76. def test_iter(self):
  77. expected = {'changed_key': 1,
  78. 'default_key': 1,
  79. 'both': 2}
  80. self.assertDictEqual(dict(items(self.view)), expected)
  81. self.assertItemsEqual(list(iter(self.view)),
  82. list(expected.keys()))
  83. self.assertItemsEqual(list(self.view.keys()), list(expected.keys()))
  84. self.assertItemsEqual(
  85. list(self.view.values()),
  86. list(expected.values()),
  87. )
  88. self.assertIn('changed_key', list(self.view.keys()))
  89. self.assertIn(2, list(self.view.values()))
  90. self.assertIn(('both', 2), list(self.view.items()))
  91. def test_add_defaults_dict(self):
  92. defaults = {'foo': 10}
  93. self.view.add_defaults(defaults)
  94. self.assertEqual(self.view.foo, 10)
  95. def test_add_defaults_object(self):
  96. defaults = Object()
  97. defaults.foo = 10
  98. self.view.add_defaults(defaults)
  99. self.assertEqual(self.view.foo, 10)
  100. def test_clear(self):
  101. self.view.clear()
  102. self.assertEqual(self.view.both, 1)
  103. self.assertNotIn('changed_key', self.view)
  104. def test_bool(self):
  105. self.assertTrue(bool(self.view))
  106. self.view._order[:] = []
  107. self.assertFalse(bool(self.view))
  108. def test_len(self):
  109. self.assertEqual(len(self.view), 3)
  110. self.view.KEY = 33
  111. self.assertEqual(len(self.view), 4)
  112. self.view.clear()
  113. self.assertEqual(len(self.view), 2)
  114. def test_isa_mapping(self):
  115. from collections import Mapping
  116. self.assertTrue(issubclass(ConfigurationView, Mapping))
  117. def test_isa_mutable_mapping(self):
  118. from collections import MutableMapping
  119. self.assertTrue(issubclass(ConfigurationView, MutableMapping))
  120. class test_ExceptionInfo(Case):
  121. def test_exception_info(self):
  122. try:
  123. raise LookupError('The quick brown fox jumps...')
  124. except Exception:
  125. einfo = ExceptionInfo()
  126. self.assertEqual(str(einfo), einfo.traceback)
  127. self.assertIsInstance(einfo.exception, LookupError)
  128. self.assertTupleEqual(
  129. einfo.exception.args, ('The quick brown fox jumps...', ),
  130. )
  131. self.assertTrue(einfo.traceback)
  132. r = repr(einfo)
  133. self.assertTrue(r)
  134. class test_LimitedSet(Case):
  135. def setUp(self):
  136. if sys.platform == 'win32':
  137. raise SkipTest('Not working on Windows')
  138. def test_add(self):
  139. if sys.platform == 'win32':
  140. raise SkipTest('Not working properly on Windows')
  141. s = LimitedSet(maxlen=2)
  142. s.add('foo')
  143. s.add('bar')
  144. for n in 'foo', 'bar':
  145. self.assertIn(n, s)
  146. s.add('baz')
  147. for n in 'bar', 'baz':
  148. self.assertIn(n, s)
  149. self.assertNotIn('foo', s)
  150. def test_purge(self):
  151. s = LimitedSet(maxlen=None)
  152. [s.add(i) for i in range(10)]
  153. s.maxlen = 2
  154. s.purge(1)
  155. self.assertEqual(len(s), 9)
  156. s.purge(None)
  157. self.assertEqual(len(s), 2)
  158. # expired
  159. s = LimitedSet(maxlen=None, expires=1)
  160. [s.add(i) for i in range(10)]
  161. s.maxlen = 2
  162. s.purge(1, now=lambda: time() + 100)
  163. self.assertEqual(len(s), 9)
  164. s.purge(None, now=lambda: time() + 100)
  165. self.assertEqual(len(s), 2)
  166. # not expired
  167. s = LimitedSet(maxlen=None, expires=1)
  168. [s.add(i) for i in range(10)]
  169. s.maxlen = 2
  170. s.purge(1, now=lambda: time() - 100)
  171. self.assertEqual(len(s), 10)
  172. s.purge(None, now=lambda: time() - 100)
  173. self.assertEqual(len(s), 10)
  174. s = LimitedSet(maxlen=None)
  175. [s.add(i) for i in range(10)]
  176. s.maxlen = 2
  177. with patch('celery.datastructures.heappop') as hp:
  178. hp.side_effect = IndexError()
  179. s.purge()
  180. hp.assert_called_with(s._heap)
  181. with patch('celery.datastructures.heappop') as hp:
  182. s._data = dict((i * 2, i * 2) for i in range(10))
  183. s.purge()
  184. self.assertEqual(hp.call_count, 10)
  185. def test_pickleable(self):
  186. s = LimitedSet(maxlen=2)
  187. s.add('foo')
  188. s.add('bar')
  189. self.assertEqual(pickle.loads(pickle.dumps(s)), s)
  190. def test_iter(self):
  191. if sys.platform == 'win32':
  192. raise SkipTest('Not working on Windows')
  193. s = LimitedSet(maxlen=3)
  194. items = ['foo', 'bar', 'baz', 'xaz']
  195. for item in items:
  196. s.add(item)
  197. l = list(iter(s))
  198. for item in items[1:]:
  199. self.assertIn(item, l)
  200. self.assertNotIn('foo', l)
  201. self.assertListEqual(l, items[1:], 'order by insertion time')
  202. def test_repr(self):
  203. s = LimitedSet(maxlen=2)
  204. items = 'foo', 'bar'
  205. for item in items:
  206. s.add(item)
  207. self.assertIn('LimitedSet(', repr(s))
  208. def test_discard(self):
  209. s = LimitedSet(maxlen=2)
  210. s.add('foo')
  211. s.discard('foo')
  212. self.assertNotIn('foo', s)
  213. s.discard('foo')
  214. def test_clear(self):
  215. s = LimitedSet(maxlen=2)
  216. s.add('foo')
  217. s.add('bar')
  218. self.assertEqual(len(s), 2)
  219. s.clear()
  220. self.assertFalse(s)
  221. def test_update(self):
  222. s1 = LimitedSet(maxlen=2)
  223. s1.add('foo')
  224. s1.add('bar')
  225. s2 = LimitedSet(maxlen=2)
  226. s2.update(s1)
  227. self.assertItemsEqual(list(s2), ['foo', 'bar'])
  228. s2.update(['bla'])
  229. self.assertItemsEqual(list(s2), ['bla', 'bar'])
  230. s2.update(['do', 're'])
  231. self.assertItemsEqual(list(s2), ['do', 're'])
  232. def test_as_dict(self):
  233. s = LimitedSet(maxlen=2)
  234. s.add('foo')
  235. self.assertIsInstance(s.as_dict(), dict)
  236. class test_AttributeDict(Case):
  237. def test_getattr__setattr(self):
  238. x = AttributeDict({'foo': 'bar'})
  239. self.assertEqual(x['foo'], 'bar')
  240. with self.assertRaises(AttributeError):
  241. x.bar
  242. x.bar = 'foo'
  243. self.assertEqual(x['bar'], 'foo')
  244. class test_DependencyGraph(Case):
  245. def graph1(self):
  246. return DependencyGraph([
  247. ('A', []),
  248. ('B', []),
  249. ('C', ['A']),
  250. ('D', ['C', 'B']),
  251. ])
  252. def test_repr(self):
  253. self.assertTrue(repr(self.graph1()))
  254. def test_topsort(self):
  255. order = self.graph1().topsort()
  256. # C must start before D
  257. self.assertLess(order.index('C'), order.index('D'))
  258. # and B must start before D
  259. self.assertLess(order.index('B'), order.index('D'))
  260. # and A must start before C
  261. self.assertLess(order.index('A'), order.index('C'))
  262. def test_edges(self):
  263. self.assertItemsEqual(
  264. list(self.graph1().edges()),
  265. ['C', 'D'],
  266. )
  267. def test_connect(self):
  268. x, y = self.graph1(), self.graph1()
  269. x.connect(y)
  270. def test_valency_of_when_missing(self):
  271. x = self.graph1()
  272. self.assertEqual(x.valency_of('foobarbaz'), 0)
  273. def test_format(self):
  274. x = self.graph1()
  275. x.formatter = Mock()
  276. obj = Mock()
  277. self.assertTrue(x.format(obj))
  278. x.formatter.assert_called_with(obj)
  279. x.formatter = None
  280. self.assertIs(x.format(obj), obj)
  281. def test_items(self):
  282. self.assertDictEqual(
  283. dict(items(self.graph1())),
  284. {'A': [], 'B': [], 'C': ['A'], 'D': ['C', 'B']},
  285. )
  286. def test_repr_node(self):
  287. x = self.graph1()
  288. self.assertTrue(x.repr_node('fasdswewqewq'))
  289. def test_to_dot(self):
  290. s = WhateverIO()
  291. self.graph1().to_dot(s)
  292. self.assertTrue(s.getvalue())