Instead of abusing the process transport, introduce a new object

This commit is contained in:
meejah 2019-08-08 15:28:54 -06:00
parent 731c58754f
commit e7c387a7fc
4 changed files with 51 additions and 101 deletions

View File

@ -30,7 +30,7 @@ from util import (
_ProcessExitedProtocol,
_create_node,
_run_node,
_cleanup_twistd_process,
_cleanup_tahoe_process,
_tahoe_runner_optional_coverage,
)
@ -130,7 +130,7 @@ def flog_gatherer(reactor, temp_dir, flog_binary, request):
pytest_twisted.blockon(twistd_protocol.magic_seen)
def cleanup():
_cleanup_twistd_process(twistd_process, twistd_protocol.exited)
_cleanup_tahoe_process(twistd_process, twistd_protocol.exited)
flog_file = mktemp('.flog_dump')
flog_protocol = _DumpOutputProtocol(open(flog_file, 'w'))
@ -209,7 +209,7 @@ log_gatherer.furl = {log_furl}
intro_dir,
),
)
request.addfinalizer(partial(_cleanup_twistd_process, process, protocol.exited))
request.addfinalizer(partial(_cleanup_tahoe_process, process, protocol.exited))
pytest_twisted.blockon(protocol.magic_seen)
return process
@ -279,8 +279,8 @@ log_gatherer.furl = {log_furl}
def cleanup():
try:
process.signalProcess('TERM')
pytest_twisted.blockon(protocol.exited)
process.transport.signalProcess('TERM')
pytest_twisted.blockon(process.transport.exited)
except ProcessExitedAlready:
pass
request.addfinalizer(cleanup)
@ -402,8 +402,8 @@ def alice_invite(reactor, alice, temp_dir, request):
# before magic-folder works, we have to stop and restart (this is
# crappy for the tests -- can we fix it in magic-folder?)
try:
alice.signalProcess('TERM')
pytest_twisted.blockon(alice.exited)
alice.transport.signalProcess('TERM')
pytest_twisted.blockon(alice.transport.exited)
except ProcessExitedAlready:
pass
with start_action(action_type=u"integration:alice:magic_folder:magic-text"):
@ -439,8 +439,8 @@ def magic_folder(reactor, alice_invite, alice, bob, temp_dir, request):
# crappy for the tests -- can we fix it in magic-folder?)
try:
print("Sending TERM to Bob")
bob.signalProcess('TERM')
pytest_twisted.blockon(bob.exited)
bob.transport.signalProcess('TERM')
pytest_twisted.blockon(bob.transport.exited)
except ProcessExitedAlready:
pass

View File

@ -336,10 +336,10 @@ def test_edmond_uploads_then_restarts(reactor, request, temp_dir, introducer_fur
assert created, "Didn't create a magic-folder"
# to actually-start the magic-folder we have to re-start
edmond.signalProcess('TERM')
yield edmond._protocol.exited
edmond.transport.signalProcess('TERM')
yield edmond.transport.exited
time.sleep(1)
edmond = yield util._run_node(reactor, edmond._node_dir, request, 'Completed initial Magic Folder scan successfully')
edmond = yield util._run_node(reactor, edmond.node_dir, request, 'Completed initial Magic Folder scan successfully')
# add a thing to the magic-folder
with open(join(magic_folder, "its_a_file"), "w") as f:
@ -383,10 +383,10 @@ def test_edmond_uploads_then_restarts(reactor, request, temp_dir, introducer_fur
# re-starting edmond right now would "normally" trigger the 2880 bug
# kill edmond
edmond.signalProcess('TERM')
yield edmond._protocol.exited
edmond.transport.signalProcess('TERM')
yield edmond.transport.exited
time.sleep(1)
edmond = yield util._run_node(reactor, edmond._node_dir, request, 'Completed initial Magic Folder scan successfully')
edmond = yield util._run_node(reactor, edmond.node_dir, request, 'Completed initial Magic Folder scan successfully')
# XXX how can we say for sure if we've waited long enough? look at
# tail of logs for magic-folder ... somethingsomething?

View File

@ -1,74 +0,0 @@
import sys
import time
import shutil
from os import mkdir, unlink, utime
from os.path import join, exists, getmtime
import util
import pytest_twisted
def test_index(alice):
"""
we can download the index file
"""
util.web_get(alice._node_dir, "")
def test_upload_download(alice):
"""
upload a file, then download it via readcap
"""
# XXX FIXME why?
print("waiting for ready..")
time.sleep(10)
FILE_CONTENTS = "some contents"
readcap = util.web_post(
alice._node_dir,
"uri",
data={
"t": "upload",
"format": "mdmf",
},
files={
"file": FILE_CONTENTS,
},
)
readcap = readcap.strip()
print("readcap '{}'".format(readcap))
data = util.web_get(
alice._node_dir, "uri",
params={
"uri": readcap,
"filename": "boom",
}
)
assert data == FILE_CONTENTS
def test_put(alice):
"""
use PUT to create a file
"""
import time; time.sleep(10) # XXX wat
FILE_CONTENTS = "added via PUT"
import requests
resp = requests.put(
util.node_url(alice._node_dir, "uri"),
files={
"file": FILE_CONTENTS,
},
)
print(resp)
print(resp.status_code)
print(resp.text)
print("\n\n\n\n\n\n\n")
assert resp.text.strip().startswith("URI:CHK:")
assert resp.text.strip().endswith(":2:4:153")

View File

@ -108,19 +108,19 @@ class _MagicTextProtocol(ProcessProtocol):
sys.stdout.write(data)
def _cleanup_twistd_process(twistd_process, exited):
def _cleanup_tahoe_process(tahoe_transport, exited):
"""
Terminate the given process with a kill signal (SIGKILL on POSIX,
TerminateProcess on Windows).
:param twistd_process: The `IProcessTransport` representing the process.
:param tahoe_transport: The `IProcessTransport` representing the process.
:param exited: A `Deferred` which fires when the process has exited.
:return: After the process has exited.
"""
try:
print("signaling {} with TERM".format(twistd_process.pid))
twistd_process.signalProcess('TERM')
print("signaling {} with TERM".format(tahoe_transport.pid))
tahoe_transport.signalProcess('TERM')
print("signaled, blocking on exit")
pytest_twisted.blockon(exited)
print("exited, goodbye")
@ -146,7 +146,30 @@ def _tahoe_runner_optional_coverage(proto, reactor, request, other_args):
)
class TahoeProcess(object):
"""
A running Tahoe process, with associated information.
"""
def __init__(self, process_transport, node_dir):
self._process_transport = process_transport # IProcessTransport instance
self._node_dir = node_dir # path
@property
def transport(self):
return self._process_transport
@property
def node_dir(self):
return self._node_dir
def _run_node(reactor, node_dir, request, magic_text):
"""
Run a tahoe process from its node_dir.
:returns: a TahoeProcess for this node
"""
if magic_text is None:
magic_text = "client running"
protocol = _MagicTextProtocol(magic_text)
@ -155,7 +178,7 @@ def _run_node(reactor, node_dir, request, magic_text):
# but on linux it means daemonize. "tahoe run" is consistent
# between platforms.
process = _tahoe_runner_optional_coverage(
transport = _tahoe_runner_optional_coverage(
protocol,
reactor,
request,
@ -165,17 +188,18 @@ def _run_node(reactor, node_dir, request, magic_text):
node_dir,
],
)
process.exited = protocol.exited
transport.exited = protocol.exited
request.addfinalizer(partial(_cleanup_twistd_process, process, protocol.exited))
request.addfinalizer(partial(_cleanup_tahoe_process, transport, protocol.exited))
# we return the 'process' ITransport instance
# XXX abusing the Deferred; should use .when_magic_seen() or something?
# XXX abusing the Deferred; should use .when_magic_seen() pattern
def got_proto(proto):
process._protocol = proto
process._node_dir = node_dir
return process
transport._protocol = proto
return TahoeProcess(
transport,
node_dir,
)
protocol.magic_seen.addCallback(got_proto)
return protocol.magic_seen