commit d709e50ae789acc55a4e05056c7b22b657a861b4 Author: Damian Johnson atagar@torproject.org Date: Fri Jan 20 11:32:35 2017 -0800
Replace test_bw_event with a setconf test
Replacing another spot where we wait for BW events with a triggered CONF_CHANGED. This shaves a couple more seconds off our test runtime. --- test/integ/control/base_controller.py | 9 +++++---- test/integ/control/controller.py | 18 +++++------------- test/integ/socket/control_message.py | 22 +++++++++++++--------- test/mocking.py | 10 ++++++++++ 4 files changed, 33 insertions(+), 26 deletions(-)
diff --git a/test/integ/control/base_controller.py b/test/integ/control/base_controller.py index 2507293..51576a8 100644 --- a/test/integ/control/base_controller.py +++ b/test/integ/control/base_controller.py @@ -2,8 +2,6 @@ Integration tests for the stem.control.BaseController class. """
-import hashlib -import os import re import threading import time @@ -13,6 +11,7 @@ import stem.control import stem.socket import stem.util.system
+import test.mocking import test.runner
from test.runner import require_controller @@ -155,10 +154,12 @@ class TestBaseController(unittest.TestCase): controller.msg('SETEVENTS CONF_CHANGED')
for i in range(10): - random_fingerprint = hashlib.sha1(os.urandom(20)).hexdigest().upper() - controller.msg('SETCONF NodeFamily=%s' % random_fingerprint) + controller.msg('SETCONF NodeFamily=%s' % test.mocking.random_fingerprint()) test.runner.exercise_controller(self, controller)
+ controller.msg('SETEVENTS') + controller.msg('RESETCONF NodeFamily') + # Concurrently shut down the controller. We need to do this in another # thread because it'll block on the event handling, which in turn is # currently blocking on the reveive_notice. diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py index f7b6555..0d17b0b 100644 --- a/test/integ/control/controller.py +++ b/test/integ/control/controller.py @@ -2,7 +2,6 @@ Integration tests for the stem.control.Controller class. """
-import hashlib import os import shutil import socket @@ -19,6 +18,7 @@ import stem.response.protocolinfo import stem.socket import stem.util.str_tools import stem.version +import test.mocking import test.network import test.runner
@@ -42,14 +42,6 @@ from test.runner import ( TEST_ROUTER_STATUS_ENTRY = None
-def random_fingerprint(): - """ - Provides a random 40 character hex string. - """ - - return hashlib.sha1(os.urandom(20)).hexdigest().upper() - - class TestController(unittest.TestCase): @only_run_once @require_controller @@ -158,7 +150,7 @@ class TestController(unittest.TestCase): controller.add_event_listener(listener2, EventType.CONF_CHANGED, EventType.DEBUG)
# The NodeFamily is a harmless option we can toggle - controller.set_conf('NodeFamily', random_fingerprint()) + controller.set_conf('NodeFamily', test.mocking.random_fingerprint())
# Wait for the event. Assert that we get it within 10 seconds event_notice1.wait(10) @@ -175,7 +167,7 @@ class TestController(unittest.TestCase):
buffer2_size = len(event_buffer2)
- controller.set_conf('NodeFamily', random_fingerprint()) + controller.set_conf('NodeFamily', test.mocking.random_fingerprint()) event_notice1.wait(10) self.assertEqual(len(event_buffer1), 2) event_notice1.clear() @@ -212,7 +204,7 @@ class TestController(unittest.TestCase):
# trigger an event
- controller.set_conf('NodeFamily', random_fingerprint()) + controller.set_conf('NodeFamily', test.mocking.random_fingerprint()) event_notice.wait(4) self.assertTrue(len(event_buffer) >= 1)
@@ -225,7 +217,7 @@ class TestController(unittest.TestCase): controller.connect() controller.authenticate(password = test.runner.CONTROL_PASSWORD) self.assertTrue(len(event_buffer) == 0) - controller.set_conf('NodeFamily', random_fingerprint()) + controller.set_conf('NodeFamily', test.mocking.random_fingerprint())
event_notice.wait(4) self.assertTrue(len(event_buffer) >= 1) diff --git a/test/integ/socket/control_message.py b/test/integ/socket/control_message.py index 567a78d..b873a59 100644 --- a/test/integ/socket/control_message.py +++ b/test/integ/socket/control_message.py @@ -7,6 +7,7 @@ import unittest
import stem.socket import stem.version +import test.mocking import test.runner
from test.runner import ( @@ -145,23 +146,26 @@ class TestControlMessage(unittest.TestCase): self.assertTrue('%s' % torrc_entry in config_text_response.content()[0][2])
@require_controller - def test_bw_event(self): + def test_setconf_event(self): """ - Issues 'SETEVENTS BW' and parses a couple events. + Issues 'SETEVENTS CONF_CHANGED' and parses an events. """
with test.runner.get_runner().get_tor_socket() as control_socket: - control_socket.send('SETEVENTS BW') + control_socket.send('SETEVENTS CONF_CHANGED') setevents_response = control_socket.recv() self.assertEqual('OK', str(setevents_response)) self.assertEqual(['OK'], list(setevents_response)) self.assertEqual('250 OK\r\n', setevents_response.raw_content()) self.assertEqual([('250', ' ', 'OK')], setevents_response.content())
- # Tor will emit a BW event once per second. Parsing two of them. + # CONF_CHANGED event will come before the SETCONF 'OK' response
- for _ in range(2): - bw_event = control_socket.recv() - self.assertTrue(re.match('BW [0-9]+ [0-9]+', str(bw_event))) - self.assertTrue(re.match('650 BW [0-9]+ [0-9]+\r\n', bw_event.raw_content())) - self.assertEqual(('650', ' '), bw_event.content()[0][:2]) + control_socket.send('SETCONF NodeFamily=%s' % test.mocking.random_fingerprint()) + + conf_changed_event = control_socket.recv() + self.assertTrue(re.match('CONF_CHANGED\nNodeFamily=.*', str(conf_changed_event))) + self.assertTrue(re.match('650-CONF_CHANGED\r\n650-NodeFamily=.*\r\n650 OK', conf_changed_event.raw_content())) + self.assertEqual(('650', '-'), conf_changed_event.content()[0][:2]) + + self.assertEqual([('250', ' ', 'OK')], control_socket.recv().content()) diff --git a/test/mocking.py b/test/mocking.py index 7ffb11c..cb576d8 100644 --- a/test/mocking.py +++ b/test/mocking.py @@ -7,6 +7,7 @@ Helper functions for creating mock objects. ::
get_all_combinations - provides all combinations of attributes + random_fingerprint - provides a random relay fingerprint
Instance Constructors get_message - stem.response.ControlMessage @@ -41,6 +42,7 @@ Helper functions for creating mock objects. import base64 import hashlib import itertools +import os import re import textwrap
@@ -242,6 +244,14 @@ def get_all_combinations(attr, include_empty = False): yield item
+def random_fingerprint(): + """ + Provides a random relay fingerprint. + """ + + return hashlib.sha1(os.urandom(20)).hexdigest().upper() + + def get_message(content, reformat = True): """ Provides a ControlMessage with content modified to be parsable. This makes
tor-commits@lists.torproject.org