예제 #1
0
async def test_async_to_async():
    s = Synchronizer()
    f_s = s(f)
    with pytest.raises(CustomException) as excinfo:
        await f_s()
    check_traceback(excinfo.value)
예제 #2
0
import pickle
import pytest

from synchronicity import Synchronizer


s = Synchronizer()


@s
class PicklableClass:
    async def f(self, x):
        return x**2


@pytest.mark.skip(reason="Let's revisit this in 0.2.0")
def test_pickle():
    obj = PicklableClass()
    assert obj.f(42) == 1764
    data = pickle.dumps(obj)
    obj2 = pickle.loads(data)
    assert obj2.f(43) == 1849


def test_pickle_synchronizer():
    pickle.dumps(s)
예제 #3
0
async def test_athrow_baseexc_async():
    gen = Synchronizer()(athrow_example_gen)()
    v = await gen.asend(None)
    assert v == "hello"
    v = await gen.athrow(KeyboardInterrupt)
    assert v == "foobar"
예제 #4
0
def test_sync_to_async():
    s = Synchronizer()
    f_baseexc_s = s(f_baseexc)
    with pytest.raises(BaseException) as excinfo:
        f_s()
    check_traceback(excinfo.value)
예제 #5
0
def test_athrow_sync():
    gen = Synchronizer()(athrow_example_gen)()
    v = gen.send(None)
    assert v == "hello"
    v = gen.throw(ZeroDivisionError)
    assert v == "world"
예제 #6
0
def test_generator_order_sync():
    events.clear()
    async_producer_synchronized = Synchronizer()(async_producer)
    for i in async_producer_synchronized():
        events.append("consumer")
    assert events == ["producer", "consumer"] * 10
예제 #7
0
async def test_generator_order_explicit_async():
    events.clear()
    async_producer_synchronized = Synchronizer().create(async_producer)[Interface.ASYNC]
    async for i in async_producer_synchronized():
        events.append("consumer")
    assert events == ["producer", "consumer"] * 10
예제 #8
0
def test_translate():
    s = Synchronizer()

    class Foo:
        pass

    class FooProvider:
        def __init__(self, foo=None):
            if foo is not None:
                assert type(foo) == Foo
                self.foo = foo
            else:
                self.foo = Foo()

        def get(self):
            return self.foo

        def get2(self):
            return [self.foo, self.foo]

        @property
        def pget(self):
            return self.foo

        def set(self, foo):
            assert type(foo) == Foo
            self.foo = foo

        @classmethod
        def cls_in(cls):
            assert cls == FooProvider

        @classmethod
        def cls_out(cls):
            return FooProvider

    BlockingFoo = s.create(Foo)[Interface.BLOCKING]
    assert BlockingFoo.__name__ == "BlockingFoo"
    BlockingFooProvider = s.create(FooProvider)[Interface.BLOCKING]
    assert BlockingFooProvider.__name__ == "BlockingFooProvider"
    foo_provider = BlockingFooProvider()

    # Make sure two instances translated out are the same
    foo1 = foo_provider.get()
    foo2 = foo_provider.get()
    assert foo1 == foo2

    # Make sure we can return a list
    foos = foo_provider.get2()
    assert foos == [foo1, foo2]

    # Make sure properties work
    foo = foo_provider.pget
    assert isinstance(foo, BlockingFoo)

    # Translate an object in and then back out, make sure it's the same
    foo = BlockingFoo()
    assert type(foo) != Foo
    foo_provider.set(foo)
    assert foo_provider.get() == foo

    # Make sure classes are translated properly too
    BlockingFooProvider.cls_in()
    assert BlockingFooProvider.cls_out() == BlockingFooProvider

    # Make sure the constructor works
    foo = BlockingFoo()
    foo_provider = BlockingFooProvider(foo)
    assert foo_provider.get() == foo
예제 #9
0
def test_check_double_wrapped(recwarn):
    s = Synchronizer()
    assert len(recwarn) == 0
    ret = s(returns_asyncgen)()
    assert inspect.isasyncgen(ret)
    assert len(recwarn) == 1