[pypy-commit] pypy stm: Add an emulator for "transaction", running serially and

arigo noreply at buildbot.pypy.org
Tue Jan 31 15:59:47 CET 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: stm
Changeset: r51989:d30f3b2ae5fc
Date: 2012-01-31 15:57 +0100
http://bitbucket.org/pypy/pypy/changeset/d30f3b2ae5fc/

Log:	Add an emulator for "transaction", running serially and dispatching
	events in random order.

diff --git a/lib_pypy/transaction.py b/lib_pypy/transaction.py
new file mode 100644
--- /dev/null
+++ b/lib_pypy/transaction.py
@@ -0,0 +1,35 @@
+import sys
+import random
+
+
+print >> sys.stderr, "warning: using lib_pypy/transaction.py, the emulator"
+
+_pending = {}
+
+def set_num_threads(num):
+    pass
+
+def add(f, *args):
+    r = random.random()
+    assert r not in _pending    # very bad luck if it is
+    _pending[r] = (f, args)
+
+def add_epoll(ep, callback):
+    def poll_reader():
+        # assume only one epoll is added.  If the _pending list is
+        # now empty, wait.  If not, then just poll non-blockingly.
+        if len(_pending) == 0:
+            timeout = -1
+        else:
+            timeout = 0
+        got = ep.poll(timeout=timeout)
+        for fd, events in got:
+            add(callback, fd, events)
+        add(poll_reader)
+    add(poll_reader)
+
+def run():
+    pending = _pending
+    while pending:
+        _, (f, args) = pending.popitem()
+        f(*args)
diff --git a/pypy/module/transaction/test/test_epoll.py b/pypy/module/transaction/test/test_epoll.py
--- a/pypy/module/transaction/test/test_epoll.py
+++ b/pypy/module/transaction/test/test_epoll.py
@@ -49,3 +49,9 @@
         assert steps == []
         raises(Done, transaction.run)
         assert steps == ['write_stuff', 'callback']
+
+
+class AppTestEpollEmulator(AppTestEpoll):
+    def setup_class(cls):
+        # test for lib_pypy/transaction.py
+        cls.space = gettestobjspace(usemodules=['select'])
diff --git a/pypy/module/transaction/test/test_transaction.py b/pypy/module/transaction/test/test_transaction.py
--- a/pypy/module/transaction/test/test_transaction.py
+++ b/pypy/module/transaction/test/test_transaction.py
@@ -59,3 +59,9 @@
             pass
         assert len(lst) == 1
         assert lst[0] == e.args[0]
+
+
+class AppTestTransactionEmulator(AppTestTransaction):
+    def setup_class(cls):
+        # test for lib_pypy/transaction.py
+        cls.space = gettestobjspace(usemodules=[])


More information about the pypy-commit mailing list