Slave data stored on config for slave and node for master.

This commit is contained in:
Meme Dough
2010-04-22 22:21:58 +10:00
parent a1ab548cb3
commit 8d6bd3ecde
2 changed files with 31 additions and 23 deletions

View File

@@ -122,24 +122,33 @@ class TestDistribution:
c1 = testdir.makeconftest(""" c1 = testdir.makeconftest("""
# This hook only called on master. # This hook only called on master.
def pytest_testnodeready(node): def pytest_testnodeready(node):
node.slavedata['data'] = 42 node.slaveinput['a'] = 42
node.slaveinput['b'] = 7
# This hook take action on slave only. # This hook only take action on slave.
def pytest_sessionstart(session): def pytest_sessionstart(session):
if session.__class__.__name__ == 'SlaveNode': if hasattr(session.config, 'slaveinput'):
assert session.slavedata['data'] == 42 a = session.config.slaveinput['a']
session.slavereport['result'] = 7 b = session.config.slaveinput['b']
r = a + b
session.config.slaveoutput['r'] = r
# This hook only called on master. # This hook only called on master.
def pytest_testnodedown(node, error): def pytest_testnodedown(node, error):
result = node.slavereport['result'] node.config.calc_result = node.slaveoutput['r']
assert result == 7
# This hook only take action on master.
def pytest_terminal_summary(terminalreporter):
if not hasattr(terminalreporter.config, 'slaveinput'):
calc_result = terminalreporter.config.calc_result
terminalreporter._tw.sep('-', 'calculated result is %s' % calc_result)
""") """)
p1 = testdir.makepyfile("def test_func(): pass") p1 = testdir.makepyfile("def test_func(): pass")
result = testdir.runpytest(p1, '-d', '--tx=popen') result = testdir.runpytest(p1, '-d', '--tx=popen')
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*popen*Python*", "*popen*Python*",
"*calculated result is 49*",
"*1 passed*" "*1 passed*"
]) ])
assert result.ret == 0 assert result.ret == 0

View File

@@ -21,9 +21,9 @@ class TXNode(object):
self.channel = install_slave(gateway, config) self.channel = install_slave(gateway, config)
self.channel.setcallback(self.callback, endmarker=self.ENDMARK) self.channel.setcallback(self.callback, endmarker=self.ENDMARK)
self._down = False self._down = False
self.slavedatasent = False self.slaveinputsent = False
self.slavedata = {} self.slaveinput = {}
self.slavereport = {} self.slaveoutput = {}
def __repr__(self): def __repr__(self):
id = self.gateway.id id = self.gateway.id
@@ -56,7 +56,7 @@ class TXNode(object):
self.notify("pytest_testnodeready", node=self) self.notify("pytest_testnodeready", node=self)
elif eventname == "slavefinished": elif eventname == "slavefinished":
self._down = True self._down = True
self.slavereport = kwargs['slavereport'] self.slaveoutput = kwargs['slaveoutput']
self.notify("pytest_testnodedown", error=None, node=self) self.notify("pytest_testnodedown", error=None, node=self)
elif eventname in ("pytest_runtest_logreport", elif eventname in ("pytest_runtest_logreport",
"pytest__teardown_final_logerror"): "pytest__teardown_final_logerror"):
@@ -72,25 +72,25 @@ class TXNode(object):
py.builtin.print_("!" * 20, excinfo) py.builtin.print_("!" * 20, excinfo)
self.config.pluginmanager.notify_exception(excinfo) self.config.pluginmanager.notify_exception(excinfo)
def sendslaveinput(self):
if not self.slaveinputsent:
self.channel.send(self.slaveinput)
self.slaveinputsent = True
def send(self, item): def send(self, item):
if not self.slavedatasent:
self.channel.send(self.slavedata)
self.slavedatasent = True
assert item is not None assert item is not None
self.sendslaveinput()
self.channel.send(item) self.channel.send(item)
def sendlist(self, itemlist): def sendlist(self, itemlist):
if not self.slavedatasent: self.sendslaveinput()
self.channel.send(self.slavedata)
self.slavedatasent = True
self.channel.send(itemlist) self.channel.send(itemlist)
def shutdown(self, kill=False): def shutdown(self, kill=False):
if kill: if kill:
self.gateway.exit() self.gateway.exit()
else: else:
if not self.slavedatasent: self.sendslaveinput()
self.channel.send(None)
self.channel.send(None) self.channel.send(None)
# setting up slave code # setting up slave code
@@ -119,8 +119,6 @@ def install_slave(gateway, config):
class SlaveNode(object): class SlaveNode(object):
def __init__(self, channel): def __init__(self, channel):
self.channel = channel self.channel = channel
self.slavedata = {}
self.slavereport = {}
def __repr__(self): def __repr__(self):
return "<%s channel=%s>" %(self.__class__.__name__, self.channel) return "<%s channel=%s>" %(self.__class__.__name__, self.channel)
@@ -143,7 +141,8 @@ class SlaveNode(object):
self.config.pluginmanager.register(self) self.config.pluginmanager.register(self)
self.runner = self.config.pluginmanager.getplugin("pytest_runner") self.runner = self.config.pluginmanager.getplugin("pytest_runner")
self.sendevent("slaveready") self.sendevent("slaveready")
self.slavedata = channel.receive() self.config.slaveinput = channel.receive()
self.config.slaveoutput = {}
try: try:
self.config.hook.pytest_sessionstart(session=self) self.config.hook.pytest_sessionstart(session=self)
while 1: while 1:
@@ -165,7 +164,7 @@ class SlaveNode(object):
self.sendevent("pytest_internalerror", excrepr=er) self.sendevent("pytest_internalerror", excrepr=er)
raise raise
else: else:
self.sendevent("slavefinished", slavereport=self.slavereport) self.sendevent("slavefinished", slaveoutput=self.config.slaveoutput)
def run_single(self, item): def run_single(self, item):
call = self.runner.CallInfo(item._reraiseunpicklingproblem, when='setup') call = self.runner.CallInfo(item._reraiseunpicklingproblem, when='setup')