2012-01-26 20:27:21 -05:00
|
|
|
import nilmdb
|
2012-12-31 15:52:28 -05:00
|
|
|
from nilmdb.utils.printf import *
|
2012-01-26 20:27:21 -05:00
|
|
|
|
2012-01-27 19:36:23 -05:00
|
|
|
import nose
|
2012-01-26 20:27:21 -05:00
|
|
|
from nose.tools import *
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2013-01-05 15:00:34 -05:00
|
|
|
from testutil.helpers import *
|
2012-03-29 17:43:05 -04:00
|
|
|
|
2012-01-26 20:27:21 -05:00
|
|
|
class Foo(object):
|
|
|
|
val = 0
|
|
|
|
|
2013-02-22 18:26:17 -05:00
|
|
|
def __init__(self, asdf = "asdf"):
|
2013-02-21 15:19:35 -05:00
|
|
|
self.init_thread = threading.current_thread().name
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def foo(self):
|
|
|
|
pass
|
|
|
|
|
2012-01-26 20:27:21 -05:00
|
|
|
def fail(self):
|
|
|
|
raise Exception("you asked me to do this")
|
|
|
|
|
|
|
|
def test(self, debug = False):
|
2013-02-21 15:19:35 -05:00
|
|
|
self.tester(debug)
|
|
|
|
|
2013-02-23 14:26:18 -05:00
|
|
|
def t(self):
|
|
|
|
pass
|
|
|
|
|
2013-02-21 15:19:35 -05:00
|
|
|
def tester(self, debug = False):
|
2012-01-26 20:27:21 -05:00
|
|
|
# purposely not thread-safe
|
2013-02-21 15:19:35 -05:00
|
|
|
self.test_thread = threading.current_thread().name
|
2012-01-26 20:27:21 -05:00
|
|
|
oldval = self.val
|
|
|
|
newval = oldval + 1
|
|
|
|
time.sleep(0.05)
|
|
|
|
self.val = newval
|
|
|
|
if debug:
|
|
|
|
printf("[%s] value changed: %d -> %d\n",
|
|
|
|
threading.current_thread().name, oldval, newval)
|
|
|
|
|
|
|
|
class Base(object):
|
|
|
|
|
2013-02-23 14:26:18 -05:00
|
|
|
def test_wrapping(self):
|
2012-01-26 20:27:21 -05:00
|
|
|
self.foo.test()
|
|
|
|
with assert_raises(Exception):
|
|
|
|
self.foo.fail()
|
|
|
|
|
2013-02-23 14:26:18 -05:00
|
|
|
def test_threaded(self):
|
2012-01-26 20:27:21 -05:00
|
|
|
def func(foo):
|
|
|
|
foo.test()
|
|
|
|
threads = []
|
|
|
|
for i in xrange(20):
|
|
|
|
threads.append(threading.Thread(target = func, args = (self.foo,)))
|
|
|
|
for t in threads:
|
|
|
|
t.start()
|
|
|
|
for t in threads:
|
|
|
|
t.join()
|
|
|
|
self.verify_result()
|
|
|
|
|
2013-02-23 14:26:18 -05:00
|
|
|
def verify_result(self):
|
|
|
|
eq_(self.foo.val, 20)
|
|
|
|
eq_(self.foo.init_thread, self.foo.test_thread)
|
|
|
|
|
2013-07-24 14:52:26 -04:00
|
|
|
class ListLike(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.thread = threading.current_thread().name
|
|
|
|
self.foo = 0
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
eq_(threading.current_thread().name, self.thread)
|
|
|
|
self.foo = 0
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
eq_(threading.current_thread().name, self.thread)
|
|
|
|
return key
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
eq_(threading.current_thread().name, self.thread)
|
|
|
|
if self.foo < 5:
|
|
|
|
self.foo += 1
|
|
|
|
return self.foo
|
|
|
|
else:
|
|
|
|
raise StopIteration
|
|
|
|
|
2012-01-26 20:27:21 -05:00
|
|
|
class TestUnserialized(Base):
|
|
|
|
def setUp(self):
|
|
|
|
self.foo = Foo()
|
|
|
|
|
|
|
|
def verify_result(self):
|
|
|
|
# This should have failed to increment properly
|
2013-02-21 15:19:35 -05:00
|
|
|
ne_(self.foo.val, 20)
|
|
|
|
# Init and tests ran in different threads
|
|
|
|
ne_(self.foo.init_thread, self.foo.test_thread)
|
2012-01-26 20:27:21 -05:00
|
|
|
|
2013-02-22 18:26:17 -05:00
|
|
|
class TestSerializer(Base):
|
|
|
|
def setUp(self):
|
2013-02-23 14:26:18 -05:00
|
|
|
self.foo = nilmdb.utils.serializer_proxy(Foo)("qwer")
|
|
|
|
|
|
|
|
def test_multi(self):
|
|
|
|
sp = nilmdb.utils.serializer_proxy
|
|
|
|
sp(Foo("x")).t()
|
|
|
|
sp(sp(Foo)("x")).t()
|
|
|
|
sp(sp(Foo))("x").t()
|
|
|
|
sp(sp(Foo("x"))).t()
|
|
|
|
sp(sp(Foo)("x")).t()
|
|
|
|
sp(sp(Foo))("x").t()
|
2013-07-24 14:52:26 -04:00
|
|
|
|
|
|
|
def test_iter(self):
|
|
|
|
sp = nilmdb.utils.serializer_proxy
|
|
|
|
i = sp(ListLike)()
|
|
|
|
eq_(list(i), [1,2,3,4,5])
|
|
|
|
eq_(i[3], 3)
|