parent
e61e7b1e26
commit
d99f335918
|
@ -12,55 +12,61 @@ def test_threadout_install_deinstall():
|
||||||
assert old == sys.stdout
|
assert old == sys.stdout
|
||||||
|
|
||||||
class TestThreadOut:
|
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):
|
def test_threadout_one(self):
|
||||||
l = []
|
out = ThreadOut(sys, 'stdout')
|
||||||
self.out.setwritefunc(l.append)
|
try:
|
||||||
print 42,13,
|
l = []
|
||||||
x = l.pop(0)
|
out.setwritefunc(l.append)
|
||||||
assert x == '42'
|
print 42,13,
|
||||||
x = l.pop(0)
|
x = l.pop(0)
|
||||||
assert x == ' '
|
assert x == '42'
|
||||||
x = l.pop(0)
|
x = l.pop(0)
|
||||||
assert x == '13'
|
assert x == ' '
|
||||||
|
x = l.pop(0)
|
||||||
|
assert x == '13'
|
||||||
|
finally:
|
||||||
|
out.deinstall()
|
||||||
|
|
||||||
def test_threadout_multi_and_default(self):
|
def test_threadout_multi_and_default(self):
|
||||||
num = 3
|
out = ThreadOut(sys, 'stdout')
|
||||||
defaults = []
|
try:
|
||||||
def f(l):
|
num = 3
|
||||||
self.out.setwritefunc(l.append)
|
defaults = []
|
||||||
print id(l),
|
def f(l):
|
||||||
self.out.delwritefunc()
|
out.setwritefunc(l.append)
|
||||||
print 1
|
print id(l),
|
||||||
self.out.setdefaultwriter(defaults.append)
|
out.delwritefunc()
|
||||||
pool = WorkerPool()
|
print 1
|
||||||
listlist = []
|
out.setdefaultwriter(defaults.append)
|
||||||
for x in range(num):
|
pool = WorkerPool()
|
||||||
l = []
|
listlist = []
|
||||||
listlist.append(l)
|
for x in range(num):
|
||||||
pool.dispatch(f, l)
|
l = []
|
||||||
pool.shutdown()
|
listlist.append(l)
|
||||||
for name, value in self.out.__dict__.items():
|
pool.dispatch(f, l)
|
||||||
print >>sys.stderr, "%s: %s" %(name, value)
|
pool.shutdown()
|
||||||
pool.join(2.0)
|
for name, value in out.__dict__.items():
|
||||||
for i in range(num):
|
print >>sys.stderr, "%s: %s" %(name, value)
|
||||||
item = listlist[i]
|
pool.join(2.0)
|
||||||
assert item ==[str(id(item))]
|
for i in range(num):
|
||||||
assert not self.out._tid2out
|
item = listlist[i]
|
||||||
assert defaults
|
assert item ==[str(id(item))]
|
||||||
expect = ['1' for x in range(num)]
|
assert not out._tid2out
|
||||||
defaults = [x for x in defaults if x.strip()]
|
assert defaults
|
||||||
assert defaults == expect
|
expect = ['1' for x in range(num)]
|
||||||
|
defaults = [x for x in defaults if x.strip()]
|
||||||
|
assert defaults == expect
|
||||||
|
finally:
|
||||||
|
out.deinstall()
|
||||||
|
|
||||||
def test_threadout_nested(self):
|
def test_threadout_nested(self):
|
||||||
# we want ThreadOuts to coexist
|
out1 = ThreadOut(sys, 'stdout')
|
||||||
last = sys.stdout
|
try:
|
||||||
out = ThreadOut(sys, 'stdout')
|
# we want ThreadOuts to coexist
|
||||||
assert last == sys.stdout
|
last = sys.stdout
|
||||||
out.deinstall()
|
out = ThreadOut(sys, 'stdout')
|
||||||
assert last == sys.stdout
|
assert last == sys.stdout
|
||||||
|
out.deinstall()
|
||||||
|
assert last == sys.stdout
|
||||||
|
finally:
|
||||||
|
out1.deinstall()
|
||||||
|
|
Loading…
Reference in New Issue