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.
 
 
 

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