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.

test_serializer.py 3.3 KiB

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