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.
 
 
 

123 lines
3.0 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. import threading
  7. import time
  8. from testutil.helpers import *
  9. class Foo(object):
  10. val = 0
  11. def __init__(self, asdf = "asdf"):
  12. self.init_thread = threading.current_thread().name
  13. @classmethod
  14. def foo(self):
  15. pass
  16. def fail(self):
  17. raise Exception("you asked me to do this")
  18. def test(self, debug = False):
  19. self.tester(debug)
  20. def t(self):
  21. pass
  22. def tester(self, debug = False):
  23. # purposely not thread-safe
  24. self.test_thread = threading.current_thread().name
  25. oldval = self.val
  26. newval = oldval + 1
  27. time.sleep(0.05)
  28. self.val = newval
  29. if debug:
  30. printf("[%s] value changed: %d -> %d\n",
  31. threading.current_thread().name, oldval, newval)
  32. class Base(object):
  33. def test_wrapping(self):
  34. self.foo.test()
  35. with assert_raises(Exception):
  36. self.foo.fail()
  37. def test_threaded(self):
  38. def func(foo):
  39. foo.test()
  40. threads = []
  41. for i in range(20):
  42. threads.append(threading.Thread(target = func, args = (self.foo,)))
  43. for t in threads:
  44. t.start()
  45. for t in threads:
  46. t.join()
  47. self.verify_result()
  48. def verify_result(self):
  49. eq_(self.foo.val, 20)
  50. eq_(self.foo.init_thread, self.foo.test_thread)
  51. class ListLike(object):
  52. def __init__(self):
  53. self.thread = threading.current_thread().name
  54. self.foo = 0
  55. def __iter__(self):
  56. eq_(threading.current_thread().name, self.thread)
  57. self.foo = 0
  58. return self
  59. def __getitem__(self, key):
  60. eq_(threading.current_thread().name, self.thread)
  61. return key
  62. def __next__(self):
  63. eq_(threading.current_thread().name, self.thread)
  64. if self.foo < 5:
  65. self.foo += 1
  66. return self.foo
  67. else:
  68. raise StopIteration
  69. class TestUnserialized(Base):
  70. def setUp(self):
  71. self.foo = Foo()
  72. def verify_result(self):
  73. # This should have failed to increment properly
  74. ne_(self.foo.val, 20)
  75. # Init and tests ran in different threads
  76. ne_(self.foo.init_thread, self.foo.test_thread)
  77. class TestSerializer(Base):
  78. def setUp(self):
  79. self.foo = nilmdb.utils.serializer_proxy(Foo)("qwer")
  80. def test_multi(self):
  81. sp = nilmdb.utils.serializer_proxy
  82. sp(Foo("x")).t()
  83. sp(sp(Foo)("x")).t()
  84. sp(sp(Foo))("x").t()
  85. sp(sp(Foo("x"))).t()
  86. sp(sp(Foo)("x")).t()
  87. sp(sp(Foo))("x").t()
  88. def test_iter(self):
  89. sp = nilmdb.utils.serializer_proxy
  90. i = sp(ListLike)()
  91. eq_(list(i), [1,2,3,4,5])
  92. eq_(i[3], 3)
  93. def test_del(self):
  94. sp = nilmdb.utils.serializer_proxy
  95. foo = sp(Foo("x"))
  96. # trigger exception in __del__, which should be ignored
  97. foo._SerializerObjectProxy__call_queue = None
  98. del foo