[pypy-commit] cffi static-callback-embedding: One more test, passing

arigo pypy.commits at gmail.com
Fri Jan 1 09:05:37 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: static-callback-embedding
Changeset: r2512:c742099e76a4
Date: 2016-01-01 15:05 +0100
http://bitbucket.org/cffi/cffi/changeset/c742099e76a4/

Log:	One more test, passing

diff --git a/testing/embedding/add3.py b/testing/embedding/add3.py
new file mode 100644
--- /dev/null
+++ b/testing/embedding/add3.py
@@ -0,0 +1,21 @@
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.cdef("""
+    extern "Python" int add3(int, int, int, int);
+""", dllexport=True)
+
+ffi.embedding_init_code(r"""
+    import sys
+
+    @ffi.def_extern()
+    def add3(x, y, z, t):
+        sys.stdout.write("adding %d, %d, %d, %d\n" % (x, y, z, t))
+        return x + y + z + t
+""")
+
+ffi.set_source("_add3_cffi", """
+""")
+
+ffi.compile()
diff --git a/testing/embedding/test_thread.py b/testing/embedding/test_thread.py
--- a/testing/embedding/test_thread.py
+++ b/testing/embedding/test_thread.py
@@ -46,3 +46,16 @@
                           "prepADD2\n"
                           "adding 1000 and 200 and 30\n"
                           "done\n")
+
+    def test_load_in_parallel_more(self):
+        self.prepare_module('add2')
+        self.prepare_module('add3')
+        self.compile('thread3-test', ['_add2_cffi', '_add3_cffi'], ['-pthread'])
+        for i in range(150):
+            output = self.execute('thread3-test')
+            for j in range(10):
+                output = self._take_out(output, "adding 40 and 2 and 100\n")
+                output = self._take_out(output, "adding 1000, 200, 30, 4\n")
+            assert output == ("starting\n"
+                              "prepADD2\n"
+                              "done\n")
diff --git a/testing/embedding/thread3-test.c b/testing/embedding/thread3-test.c
new file mode 100644
--- /dev/null
+++ b/testing/embedding/thread3-test.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <assert.h>
+
+extern int add2(int, int, int);
+extern int add3(int, int, int, int);
+
+static sem_t done;
+
+
+static void *start_routine_2(void *arg)
+{
+    int x, status;
+    x = add2(40, 2, 100);
+    assert(x == 142);
+
+    status = sem_post(&done);
+    assert(status == 0);
+
+    return arg;
+}
+
+static void *start_routine_3(void *arg)
+{
+    int x, status;
+    x = add3(1000, 200, 30, 4);
+    assert(x == 1234);
+
+    status = sem_post(&done);
+    assert(status == 0);
+
+    return arg;
+}
+
+int main(void)
+{
+    pthread_t th;
+    int i, status = sem_init(&done, 0, 0);
+    assert(status == 0);
+
+    printf("starting\n");
+    for (i = 0; i < 10; i++) {
+        status = pthread_create(&th, NULL, start_routine_2, NULL);
+        assert(status == 0);
+        status = pthread_create(&th, NULL, start_routine_3, NULL);
+        assert(status == 0);
+    }
+    for (i = 0; i < 20; i++) {
+        status = sem_wait(&done);
+        assert(status == 0);
+    }
+    printf("done\n");
+    return 0;
+}


More information about the pypy-commit mailing list