test_saferef.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from __future__ import absolute_import, unicode_literals
  2. from celery.five import range
  3. from celery.utils.dispatch.saferef import safe_ref
  4. class Class1(object):
  5. def x(self):
  6. pass
  7. def fun(obj):
  8. pass
  9. class Class2(object):
  10. def __call__(self, obj):
  11. pass
  12. class test_safe_ref:
  13. def setup(self):
  14. ts = []
  15. ss = []
  16. for x in range(5000):
  17. t = Class1()
  18. ts.append(t)
  19. s = safe_ref(t.x, self._closure)
  20. ss.append(s)
  21. ts.append(fun)
  22. ss.append(safe_ref(fun, self._closure))
  23. for x in range(30):
  24. t = Class2()
  25. ts.append(t)
  26. s = safe_ref(t, self._closure)
  27. ss.append(s)
  28. self.ts = ts
  29. self.ss = ss
  30. self.closureCount = 0
  31. def test_in(self):
  32. """test_in
  33. Test the "in" operator for safe references (cmp)
  34. """
  35. for t in self.ts[:50]:
  36. assert safe_ref(t.x) in self.ss
  37. def test_valid(self):
  38. """test_value
  39. Test that the references are valid (return instance methods)
  40. """
  41. for s in self.ss:
  42. assert s()
  43. def test_shortcircuit(self):
  44. """test_shortcircuit
  45. Test that creation short-circuits to reuse existing references
  46. """
  47. sd = {}
  48. for s in self.ss:
  49. sd[s] = 1
  50. for t in self.ts:
  51. if hasattr(t, 'x'):
  52. assert safe_ref(t.x) in sd
  53. else:
  54. assert safe_ref(t) in sd
  55. def test_representation(self):
  56. """test_representation
  57. Test that the reference object's representation works
  58. XXX Doesn't currently check the results, just that no error
  59. is raised
  60. """
  61. repr(self.ss[-1])
  62. def _closure(self, ref):
  63. """Dumb utility mechanism to increment deletion counter"""
  64. self.closureCount += 1