[svn r37264] create the new development trunk
--HG-- branch : trunk
This commit is contained in:
1
py/thread/testing/__init__.py
Normal file
1
py/thread/testing/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
#
|
||||
66
py/thread/testing/test_io.py
Normal file
66
py/thread/testing/test_io.py
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
import py
|
||||
import sys
|
||||
|
||||
WorkerPool = py._thread.WorkerPool
|
||||
ThreadOut = py._thread.ThreadOut
|
||||
|
||||
def test_threadout_install_deinstall():
|
||||
old = sys.stdout
|
||||
out = ThreadOut(sys, 'stdout')
|
||||
out.deinstall()
|
||||
assert old == sys.stdout
|
||||
|
||||
class TestThreadOut:
|
||||
def setup_method(self, method):
|
||||
self.out = ThreadOut(sys, 'stdout')
|
||||
def teardown_method(self, method):
|
||||
self.out.deinstall()
|
||||
|
||||
def test_threadout_one(self):
|
||||
l = []
|
||||
self.out.setwritefunc(l.append)
|
||||
print 42,13,
|
||||
x = l.pop(0)
|
||||
assert x == '42'
|
||||
x = l.pop(0)
|
||||
assert x == ' '
|
||||
x = l.pop(0)
|
||||
assert x == '13'
|
||||
|
||||
|
||||
def test_threadout_multi_and_default(self):
|
||||
num = 3
|
||||
defaults = []
|
||||
def f(l):
|
||||
self.out.setwritefunc(l.append)
|
||||
print id(l),
|
||||
self.out.delwritefunc()
|
||||
print 1
|
||||
self.out.setdefaultwriter(defaults.append)
|
||||
pool = WorkerPool()
|
||||
listlist = []
|
||||
for x in range(num):
|
||||
l = []
|
||||
listlist.append(l)
|
||||
pool.dispatch(f, l)
|
||||
pool.shutdown()
|
||||
for name, value in self.out.__dict__.items():
|
||||
print >>sys.stderr, "%s: %s" %(name, value)
|
||||
pool.join(2.0)
|
||||
for i in range(num):
|
||||
item = listlist[i]
|
||||
assert item ==[str(id(item))]
|
||||
assert not self.out._tid2out
|
||||
assert defaults
|
||||
expect = ['1' for x in range(num)]
|
||||
defaults = [x for x in defaults if x.strip()]
|
||||
assert defaults == expect
|
||||
|
||||
def test_threadout_nested(self):
|
||||
# we want ThreadOuts to coexist
|
||||
last = sys.stdout
|
||||
out = ThreadOut(sys, 'stdout')
|
||||
assert last == sys.stdout
|
||||
out.deinstall()
|
||||
assert last == sys.stdout
|
||||
93
py/thread/testing/test_pool.py
Normal file
93
py/thread/testing/test_pool.py
Normal file
@@ -0,0 +1,93 @@
|
||||
|
||||
import py
|
||||
import sys
|
||||
|
||||
WorkerPool = py._thread.WorkerPool
|
||||
ThreadOut = py._thread.ThreadOut
|
||||
|
||||
def test_some():
|
||||
pool = WorkerPool()
|
||||
q = py.std.Queue.Queue()
|
||||
num = 4
|
||||
|
||||
def f(i):
|
||||
q.put(i)
|
||||
while q.qsize():
|
||||
py.std.time.sleep(0.01)
|
||||
for i in range(num):
|
||||
pool.dispatch(f, i)
|
||||
for i in range(num):
|
||||
q.get()
|
||||
assert len(pool._alive) == 4
|
||||
pool.shutdown()
|
||||
# XXX I replaced the following join() with a time.sleep(1), which seems
|
||||
# to fix the test on Windows, and doesn't break it on Linux... Completely
|
||||
# unsure what the idea is, though, so it would be nice if someone with some
|
||||
# more understanding of what happens here would either fix this better, or
|
||||
# remove this comment...
|
||||
# pool.join(timeout=1.0)
|
||||
py.std.time.sleep(1)
|
||||
assert len(pool._alive) == 0
|
||||
assert len(pool._ready) == 0
|
||||
|
||||
def test_get():
|
||||
pool = WorkerPool()
|
||||
def f():
|
||||
return 42
|
||||
reply = pool.dispatch(f)
|
||||
result = reply.get()
|
||||
assert result == 42
|
||||
|
||||
def test_get_timeout():
|
||||
pool = WorkerPool()
|
||||
def f():
|
||||
py.std.time.sleep(0.2)
|
||||
return 42
|
||||
reply = pool.dispatch(f)
|
||||
py.test.raises(IOError, "reply.get(timeout=0.01)")
|
||||
|
||||
def test_get_excinfo():
|
||||
pool = WorkerPool()
|
||||
def f():
|
||||
raise ValueError("42")
|
||||
reply = pool.dispatch(f)
|
||||
excinfo = py.test.raises(ValueError, "reply.get(1.0)")
|
||||
py.test.raises(EOFError, "reply.get(1.0)")
|
||||
|
||||
def test_maxthreads():
|
||||
pool = WorkerPool(maxthreads=1)
|
||||
def f():
|
||||
py.std.time.sleep(0.5)
|
||||
try:
|
||||
pool.dispatch(f)
|
||||
py.test.raises(IOError, pool.dispatch, f)
|
||||
finally:
|
||||
pool.shutdown()
|
||||
|
||||
def test_join_timeout():
|
||||
pool = WorkerPool()
|
||||
q = py.std.Queue.Queue()
|
||||
def f():
|
||||
q.get()
|
||||
reply = pool.dispatch(f)
|
||||
pool.shutdown()
|
||||
py.test.raises(IOError, pool.join, 0.01)
|
||||
q.put(None)
|
||||
reply.get(timeout=1.0)
|
||||
pool.join(timeout=0.1)
|
||||
|
||||
def test_pool_clean_shutdown():
|
||||
capture = py.io.OutErrCapture()
|
||||
pool = WorkerPool()
|
||||
def f():
|
||||
pass
|
||||
pool.dispatch(f)
|
||||
pool.dispatch(f)
|
||||
pool.shutdown()
|
||||
pool.join(timeout=1.0)
|
||||
assert not pool._alive
|
||||
assert not pool._ready
|
||||
out, err = capture.reset()
|
||||
print out
|
||||
print >>sys.stderr, err
|
||||
assert err == ''
|
||||
Reference in New Issue
Block a user