def test_exceptions(): fr = FooReceiver.create() exception_arg = "cpp_impl_error!" # pytest.raises(RuntimeError(exception_arg), fr.cause_cpp_exception, exception_arg) # tried to simplify like this, did not work yet # CAUSE CUSTOM EXCEPTION in CPP IMPLEMENTATION try: fr.cause_cpp_exception(exception_arg) except DjinniException as e: # Note: fully checking exception equality requires type check as well; not sure if needed for our purposes assert e.args == DjinniException(exception_arg).args fl = FooListenerImpl_FullTest() fr.add_listener(fl) # CAUSE CUSTOM EXCEPTION in PY IMPLEMENTATION exception_arg = "py_impl_error!" try: fr.cause_py_exception(exception_arg) except Exception as e: # Note: fully checking exception equality requires type check as well; not sure if needed for our purposes assert e.args == Exception(exception_arg).args # CAUSE REGULAR PY EXCEPTION IN PY pytest.raises(ZeroDivisionError, fr.cause_zero_division_error) from djinni.exception import ExceptionHelper assert len(ExceptionHelper.c_data_set) == 0
def test_interface_back_forth(): print("start test len ", len(FooListenerBfHelper.c_data_set)) fr = FooReceiver.create() fl = FooListenerBfImpl() # python implementation of listener fl_cpp = fr.get_foo_listener_bf() # cpp implementation of listener # both direct and indirect test for python impl of FooListenerBf fr_set_get(fr, fl, "Hello world!") # both direct and indirect test for cpp impl of FooListenerBf fr_set_get(fr, fl_cpp, "Goodbye world!") fr_set_get(fr, fl_cpp, "Goodbye world!") # send python implementation back and forth and see that it can still be used, and that no wrapper was added fl_1 = fr.send_return(fl) fl_2 = fr.send_return(fl_1) fr_set_get(fr, fl_2, "Hello") assert fl == fl_1 and fl_1 == fl_2, "test_interface_back_forth failed" # send cpp implementation back and forth and see that is can still be used, and handles hold same implementation fl_cpp_1 = fr.send_return(fl_cpp) fl_cpp_2 = fr.send_return(fl_cpp_1) fr_set_get(fr, fl_cpp_2, "Goodbye") assert lib.equal_handles_cw__foo_listener_bf(fl_cpp._cpp_impl, fl_cpp_1._cpp_impl) and \ lib.equal_handles_cw__foo_listener_bf(fl_cpp_1._cpp_impl, fl_cpp_2._cpp_impl) fl = fl_1 = fl_2 = fl_cpp = fl_cpp_1 = fl_cpp_2 = None gc.collect() fr = None gc.collect() assert 0 == len(FooListenerBfHelper.c_data_set)
def test_interface_back_forth(): print ("start test len ", len(FooListenerBfHelper.c_data_set)) fr = FooReceiver.create() fl = FooListenerBfImpl() # python implementation of listener fl_cpp = fr.get_foo_listener_bf() # cpp implementation of listener # both direct and indirect test for python impl of FooListenerBf fr_set_get(fr, fl, "Hello world!") # both direct and indirect test for cpp impl of FooListenerBf fr_set_get(fr, fl_cpp, "Goodbye world!") fr_set_get(fr, fl_cpp, "Goodbye world!") # send python implementation back and forth and see that it can still be used, and that no wrapper was added fl_1 = fr.send_return(fl) fl_2 = fr.send_return(fl_1) fr_set_get(fr, fl_2, "Hello") assert fl == fl_1 and fl_1 == fl_2, "test_interface_back_forth failed" # send cpp implementation back and forth and see that is can still be used, and handles hold same implementation fl_cpp_1 = fr.send_return(fl_cpp) fl_cpp_2 = fr.send_return(fl_cpp_1) fr_set_get(fr, fl_cpp_2, "Goodbye") assert lib.equal_handles_cw__foo_listener_bf(fl_cpp._cpp_impl, fl_cpp_1._cpp_impl) and \ lib.equal_handles_cw__foo_listener_bf(fl_cpp_1._cpp_impl, fl_cpp_2._cpp_impl) fl = fl_1 = fl_2 = fl_cpp = fl_cpp_1 = fl_cpp_2 = None gc.collect() fr = None gc.collect() assert 0 == len(FooListenerBfHelper.c_data_set)
def test_interface_callback_back_forth(): fr = FooReceiver.create() fl = FooListenerBfImpl() fr.add_listener_bf(fl) fl_in_fl = FooListenerBfImpl() b = b'Some Binary 11' fr_fl_set_get(fr, fl_in_fl, b) # listener 1 in python, listener 2 in python fl_in_fl_1 = fr.in_listener_bf_send_return(fl_in_fl) fl_in_fl_2 = fr.in_listener_bf_send_return(fl_in_fl_1) assert fl_in_fl == fl_in_fl_1 and fl_in_fl_1 == fl_in_fl_2, "test_interface_back_forth failed" fr_fl_set_get( fr, fl_in_fl_2, b) # listener 1 in python, listener 2 in python after back&forth fl_in_fl = fr.get_foo_listener_bf() b = b'Some Other Binary 12' fr_fl_set_get(fr, fl_in_fl, b) # listener 1 in python, listener 2 in cpp fl_in_fl_1 = fr.in_listener_bf_send_return(fl_in_fl) fl_in_fl_2 = fr.in_listener_bf_send_return(fl_in_fl_1) assert lib.equal_handles_cw__foo_listener_bf(fl_in_fl._cpp_impl, fl_in_fl_1._cpp_impl) and \ lib.equal_handles_cw__foo_listener_bf(fl_in_fl_1._cpp_impl, fl_in_fl_2._cpp_impl) fr_fl_set_get( fr, fl_in_fl_2, b) # listener 1 in python, listener 2 in cpp after back&forth fl = fr.get_foo_listener_bf() fr.add_listener_bf(fl) fl_in_fl = FooListenerBfImpl() b = b'Some Binary 21' fr_fl_set_get(fr, fl_in_fl, b) # listener 1 in cpp, listener 2 in python fl_in_fl_1 = fr.in_listener_bf_send_return(fl_in_fl) fl_in_fl_2 = fr.in_listener_bf_send_return(fl_in_fl_1) assert fl_in_fl == fl_in_fl_1 and fl_in_fl_1 == fl_in_fl_2, "test_interface_back_forth failed" fr_fl_set_get( fr, fl_in_fl_2, b) # listener 1 in cpp, listener 2 in python after back&forth fl_in_fl = fr.get_foo_listener_bf() b = b'Some Other Binary 22' fr_fl_set_get(fr, fl_in_fl, b) # listener 1 in cpp, listener 2 in cpp fl_in_fl_1 = fr.in_listener_bf_send_return(fl_in_fl) fl_in_fl_2 = fr.in_listener_bf_send_return(fl_in_fl_1) assert lib.equal_handles_cw__foo_listener_bf(fl_in_fl._cpp_impl, fl_in_fl_1._cpp_impl) and \ lib.equal_handles_cw__foo_listener_bf(fl_in_fl_1._cpp_impl, fl_in_fl_2._cpp_impl) fr_fl_set_get(fr, fl_in_fl_2, b) # listener 1 in cpp, listener 2 in cpp after back&forth fl = fl_in_fl = fl_in_fl_1 = fl_in_fl_2 = None gc.collect() fr = None gc.collect() assert 0 == len(FooListenerBfHelper.c_data_set)
def test_simple_callback_indirect(): fr = FooReceiver.create() fl = FooListenerImpl() fr.add_listener(fl) # indirect check via returned value from receiver s = "Hello world!" assert fr.set_private_string(s) == s, "test_simple_callback_indirect failed" fl = None fr = None gc.collect()
def test_simple_callback_direct(): fr = FooReceiver.create() fl = FooListenerImpl_SimpleTest() fr.add_listener(fl) s = "" assert fr.set_private_string(s) == s # direct check via peek inside listener assert fl._local_true == True and fl._local_false == False, "test_simple_callback_direct failed" fl = None fr = None gc.collect()
def test_interface_callback_back_forth(): fr = FooReceiver.create() fl = FooListenerBfImpl() fr.add_listener_bf(fl) fl_in_fl = FooListenerBfImpl() b = b'Some Binary 11' fr_fl_set_get(fr, fl_in_fl, b) # listener 1 in python, listener 2 in python fl_in_fl_1 = fr.in_listener_bf_send_return(fl_in_fl) fl_in_fl_2 = fr.in_listener_bf_send_return(fl_in_fl_1) assert fl_in_fl == fl_in_fl_1 and fl_in_fl_1 == fl_in_fl_2, "test_interface_back_forth failed" fr_fl_set_get(fr, fl_in_fl_2, b) # listener 1 in python, listener 2 in python after back&forth fl_in_fl = fr.get_foo_listener_bf() b = b'Some Other Binary 12' fr_fl_set_get(fr, fl_in_fl, b) # listener 1 in python, listener 2 in cpp fl_in_fl_1 = fr.in_listener_bf_send_return(fl_in_fl) fl_in_fl_2 = fr.in_listener_bf_send_return(fl_in_fl_1) assert lib.equal_handles_cw__foo_listener_bf(fl_in_fl._cpp_impl, fl_in_fl_1._cpp_impl) and \ lib.equal_handles_cw__foo_listener_bf(fl_in_fl_1._cpp_impl, fl_in_fl_2._cpp_impl) fr_fl_set_get(fr, fl_in_fl_2, b) # listener 1 in python, listener 2 in cpp after back&forth fl = fr.get_foo_listener_bf() fr.add_listener_bf(fl) fl_in_fl = FooListenerBfImpl() b = b'Some Binary 21' fr_fl_set_get(fr, fl_in_fl, b) # listener 1 in cpp, listener 2 in python fl_in_fl_1 = fr.in_listener_bf_send_return(fl_in_fl) fl_in_fl_2 = fr.in_listener_bf_send_return(fl_in_fl_1) assert fl_in_fl == fl_in_fl_1 and fl_in_fl_1 == fl_in_fl_2, "test_interface_back_forth failed" fr_fl_set_get(fr, fl_in_fl_2, b) # listener 1 in cpp, listener 2 in python after back&forth fl_in_fl = fr.get_foo_listener_bf() b = b'Some Other Binary 22' fr_fl_set_get(fr, fl_in_fl, b) # listener 1 in cpp, listener 2 in cpp fl_in_fl_1 = fr.in_listener_bf_send_return(fl_in_fl) fl_in_fl_2 = fr.in_listener_bf_send_return(fl_in_fl_1) assert lib.equal_handles_cw__foo_listener_bf(fl_in_fl._cpp_impl, fl_in_fl_1._cpp_impl) and \ lib.equal_handles_cw__foo_listener_bf(fl_in_fl_1._cpp_impl, fl_in_fl_2._cpp_impl) fr_fl_set_get(fr, fl_in_fl_2, b) # listener 1 in cpp, listener 2 in cpp after back&forth fl = fl_in_fl = fl_in_fl_1 = fl_in_fl_2 = None gc.collect() fr = None gc.collect() assert 0 == len(FooListenerBfHelper.c_data_set)
def test_full_callback(): fr = FooReceiver.create() fl = FooListenerImpl_FullTest() fl_opt = FooListenerImpl_FullTest() fr.add_listener(fl) fr_fl(fr,fl) fr.add_optional_listener(fl_opt) fr_fl(fr,fl_opt) fr.add_optional_listener(None) assert fr.get_optional_listener() is None, "test_full_callback failed" fl = None gc.collect() fr = None gc.collect() assert 0 == len(FooListenerHelper.c_data_set), "test_full_callback failed"