def test_multiple_wraps(self): prox1 = tpool.Proxy(re) prox2 = tpool.Proxy(re) x1 = prox1.compile('.') x2 = prox1.compile('.') del x2 x3 = prox2.compile('.')
def test_autowrap_names(self): x = tpool.Proxy({'a': 1, 'b': 2}, autowrap_names=('get', )) self.assert_(isinstance(x.get('a'), tpool.Proxy)) self.assert_(not isinstance(x.items(), tpool.Proxy)) from tests import test_tpool x = tpool.Proxy(test_tpool, autowrap_names=('one', )) self.assert_(isinstance(x.one, tpool.Proxy)) self.assert_(not isinstance(x.two, tpool.Proxy))
def test_wrap_hash(self): prox1 = tpool.Proxy('' + 'A') prox2 = tpool.Proxy('A' + '') self.assert_(prox1 == 'A') self.assert_('A' == prox2) #self.assert_(prox1==prox2) FIXME - could __eq__ unwrap rhs if it is other proxy? self.assertEqual(hash(prox1), hash(prox2)) proxList = tpool.Proxy([]) self.assertRaises(TypeError, hash, proxList)
def test_autowrap(self): x = tpool.Proxy({'a': 1, 'b': 2}, autowrap=(int, )) self.assert_(isinstance(x.get('a'), tpool.Proxy)) self.assert_(not isinstance(x.items(), tpool.Proxy)) # attributes as well as callables from tests import test_tpool x = tpool.Proxy(test_tpool, autowrap=(int, )) self.assert_(isinstance(x.one, tpool.Proxy)) self.assert_(not isinstance(x.none, tpool.Proxy))
def test_callable(self): def wrapped(arg): return arg x = tpool.Proxy(wrapped) self.assertEquals(4, x(4)) # verify that it wraps return values if specified x = tpool.Proxy(wrapped, autowrap_names=('__call__', )) self.assert_(isinstance(x(4), tpool.Proxy)) self.assertEquals("4", str(x(4)))
def test_raising_exceptions(self): prox = tpool.Proxy(re) def nofunc(): prox.never_name_a_function_like_this() self.assertRaises(AttributeError, nofunc) from tests import test_tpool prox = tpool.Proxy(test_tpool) self.assertRaises(RuntimeError, prox.raise_exception)
def test_wrap_iterator(self): self.reset_timeout(2) prox = tpool.Proxy(xrange(10)) result = [] for i in prox: result.append(i) self.assertEquals(range(10), result)
def test_wrap_eq(self): prox = tpool.Proxy(re) exp1 = prox.compile('.') exp2 = prox.compile(exp1.pattern) self.assertEqual(exp1, exp2) exp3 = prox.compile('/') self.assert_(exp1 != exp3)
def test_wrap_iterator2(self): self.reset_timeout(5) # might take a while due to imprecise sleeping def foo(): import time for x in xrange(2): yield x time.sleep(0.001) counter = [0] def tick(): for i in xrange(20000): counter[0] += 1 if counter[0] % 20 == 0: sleep(0.0001) else: sleep() gt = spawn(tick) previtem = 0 for item in tpool.Proxy(foo()): self.assert_(item >= previtem) # make sure the tick happened at least a few times so that we know # that our iterations in foo() were actually tpooled self.assert_(counter[0] > 10, counter[0]) gt.kill()
def test_variable_and_keyword_arguments_with_function_calls(self): import optparse parser = tpool.Proxy(optparse.OptionParser()) z = parser.add_option('-n', action='store', type='string', dest='n') opts, args = parser.parse_args(["-nfoo"]) self.assertEqual(opts.n, 'foo')
def test_wrap_dict(self): my_object = {'a': 1} prox = tpool.Proxy(my_object) self.assertEqual('a', prox.keys()[0]) self.assertEqual(1, prox['a']) self.assertEqual(str(my_object), str(prox)) self.assertEqual(repr(my_object), repr(prox))
def test_autowrap_both(self): from tests import test_tpool x = tpool.Proxy(test_tpool, autowrap=(int, ), autowrap_names=('one', )) self.assert_(isinstance(x.one, tpool.Proxy)) # violating the abstraction to check that we didn't double-wrap self.assert_(not isinstance(x._obj, tpool.Proxy))
def connect (cls, db_module, connect_timeout, *args, **kw): t = timeout.Timeout(connect_timeout, ConnectTimeout()) try: from evy import tpool conn = tpool.execute(db_module.connect, *args, **kw) return tpool.Proxy(conn, autowrap_names = ('cursor',)) finally: t.cancel()
def test_callable_iterator(self): def wrapped(arg): yield arg yield arg yield arg x = tpool.Proxy(wrapped, autowrap_names=('__call__', )) for r in x(3): self.assertEquals(3, r)
def sender_loop(loopnum): obj = tpool.Proxy(Dummy()) count = 100 for n in xrange(count): sleep(random.random() / 200.0) now = time.time() token = loopnum * count + n rv = obj.foo(now, token=token) self.assertEquals(token, rv) sleep(random.random() / 200.0)
def test_wrap_uniterable(self): prox = tpool.Proxy([]) def index(): prox[0] def key(): prox['a'] self.assertRaises(IndexError, index) self.assertRaises(TypeError, key)
def test_contention(self): from tests import test_tpool prox = tpool.Proxy(test_tpool) pile = GreenPile(4) pile.spawn(lambda: self.assertEquals(prox.one, 1)) pile.spawn(lambda: self.assertEquals(prox.two, 2)) pile.spawn(lambda: self.assertEquals(prox.three, 3)) results = list(pile) self.assertEquals(len(results), 3)
def Connection(*args, **kw): conn = tpool.execute(__orig_connections.Connection, *args, **kw) return tpool.Proxy(conn, autowrap_names=('cursor', ))
def test_wrap_string(self): my_object = "whatever" prox = tpool.Proxy(my_object) self.assertEqual(str(my_object), str(prox)) self.assertEqual(len(my_object), len(prox)) self.assertEqual(my_object.join(['a', 'b']), prox.join(['a', 'b']))
def test_wrap_tuple(self): my_tuple = (1, 2) prox = tpool.Proxy(my_tuple) self.assertEqual(prox[0], 1) self.assertEqual(prox[1], 2) self.assertEqual(len(my_tuple), 2)
def test_wrap_ints(self): p = tpool.Proxy(4) self.assert_(p == 4)
def test_wrap_nonzero(self): prox = tpool.Proxy(re) exp1 = prox.compile('.') self.assert_(bool(exp1)) prox2 = tpool.Proxy([1, 2, 3]) self.assert_(bool(prox2))
def test_wrap_getitem(self): prox = tpool.Proxy([0, 1, 2]) self.assertEqual(prox[0], 0)
def test_wrap_module_class(self): prox = tpool.Proxy(re) self.assertEqual(tpool.Proxy, type(prox)) exp = prox.compile('(.)(.)(.)') self.assertEqual(exp.groups, 3) self.assert_(repr(prox.compile))
def test_wrap_setitem(self): prox = tpool.Proxy([0, 1, 2]) prox[1] = 2 self.assertEqual(prox[1], 2)