diff options
| author | Armin Rigo <arigo@tunes.org> | 2015-11-23 16:11:58 +0100 |
|---|---|---|
| committer | Armin Rigo <arigo@tunes.org> | 2015-11-23 16:11:58 +0100 |
| commit | 4f76ae1395509642cd37bd663d1df82cc2f66ca8 (patch) | |
| tree | 8fb11e00db333bee69715c2aa138347c201cfb96 /testing | |
| parent | 5f2b11cd693cb67ada74c910236aae247e1184af (diff) | |
| parent | d54ecf308d23a7b9118fae738542c42fe7b20f61 (diff) | |
| download | cffi-4f76ae1395509642cd37bd663d1df82cc2f66ca8.tar.gz | |
hg merge default
Diffstat (limited to 'testing')
| -rw-r--r-- | testing/cffi0/backend_tests.py | 32 | ||||
| -rw-r--r-- | testing/cffi0/test_function.py | 2 | ||||
| -rw-r--r-- | testing/cffi1/test_ffi_obj.py | 39 |
3 files changed, 72 insertions, 1 deletions
diff --git a/testing/cffi0/backend_tests.py b/testing/cffi0/backend_tests.py index 23b925f..2a6af1b 100644 --- a/testing/cffi0/backend_tests.py +++ b/testing/cffi0/backend_tests.py @@ -1809,3 +1809,35 @@ class BackendTests: assert lib.EE1 == 0 assert lib.EE2 == 0 assert lib.EE3 == 1 + + def test_init_once(self): + def do_init(): + seen.append(1) + return 42 + ffi = FFI() + seen = [] + for i in range(3): + res = ffi.init_once(do_init, "tag1") + assert res == 42 + assert seen == [1] + for i in range(3): + res = ffi.init_once(do_init, "tag2") + assert res == 42 + assert seen == [1, 1] + + def test_init_once_multithread(self): + import thread, time + def do_init(): + seen.append('init!') + time.sleep(1) + seen.append('init done') + return 7 + ffi = FFI() + seen = [] + for i in range(6): + def f(): + res = ffi.init_once(do_init, "tag") + seen.append(res) + thread.start_new_thread(f, ()) + time.sleep(1.5) + assert seen == ['init!', 'init done'] + 6 * [7] diff --git a/testing/cffi0/test_function.py b/testing/cffi0/test_function.py index 8fdcc86..6771894 100644 --- a/testing/cffi0/test_function.py +++ b/testing/cffi0/test_function.py @@ -464,7 +464,7 @@ class TestFunction(object): ffi = FFI(backend=self.Backend()) ffi.cdef("double __stdcall sin(double x);") # stdcall ignored m = ffi.dlopen(lib_m) - if (sys.platform == 'win32' and sys.maxint < 2**32 and + if (sys.platform == 'win32' and sys.maxsize < 2**32 and self.Backend is not CTypesBackend): assert "double(__stdcall *)(double)" in str(ffi.typeof(m.sin)) else: diff --git a/testing/cffi1/test_ffi_obj.py b/testing/cffi1/test_ffi_obj.py index 7cb67cc..d7f5992 100644 --- a/testing/cffi1/test_ffi_obj.py +++ b/testing/cffi1/test_ffi_obj.py @@ -193,6 +193,11 @@ def test_handle(): yp = ffi.new_handle([6, 4, 2]) assert ffi.from_handle(yp) == [6, 4, 2] +def test_handle_unique(): + ffi = _cffi1_backend.FFI() + assert ffi.new_handle(None) is not ffi.new_handle(None) + assert ffi.new_handle(None) != ffi.new_handle(None) + def test_ffi_cast(): ffi = _cffi1_backend.FFI() assert ffi.cast("int(*)(int)", 0) == ffi.NULL @@ -415,3 +420,37 @@ def test_cast_from_int_type_to_bool(): assert int(ffi.cast("_Bool", ffi.cast(type, 42))) == 1 assert int(ffi.cast("bool", ffi.cast(type, 42))) == 1 assert int(ffi.cast("_Bool", ffi.cast(type, 0))) == 0 + +def test_init_once(): + def do_init(): + seen.append(1) + return 42 + ffi = _cffi1_backend.FFI() + seen = [] + for i in range(3): + res = ffi.init_once(do_init, "tag1") + assert res == 42 + assert seen == [1] + for i in range(3): + res = ffi.init_once(do_init, "tag2") + assert res == 42 + assert seen == [1, 1] + +def test_init_once_multithread(): + import thread, time + def do_init(): + print 'init!' + seen.append('init!') + time.sleep(1) + seen.append('init done') + print 'init done' + return 7 + ffi = _cffi1_backend.FFI() + seen = [] + for i in range(6): + def f(): + res = ffi.init_once(do_init, "tag") + seen.append(res) + thread.start_new_thread(f, ()) + time.sleep(1.5) + assert seen == ['init!', 'init done'] + 6 * [7] |
