Ejemplo n.º 1
0
 def test_multiple_wraps(self):
     prox1 = tpool.Proxy(re)
     prox2 = tpool.Proxy(re)
     x1 = prox1.compile('.')
     x2 = prox1.compile('.')
     del x2
     x3 = prox2.compile('.')
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
    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))
Ejemplo n.º 5
0
    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)))
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
    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()
Ejemplo n.º 10
0
    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')
Ejemplo n.º 11
0
 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))
Ejemplo n.º 12
0
    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))
Ejemplo n.º 13
0
    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()
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
 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)
Ejemplo n.º 16
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)
Ejemplo n.º 17
0
    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)
Ejemplo n.º 18
0
def Connection(*args, **kw):
    conn = tpool.execute(__orig_connections.Connection, *args, **kw)
    return tpool.Proxy(conn, autowrap_names=('cursor', ))
Ejemplo n.º 19
0
 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']))
Ejemplo n.º 20
0
 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)
Ejemplo n.º 21
0
 def test_wrap_ints(self):
     p = tpool.Proxy(4)
     self.assert_(p == 4)
Ejemplo n.º 22
0
 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))
Ejemplo n.º 23
0
 def test_wrap_getitem(self):
     prox = tpool.Proxy([0, 1, 2])
     self.assertEqual(prox[0], 0)
Ejemplo n.º 24
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))
Ejemplo n.º 25
0
 def test_wrap_setitem(self):
     prox = tpool.Proxy([0, 1, 2])
     prox[1] = 2
     self.assertEqual(prox[1], 2)