From 6bdaaf368c245ea6a0f20c199e3690bbc1ed29c2 Mon Sep 17 00:00:00 2001 From: laharah Date: Fri, 17 Jun 2016 22:03:58 -0700 Subject: [PATCH] Moved slow connection notification to stderr added test for slow connection notifications --- src/wormhole/cli/cmd_receive.py | 5 ++-- src/wormhole/cli/cmd_send.py | 5 ++-- src/wormhole/test/test_scripts.py | 39 ++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/wormhole/cli/cmd_receive.py b/src/wormhole/cli/cmd_receive.py index 07adb48..c23b44a 100644 --- a/src/wormhole/cli/cmd_receive.py +++ b/src/wormhole/cli/cmd_receive.py @@ -12,6 +12,7 @@ from ..util import (dict_to_bytes, bytes_to_dict, bytes_to_hexstr, estimate_free_space) APPID = u"lothar.com/wormhole/text-or-file-xfer" +VERIFY_TIMER = 1 class RespondError(Exception): def __init__(self, response): @@ -78,8 +79,8 @@ class TwistedReceiver: yield w.establish_key() def on_slow_connection(): print(u"Key established, waiting for confirmation...", - file=self.args.stdout) - notify = self._reactor.callLater(1, on_slow_connection) + file=self.args.stderr) + notify = self._reactor.callLater(VERIFY_TIMER, on_slow_connection) try: verifier = yield w.verify() finally: diff --git a/src/wormhole/cli/cmd_send.py b/src/wormhole/cli/cmd_send.py index 8ab914b..2c46af8 100644 --- a/src/wormhole/cli/cmd_send.py +++ b/src/wormhole/cli/cmd_send.py @@ -12,6 +12,7 @@ from ..transit import TransitSender from ..util import dict_to_bytes, bytes_to_dict, bytes_to_hexstr APPID = u"lothar.com/wormhole/text-or-file-xfer" +VERIFY_TIMER = 1 def send(args, reactor=reactor): """I implement 'wormhole send'. I return a Deferred that fires with None @@ -88,8 +89,8 @@ class Sender: yield w.establish_key() def on_slow_connection(): print(u"Key established, waiting for confirmation...", - file=args.stdout) - notify = self._reactor.callLater(1, on_slow_connection) + file=args.stderr) + notify = self._reactor.callLater(VERIFY_TIMER, on_slow_connection) # TODO: don't stall on w.verify() unless they want it try: diff --git a/src/wormhole/test/test_scripts.py b/src/wormhole/test/test_scripts.py index e57f2fb..2af0ce7 100644 --- a/src/wormhole/test/test_scripts.py +++ b/src/wormhole/test/test_scripts.py @@ -225,9 +225,9 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): @inlineCallbacks def _do_test(self, as_subprocess=False, mode="text", addslash=False, override_filename=False): - assert mode in ("text", "file", "directory") - send_cfg = config("send") - recv_cfg = config("receive") + assert mode in ("text", "file", "directory", "slow") + send_cfg = Config() + recv_cfg = Config() message = "blah blah blah ponies" for cfg in [send_cfg, recv_cfg]: @@ -244,7 +244,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): receive_dir = self.mktemp() os.mkdir(receive_dir) - if mode == "text": + if mode == "text" or mode == "slow": send_cfg.text = message elif mode == "file": @@ -348,8 +348,14 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): # The sender might fail, leaving the receiver hanging, or vice # versa. Make sure we don't wait on one side exclusively + if mode == "slow": + with mock.patch.object(cmd_send, "VERIFY_TIMER", 0), \ + mock.patch.object(cmd_receive, "VERIFY_TIMER", 0): + yield gatherResults([send_d, receive_d], True) + else: + yield gatherResults([send_d, receive_d], True) + - yield gatherResults([send_d, receive_d], True) send_stdout = send_cfg.stdout.getvalue() send_stderr = send_cfg.stderr.getvalue() receive_stdout = recv_cfg.stdout.getvalue() @@ -361,13 +367,21 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): self.maxDiff = None # show full output for assertion failures - self.failUnlessEqual(send_stderr, "", - (send_stdout, send_stderr)) - self.failUnlessEqual(receive_stderr, "", - (receive_stdout, receive_stderr)) + if mode != "slow": + self.failUnlessEqual(send_stderr, "", + (send_stdout, send_stderr)) + self.failUnlessEqual(receive_stderr, "", + (receive_stdout, receive_stderr)) + else: + self.assertEqual(send_stderr, + "Key established, waiting for confirmation...\n", + (send_stdout, send_stderr)) + self.assertEqual(receive_stderr, + "Key established, waiting for confirmation...\n", + (receive_stdout, receive_stderr)) # check sender - if mode == "text": + if mode == "text" or mode == "slow": expected = ("Sending text message ({bytes:d} Bytes){NL}" "On the other computer, please run: " "wormhole receive{NL}" @@ -401,7 +415,7 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): .format(NL=NL), send_stdout) # check receiver - if mode == "text": + if mode == "text" or mode == "slow": self.failUnlessEqual(receive_stdout, message+NL) elif mode == "file": self.failUnlessIn("Receiving file ({size:s}) into: {name}" @@ -448,6 +462,9 @@ class PregeneratedCode(ServerBase, ScriptsBase, unittest.TestCase): def test_directory_override(self): return self._do_test(mode="directory", override_filename=True) + def test_slow(self): + return self._do_test(mode="slow") + @inlineCallbacks def _do_test_fail(self, mode, failmode): assert mode in ("file", "directory")