Example #1
0
def test_function_sync():
    s = Synchronizer()

    async def f(x):
        return x**2

    f_blocking = s.create(f)[Interface.BLOCKING]
    assert f_blocking(42) == 1764
def test_is_synchronized():
    s = Synchronizer()

    class Foo:
        pass

    BlockingFoo = s.create(Foo)[Interface.BLOCKING]
    assert s.is_synchronized(Foo) == False
    assert s.is_synchronized(BlockingFoo) == True
Example #3
0
def test_doc_transfer(interface_type):
    class Foo:
        """Hello"""
        def foo(self):
            """hello"""

    s = Synchronizer()
    output_class = s.create(Foo)[interface_type]

    assert output_class.__doc__ == "Hello"
    assert output_class.foo.__doc__ == "hello"
Example #4
0
def test_inspect_coroutinefunction():
    s = Synchronizer()
    interfaces = s.create(_Api)

    BlockingApi = interfaces[Interface.BLOCKING]
    AioApi = interfaces[Interface.ASYNC]

    assert inspect.iscoroutinefunction(BlockingApi.blocking_func) == False
    assert inspect.iscoroutinefunction(BlockingApi.async_func) == False
    assert inspect.iscoroutinefunction(AioApi.blocking_func) == False
    assert inspect.iscoroutinefunction(AioApi.async_func) == True
def test_recursive():
    s = Synchronizer()

    async def f(n):
        if n == 0:
            raise CustomException("boom!")
        else:
            return await f(n - 1)

    with pytest.raises(CustomException) as excinfo:
        f_blocking = s.create(f)[Interface.BLOCKING]
        f_blocking(10)
    check_traceback(excinfo.value)
Example #6
0
async def test_translate():
    s = Synchronizer()

    class Foo:
        def __init__(self, x):
            self.x = x

        def get(self):
            return self.x

    BlockingFoo = s.create(Foo)[Interface.BLOCKING]

    def f(foo):
        assert isinstance(foo, BlockingFoo)
        x = foo.get()
        return BlockingFoo(x + 1)

    f_cb = s.create_callback(f, Interface.BLOCKING)

    foo1 = Foo(42)
    foo2 = await f_cb(foo1)
    assert foo2.x == 43
Example #7
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