2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
import py
|
2009-03-23 04:44:45 +08:00
|
|
|
from py.__.test.dist.txnode import TXNode
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class EventQueue:
|
|
|
|
def __init__(self, bus, queue=None):
|
|
|
|
if queue is None:
|
|
|
|
queue = py.std.Queue.Queue()
|
|
|
|
self.queue = queue
|
|
|
|
bus.register(self)
|
|
|
|
|
2009-04-03 18:57:34 +08:00
|
|
|
def pyevent(self, eventname, args, kwargs):
|
2009-02-27 18:18:27 +08:00
|
|
|
self.queue.put((eventname, args, kwargs))
|
|
|
|
|
|
|
|
def geteventargs(self, eventname, timeout=2.0):
|
2008-08-16 23:26:59 +08:00
|
|
|
events = []
|
|
|
|
while 1:
|
|
|
|
try:
|
2009-02-27 18:18:27 +08:00
|
|
|
eventcall = self.queue.get(timeout=timeout)
|
2008-08-16 23:26:59 +08:00
|
|
|
except py.std.Queue.Empty:
|
2009-02-27 18:18:27 +08:00
|
|
|
#print "node channel", self.node.channel
|
|
|
|
#print "remoteerror", self.node.channel._getremoteerror()
|
2008-08-16 23:26:59 +08:00
|
|
|
print "seen events", events
|
2009-02-27 18:18:27 +08:00
|
|
|
raise IOError("did not see %r events" % (eventname))
|
2008-08-16 23:26:59 +08:00
|
|
|
else:
|
2009-02-27 18:18:27 +08:00
|
|
|
name, args, kwargs = eventcall
|
|
|
|
assert isinstance(name, str)
|
|
|
|
if name == eventname:
|
2009-04-09 07:33:48 +08:00
|
|
|
if args:
|
|
|
|
return args
|
|
|
|
return kwargs
|
2009-02-27 18:18:27 +08:00
|
|
|
events.append(name)
|
2009-04-04 06:36:29 +08:00
|
|
|
if name == "internalerror":
|
|
|
|
print str(kwargs["excrepr"])
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class MySetup:
|
|
|
|
def __init__(self, pyfuncitem):
|
|
|
|
self.pyfuncitem = pyfuncitem
|
|
|
|
|
|
|
|
def geteventargs(self, eventname, timeout=2.0):
|
|
|
|
eq = EventQueue(self.config.bus, self.queue)
|
|
|
|
return eq.geteventargs(eventname, timeout=timeout)
|
|
|
|
|
|
|
|
def makenode(self, config=None):
|
|
|
|
if config is None:
|
|
|
|
config = py.test.config._reparse([])
|
|
|
|
self.config = config
|
2008-08-16 23:26:59 +08:00
|
|
|
self.queue = py.std.Queue.Queue()
|
2009-03-21 03:04:36 +08:00
|
|
|
self.xspec = py.execnet.XSpec("popen")
|
|
|
|
self.gateway = py.execnet.makegateway(self.xspec)
|
2009-03-23 04:44:45 +08:00
|
|
|
self.node = TXNode(self.gateway, self.config, putevent=self.queue.put)
|
2008-08-16 23:26:59 +08:00
|
|
|
assert not self.node.channel.isclosed()
|
2009-02-27 18:18:27 +08:00
|
|
|
return self.node
|
|
|
|
|
2009-04-03 18:57:34 +08:00
|
|
|
def xfinalize(self):
|
2009-03-21 03:33:05 +08:00
|
|
|
if hasattr(self, 'node'):
|
|
|
|
gw = self.node.gateway
|
|
|
|
print "exiting:", gw
|
|
|
|
gw.exit()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-03-26 20:50:12 +08:00
|
|
|
def pytest_funcarg__mysetup(pyfuncitem):
|
2009-02-27 18:18:27 +08:00
|
|
|
mysetup = MySetup(pyfuncitem)
|
2009-04-03 18:57:34 +08:00
|
|
|
#pyfuncitem.addfinalizer(mysetup.finalize)
|
2009-02-27 18:18:27 +08:00
|
|
|
return mysetup
|
|
|
|
|
2009-03-26 20:50:12 +08:00
|
|
|
def pytest_funcarg__testdir(__call__, pyfuncitem):
|
2009-03-04 01:42:32 +08:00
|
|
|
# decorate to make us always change to testdir
|
|
|
|
testdir = __call__.execute(firstresult=True)
|
|
|
|
testdir.chdir()
|
|
|
|
return testdir
|
|
|
|
|
2009-03-21 03:04:36 +08:00
|
|
|
def test_node_hash_equality(mysetup):
|
|
|
|
node = mysetup.makenode()
|
|
|
|
node2 = mysetup.makenode()
|
|
|
|
assert node != node2
|
|
|
|
assert node == node
|
|
|
|
assert not (node != node)
|
2008-09-02 21:22:10 +08:00
|
|
|
|
2009-03-21 03:04:36 +08:00
|
|
|
class TestMasterSlaveConnection:
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_crash_invalid_item(self, mysetup):
|
|
|
|
node = mysetup.makenode()
|
|
|
|
node.send(123) # invalid item
|
2009-04-09 07:33:48 +08:00
|
|
|
kwargs = mysetup.geteventargs("pytest_testnodedown")
|
|
|
|
assert kwargs['node'] is node
|
|
|
|
assert str(kwargs['error']).find("AttributeError") != -1
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_crash_killed(self, testdir, mysetup):
|
2008-08-16 23:26:59 +08:00
|
|
|
if not hasattr(py.std.os, 'kill'):
|
|
|
|
py.test.skip("no os.kill")
|
2009-02-27 18:18:27 +08:00
|
|
|
item = testdir.getitem("""
|
2008-09-02 21:22:10 +08:00
|
|
|
def test_func():
|
|
|
|
import os
|
|
|
|
os.kill(os.getpid(), 15)
|
|
|
|
""")
|
2009-03-18 07:48:07 +08:00
|
|
|
node = mysetup.makenode(item.config)
|
2009-02-27 18:18:27 +08:00
|
|
|
node.send(item)
|
2009-04-09 07:33:48 +08:00
|
|
|
kwargs = mysetup.geteventargs("pytest_testnodedown")
|
|
|
|
assert kwargs['node'] is node
|
|
|
|
assert str(kwargs['error']).find("Not properly terminated") != -1
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_node_down(self, mysetup):
|
|
|
|
node = mysetup.makenode()
|
|
|
|
node.shutdown()
|
2009-04-09 07:33:48 +08:00
|
|
|
kwargs = mysetup.geteventargs("pytest_testnodedown")
|
|
|
|
assert kwargs['node'] is node
|
|
|
|
assert not kwargs['error']
|
2009-02-27 18:18:27 +08:00
|
|
|
node.callback(node.ENDMARK)
|
2008-08-16 23:26:59 +08:00
|
|
|
excinfo = py.test.raises(IOError,
|
2009-03-20 09:09:28 +08:00
|
|
|
"mysetup.geteventargs('testnodedown', timeout=0.01)")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_send_on_closed_channel(self, testdir, mysetup):
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
2009-03-18 07:48:07 +08:00
|
|
|
node = mysetup.makenode(item.config)
|
2009-02-27 18:18:27 +08:00
|
|
|
node.channel.close()
|
|
|
|
py.test.raises(IOError, "node.send(item)")
|
2009-04-07 17:53:01 +08:00
|
|
|
#ev = self.getcalls("internalerror")
|
2008-08-16 23:26:59 +08:00
|
|
|
#assert ev.excinfo.errisinstance(IOError)
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_send_one(self, testdir, mysetup):
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
2009-03-18 07:48:07 +08:00
|
|
|
node = mysetup.makenode(item.config)
|
2009-02-27 18:18:27 +08:00
|
|
|
node.send(item)
|
2009-04-09 07:33:48 +08:00
|
|
|
kwargs = mysetup.geteventargs("pytest_itemtestreport")
|
|
|
|
rep = kwargs['rep']
|
|
|
|
assert rep.passed
|
|
|
|
print rep
|
|
|
|
assert rep.colitem == item
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_send_some(self, testdir, mysetup):
|
|
|
|
items = testdir.getitems("""
|
2008-09-02 21:22:10 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_skip():
|
|
|
|
import py
|
|
|
|
py.test.skip("x")
|
|
|
|
""")
|
2009-03-18 07:48:07 +08:00
|
|
|
node = mysetup.makenode(items[0].config)
|
2008-09-02 21:22:10 +08:00
|
|
|
for item in items:
|
2009-02-27 18:18:27 +08:00
|
|
|
node.send(item)
|
2008-08-16 23:26:59 +08:00
|
|
|
for outcome in "passed failed skipped".split():
|
2009-04-09 07:33:48 +08:00
|
|
|
kwargs = mysetup.geteventargs("pytest_itemtestreport")
|
|
|
|
rep = kwargs['rep']
|
|
|
|
assert getattr(rep, outcome)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
node.sendlist(items)
|
2008-08-16 23:26:59 +08:00
|
|
|
for outcome in "passed failed skipped".split():
|
2009-04-09 07:33:48 +08:00
|
|
|
rep = mysetup.geteventargs("pytest_itemtestreport")['rep']
|
|
|
|
assert getattr(rep, outcome)
|