test_saferef.py 1.8 KB

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