1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
"""
Integration tests for the stem.control.BaseController class.
"""
import re
import threading
import time
import unittest
import stem.control
import test.runner
import stem.socket
import stem.util.system
class StateObserver(object):
"""
Simple container for listening to ControlSocket state changes and
rembembering them for the test.
"""
controller = None
state = None
timestamp = None
def reset(self):
self.controller = None
self.state = None
self.timestamp = None
def listener(self, controller, state, timestamp):
self.controller = controller
self.state = state
self.timestamp = timestamp
class TestBaseController(unittest.TestCase):
def test_connect_repeatedly(self):
"""
Connects and closes the socket repeatedly. This is a simple attempt to
trigger concurrency issues.
"""
if test.runner.require_control(self):
return
elif stem.util.system.is_mac():
test.runner.skip(self, '(ticket #6235)')
return
with test.runner.get_runner().get_tor_socket() as control_socket:
controller = stem.control.BaseController(control_socket)
for _ in xrange(250):
controller.connect()
controller.close()
def test_msg(self):
"""
Tests a basic query with the msg() method.
"""
if test.runner.require_control(self):
return
with test.runner.get_runner().get_tor_socket() as control_socket:
controller = stem.control.BaseController(control_socket)
test.runner.exercise_controller(self, controller)
def test_msg_invalid(self):
"""
Tests the msg() method against an invalid controller command.
"""
if test.runner.require_control(self):
return
with test.runner.get_runner().get_tor_socket() as control_socket:
controller = stem.control.BaseController(control_socket)
response = controller.msg('invalid')
self.assertEquals('Unrecognized command "invalid"', str(response))
def test_msg_invalid_getinfo(self):
"""
Tests the msg() method against a non-existant GETINFO option.
"""
if test.runner.require_control(self):
return
with test.runner.get_runner().get_tor_socket() as control_socket:
controller = stem.control.BaseController(control_socket)
response = controller.msg('GETINFO blarg')
self.assertEquals('Unrecognized key "blarg"', str(response))
def test_msg_repeatedly(self):
"""
Connects, sends a burst of messages, and closes the socket repeatedly. This
is a simple attempt to trigger concurrency issues.
"""
if test.runner.require_control(self):
return
elif stem.util.system.is_mac():
test.runner.skip(self, '(ticket #6235)')
return
with test.runner.get_runner().get_tor_socket() as control_socket:
controller = stem.control.BaseController(control_socket)
def run_getinfo():
for _ in xrange(150):
try:
controller.msg('GETINFO version')
controller.msg('GETINFO blarg')
controller.msg('blarg')
except stem.ControllerError:
pass
message_threads = []
for _ in xrange(5):
msg_thread = threading.Thread(target = run_getinfo)
message_threads.append(msg_thread)
msg_thread.setDaemon(True)
msg_thread.start()
for index in xrange(100):
controller.connect()
controller.close()
for msg_thread in message_threads:
msg_thread.join()
def test_asynchronous_event_handling(self):
"""
Check that we can both receive asynchronous events while hammering our
socket with queries, and checks that when a controller is closed the
listeners will still receive all of the enqueued events.
"""
if test.runner.require_control(self):
return
class ControlledListener(stem.control.BaseController):
"""
Controller that blocks event handling until told to do so.
"""
def __init__(self, control_socket):
stem.control.BaseController.__init__(self, control_socket)
self.received_events = []
self.receive_notice = threading.Event()
def _handle_event(self, event_message):
self.receive_notice.wait()
self.received_events.append(event_message)
with test.runner.get_runner().get_tor_socket() as control_socket:
controller = ControlledListener(control_socket)
controller.msg('SETEVENTS BW')
# Wait for a couple events for events to be enqueued. Doing a bunch of
# GETINFO queries while waiting to better exercise the asynchronous event
# handling.
start_time = time.time()
while (time.time() - start_time) < 3:
test.runner.exercise_controller(self, controller)
# 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.
close_thread = threading.Thread(target = controller.close, name = 'Closing controller')
close_thread.setDaemon(True)
close_thread.start()
# Finally start handling the BW events that we've received. We should
# have at least a couple of them.
controller.receive_notice.set()
close_thread.join()
self.assertTrue(len(controller.received_events) >= 2)
for bw_event in controller.received_events:
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.assertEquals(('650', ' '), bw_event.content()[0][:2])
def test_get_latest_heartbeat(self):
"""
Basic check for get_latest_heartbeat().
"""
if test.runner.require_control(self):
return
# makes a getinfo query, then checks that the heartbeat is close to now
with test.runner.get_runner().get_tor_socket() as control_socket:
controller = stem.control.BaseController(control_socket)
controller.msg('GETINFO version')
self.assertTrue((time.time() - controller.get_latest_heartbeat()) < 5)
def test_status_notifications(self):
"""
Checks basic functionality of the add_status_listener() and
remove_status_listener() methods.
"""
if test.runner.require_control(self):
return
state_observer = StateObserver()
with test.runner.get_runner().get_tor_socket(False) as control_socket:
controller = stem.control.BaseController(control_socket)
controller.add_status_listener(state_observer.listener, False)
controller.close()
self.assertEquals(controller, state_observer.controller)
self.assertEquals(stem.control.State.CLOSED, state_observer.state)
self.assertTrue(state_observer.timestamp <= time.time())
self.assertTrue(state_observer.timestamp > time.time() - 1.0)
state_observer.reset()
controller.connect()
self.assertEquals(controller, state_observer.controller)
self.assertEquals(stem.control.State.INIT, state_observer.state)
self.assertTrue(state_observer.timestamp <= time.time())
self.assertTrue(state_observer.timestamp > time.time() - 1.0)
state_observer.reset()
# cause the socket to shut down without calling close()
controller.msg('Blarg!')
self.assertRaises(stem.SocketClosed, controller.msg, 'blarg')
self.assertEquals(controller, state_observer.controller)
self.assertEquals(stem.control.State.CLOSED, state_observer.state)
self.assertTrue(state_observer.timestamp <= time.time())
self.assertTrue(state_observer.timestamp > time.time() - 1.0)
state_observer.reset()
# remove listener and make sure we don't get further notices
controller.remove_status_listener(state_observer.listener)
controller.connect()
self.assertEquals(None, state_observer.controller)
self.assertEquals(None, state_observer.state)
self.assertEquals(None, state_observer.timestamp)
state_observer.reset()
# add with spawn as true, we need a little delay on this since we then
# get the notice asynchronously
controller.add_status_listener(state_observer.listener, True)
controller.close()
time.sleep(0.1) # not much work going on so this doesn't need to be much
self.assertEquals(controller, state_observer.controller)
self.assertEquals(stem.control.State.CLOSED, state_observer.state)
self.assertTrue(state_observer.timestamp <= time.time())
self.assertTrue(state_observer.timestamp > time.time() - 1.0)
state_observer.reset()
|