2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
import py
|
2009-10-02 22:58:57 +08:00
|
|
|
import execnet
|
2009-03-23 04:44:45 +08:00
|
|
|
from py.__.test.dist.txnode import TXNode
|
2009-09-05 03:47:49 +08:00
|
|
|
queue = py.builtin._tryimport("queue", "Queue")
|
|
|
|
Queue = queue.Queue
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class EventQueue:
|
2009-04-10 02:07:05 +08:00
|
|
|
def __init__(self, registry, queue=None):
|
2009-02-27 18:18:27 +08:00
|
|
|
if queue is None:
|
2009-09-05 01:08:10 +08:00
|
|
|
queue = Queue()
|
2009-02-27 18:18:27 +08:00
|
|
|
self.queue = queue
|
2009-04-10 02:07:05 +08:00
|
|
|
registry.register(self)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
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)
|
2009-09-05 03:47:49 +08:00
|
|
|
except queue.Empty:
|
2009-02-27 18:18:27 +08:00
|
|
|
#print "node channel", self.node.channel
|
|
|
|
#print "remoteerror", self.node.channel._getremoteerror()
|
2009-08-30 02:04:48 +08:00
|
|
|
py.builtin.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-09 08:12:10 +08:00
|
|
|
if name == "pytest_internalerror":
|
2009-08-30 02:04:48 +08:00
|
|
|
py.builtin.print_(str(kwargs["excrepr"]))
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class MySetup:
|
2009-04-15 00:30:26 +08:00
|
|
|
def __init__(self, request):
|
2009-07-31 20:22:02 +08:00
|
|
|
self.id = 0
|
2009-04-15 00:30:26 +08:00
|
|
|
self.request = request
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def geteventargs(self, eventname, timeout=2.0):
|
2009-04-09 22:03:09 +08:00
|
|
|
eq = EventQueue(self.config.pluginmanager, self.queue)
|
2009-02-27 18:18:27 +08:00
|
|
|
return eq.geteventargs(eventname, timeout=timeout)
|
|
|
|
|
|
|
|
def makenode(self, config=None):
|
|
|
|
if config is None:
|
|
|
|
config = py.test.config._reparse([])
|
|
|
|
self.config = config
|
2009-09-05 01:08:10 +08:00
|
|
|
self.queue = Queue()
|
2009-10-02 22:58:57 +08:00
|
|
|
self.xspec = execnet.XSpec("popen")
|
|
|
|
self.gateway = execnet.makegateway(self.xspec)
|
2009-07-31 20:22:02 +08:00
|
|
|
self.id += 1
|
|
|
|
self.gateway.id = str(self.id)
|
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
|
2009-08-30 02:04:48 +08:00
|
|
|
py.builtin.print_("exiting:", gw)
|
2009-03-21 03:33:05 +08:00
|
|
|
gw.exit()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-15 00:30:26 +08:00
|
|
|
def pytest_funcarg__mysetup(request):
|
|
|
|
mysetup = MySetup(request)
|
2009-04-03 18:57:34 +08:00
|
|
|
#pyfuncitem.addfinalizer(mysetup.finalize)
|
2009-02-27 18:18:27 +08:00
|
|
|
return mysetup
|
|
|
|
|
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-09 08:12:10 +08:00
|
|
|
#ev = self.getcalls(pytest_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-06-09 00:31:10 +08:00
|
|
|
kwargs = mysetup.geteventargs("pytest_runtest_logreport")
|
2009-08-04 18:00:04 +08:00
|
|
|
rep = kwargs['report']
|
2009-04-09 07:33:48 +08:00
|
|
|
assert rep.passed
|
2009-08-30 02:04:48 +08:00
|
|
|
py.builtin.print_(rep)
|
2009-05-23 01:56:05 +08:00
|
|
|
assert rep.item == 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-06-09 00:31:10 +08:00
|
|
|
kwargs = mysetup.geteventargs("pytest_runtest_logreport")
|
2009-08-04 18:00:04 +08:00
|
|
|
report = kwargs['report']
|
|
|
|
assert getattr(report, 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-08-04 18:00:04 +08:00
|
|
|
rep = mysetup.geteventargs("pytest_runtest_logreport")['report']
|
2009-04-09 07:33:48 +08:00
|
|
|
assert getattr(rep, outcome)
|