[tor-commits] [stem/master] Allow float timeout when starting tor

atagar at torproject.org atagar at torproject.org
Mon Jan 30 18:31:26 UTC 2017


commit 20284e9785e408a85ea5e45aba0e4adc5cc77dc7
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jan 22 14:12:56 2017 -0800

    Allow float timeout when starting tor
    
    Supporting floats for our timeout when spawning tor. This in turn lets us shave
    a couple more seconds off our test runtime. Trick is thanks to...
    
      https://stackoverflow.com/questions/11901328/how-to-timeout-function-in-python-timeout-less-than-a-second
---
 stem/process.py       | 5 ++++-
 test/integ/process.py | 6 +++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/stem/process.py b/stem/process.py
index a9c8e76..08e0426 100644
--- a/stem/process.py
+++ b/stem/process.py
@@ -50,6 +50,9 @@ def launch_tor(tor_cmd = 'tor', args = None, torrc_path = None, completion_perce
   Note: The timeout argument does not work on Windows, and relies on the global
   state of the signal module.
 
+  .. versionchanged:: 1.6.0
+     Allowing the timeout argument to be a float.
+
   :param str tor_cmd: command for starting tor
   :param list args: additional arguments for tor
   :param str torrc_path: location of the torrc for us to use
@@ -119,7 +122,7 @@ def launch_tor(tor_cmd = 'tor', args = None, torrc_path = None, completion_perce
         raise OSError('reached a %i second timeout without success' % timeout)
 
       signal.signal(signal.SIGALRM, timeout_handler)
-      signal.alarm(timeout)
+      signal.setitimer(signal.ITIMER_REAL, timeout)
 
     bootstrap_line = re.compile('Bootstrapped ([0-9]+)%: ')
     problem_line = re.compile('\[(warn|err)\] (.*)$')
diff --git a/test/integ/process.py b/test/integ/process.py
index 9e13be5..b082d0b 100644
--- a/test/integ/process.py
+++ b/test/integ/process.py
@@ -378,11 +378,11 @@ class TestProcess(unittest.TestCase):
     runner = test.runner.get_runner()
     start_time = time.time()
     config = {'SocksPort': '2777', 'DataDirectory': self.data_directory}
-    self.assertRaises(OSError, stem.process.launch_tor_with_config, config, runner.get_tor_command(), 100, None, 2)
+    self.assertRaises(OSError, stem.process.launch_tor_with_config, config, runner.get_tor_command(), 100, None, 0.05)
     runtime = time.time() - start_time
 
-    if not (runtime > 2 and runtime < 3):
-      self.fail('Test should have taken 2-3 seconds, took %0.1f instead' % runtime)
+    if not (runtime > 0.05 and runtime < 1):
+      self.fail('Test should have taken 0.05-1 seconds, took %0.1f instead' % runtime)
 
   @require_version(stem.version.Requirement.TAKEOWNERSHIP)
   @only_run_once





More information about the tor-commits mailing list