You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

97 lines
2.7 KiB

  1. import nilmdb
  2. from nilmdb.utils.printf import *
  3. import nose
  4. from nose.tools import *
  5. from nose.tools import assert_raises
  6. from testutil.helpers import *
  7. import threading
  8. class Thread(threading.Thread):
  9. def __init__(self, target):
  10. self.target = target
  11. threading.Thread.__init__(self)
  12. def run(self):
  13. try:
  14. self.target()
  15. except AssertionError as e:
  16. self.error = e
  17. else:
  18. self.error = None
  19. class Test():
  20. def __init__(self):
  21. self.test = 1234
  22. @classmethod
  23. def asdf(cls):
  24. pass
  25. def foo(self, exception = False, reenter = False):
  26. if exception:
  27. raise Exception()
  28. self.bar(reenter)
  29. def bar(self, reenter):
  30. if reenter:
  31. self.foo()
  32. return 123
  33. def baz_threaded(self, target):
  34. t = Thread(target)
  35. t.start()
  36. t.join()
  37. return t
  38. def baz(self, target):
  39. target()
  40. class TestThreadSafety(object):
  41. def tryit(self, c, threading_ok, concurrent_ok):
  42. eq_(c.test, 1234)
  43. c.foo()
  44. t = Thread(c.foo)
  45. t.start()
  46. t.join()
  47. if threading_ok and t.error:
  48. raise Exception("got unexpected error: " + str(t.error))
  49. if not threading_ok and not t.error:
  50. raise Exception("failed to get expected error")
  51. try:
  52. c.baz(c.foo)
  53. except AssertionError as e:
  54. if concurrent_ok:
  55. raise Exception("got unexpected error: " + str(e))
  56. else:
  57. if not concurrent_ok:
  58. raise Exception("failed to get expected error")
  59. t = c.baz_threaded(c.foo)
  60. if (concurrent_ok and threading_ok) and t.error:
  61. raise Exception("got unexpected error: " + str(t.error))
  62. if not (concurrent_ok and threading_ok) and not t.error:
  63. raise Exception("failed to get expected error")
  64. def test(self):
  65. proxy = nilmdb.utils.threadsafety.verify_proxy
  66. self.tryit(Test(), True, True)
  67. self.tryit(proxy(Test(), True, True, True), False, False)
  68. self.tryit(proxy(Test(), True, True, False), False, True)
  69. self.tryit(proxy(Test(), True, False, True), True, False)
  70. self.tryit(proxy(Test(), True, False, False), True, True)
  71. self.tryit(proxy(Test, True, True, True)(), False, False)
  72. self.tryit(proxy(Test, True, True, False)(), False, True)
  73. self.tryit(proxy(Test, True, False, True)(), True, False)
  74. self.tryit(proxy(Test, True, False, False)(), True, True)
  75. proxy(proxy(proxy(Test))()).foo()
  76. c = proxy(Test())
  77. c.foo()
  78. try:
  79. c.foo(exception = True)
  80. except Exception:
  81. pass
  82. c.foo()