def test_monkeypatch_patch(monkeypatch): monkeypatch.delitem(sys.modules, "threading") monkeypatch.setattr(cthreading, "_patched", False) cthreading.monkeypatch() import thread import threading assert thread.allocate_lock is cthreading.Lock assert threading._allocate_lock is cthreading.Lock assert threading.Lock is cthreading.Lock assert threading.RLock is cthreading.RLock assert threading.Condition is cthreading.Condition
def main(args): options, args = parse_args(args) if options.monkeypatch: if options.monkeypatch == "cthreading": import cthreading cthreading.monkeypatch() elif options.monkeypatch == "pthreading": import pthreading pthreading.monkey_patch() else: raise ValueError("Usupported monkeypatch %r" % options.monkeypatch) try: import Queue as queue _range = xrange except ImportError: import queue _range = range import threading if options.profile: import yappi yappi.set_clock_type('cpu') yappi.start(builtins=True, profile_threads=True) leftmost = queue.Queue() left = leftmost for i in _range(options.whispers): right = queue.Queue() t = threading.Thread(target=whisper, args=(left, right)) t.daemon = True t.start() left = right for i in _range(options.jobs): right.put(1) for i in _range(options.jobs): n = leftmost.get() assert n == options.whispers + 1 if options.profile: yappi.stop() stats = yappi.get_func_stats() stats.save(options.profile, 'pstat')
def run(func, options): if options.monkeypatch: if options.monkeypatch == "cthreading": import cthreading cthreading.monkeypatch() elif options.monkeypatch == "pthreading": import pthreading pthreading.monkey_patch() else: raise ValueError("Usupported monkeypatch %r" % options.monkeypatch) if options.profile: import yappi yappi.set_clock_type('cpu') yappi.start(builtins=True, profile_threads=True) func(options) if options.profile: yappi.stop() stats = yappi.get_func_stats() stats.save(options.profile, 'pstat')
def main(args): options, args = parse_args(args) if options.monkeypatch: if options.monkeypatch == "cthreading": import cthreading cthreading.monkeypatch() elif options.monkeypatch == "pthreading": import pthreading pthreading.monkey_patch() else: raise ValueError("Usupported monkeypatch %r" % options.monkeypatch) try: import Queue as queue _range = xrange except ImportError: import queue _range = range import threading src = queue.Queue() dst = queue.Queue() for i in _range(options.workers): t = threading.Thread(target=worker, args=(src, dst)) t.daemon = True t.start() for i in _range(options.rounds): for j in _range(options.jobs): src.put(1) for j in _range(options.jobs): n = dst.get() assert n == 2
def test_monkeypatch_twice(monkeypatch): monkeypatch.delitem(sys.modules, "threading") monkeypatch.delitem(sys.modules, "thread") monkeypatch.setattr(cthreading, "_patched", False) cthreading.monkeypatch() pytest.raises(RuntimeError, cthreading.monkeypatch)
def test_monkeypatch_twice(monkeypatch): monkeypatch.delitem(sys.modules, "threading") monkeypatch.setattr(cthreading, "_patched", False) cthreading.monkeypatch() pytest.raises(RuntimeError, cthreading.monkeypatch)
# Copyright 2015 Nir Soffer <*****@*****.**> # # This copyrighted material is made available to anyone wishing to use, # modify, copy, or redistribute it subject to the terms and conditions # of the GNU General Public License v2 or (at your option) any later version. import cthreading cthreading.monkeypatch() # Requires python-test package, not installed by default from test import regrtest regrtest.main()