/usr/lib/python2.7/dist-packages/kazoo/tests/test_threading_handler.py is in python-kazoo 2.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | import threading
import unittest
import mock
from nose.tools import assert_raises
from nose.tools import eq_
from nose.tools import raises
class TestThreadingHandler(unittest.TestCase):
def _makeOne(self, *args):
from kazoo.handlers.threading import SequentialThreadingHandler
return SequentialThreadingHandler(*args)
def _getAsync(self, *args):
from kazoo.handlers.threading import AsyncResult
return AsyncResult
def test_proper_threading(self):
h = self._makeOne()
h.start()
# In Python 3.3 _Event is gone, before Event is function
event_class = getattr(threading, '_Event', threading.Event)
assert isinstance(h.event_object(), event_class)
def test_matching_async(self):
h = self._makeOne()
h.start()
async = self._getAsync()
assert isinstance(h.async_result(), async)
def test_exception_raising(self):
h = self._makeOne()
@raises(h.timeout_exception)
def testit():
raise h.timeout_exception("This is a timeout")
testit()
def test_double_start_stop(self):
h = self._makeOne()
h.start()
self.assertTrue(h._running)
h.start()
h.stop()
h.stop()
self.assertFalse(h._running)
class TestThreadingAsync(unittest.TestCase):
def _makeOne(self, *args):
from kazoo.handlers.threading import AsyncResult
return AsyncResult(*args)
def _makeHandler(self):
from kazoo.handlers.threading import SequentialThreadingHandler
return SequentialThreadingHandler()
def test_ready(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
eq_(async.ready(), False)
async.set('val')
eq_(async.ready(), True)
eq_(async.successful(), True)
eq_(async.exception, None)
def test_callback_queued(self):
mock_handler = mock.Mock()
mock_handler.completion_queue = mock.Mock()
async = self._makeOne(mock_handler)
async.rawlink(lambda a: a)
async.set('val')
assert mock_handler.completion_queue.put.called
def test_set_exception(self):
mock_handler = mock.Mock()
mock_handler.completion_queue = mock.Mock()
async = self._makeOne(mock_handler)
async.rawlink(lambda a: a)
async.set_exception(ImportError('Error occured'))
assert isinstance(async.exception, ImportError)
assert mock_handler.completion_queue.put.called
def test_get_wait_while_setting(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
lst = []
bv = threading.Event()
cv = threading.Event()
def wait_for_val():
bv.set()
val = async.get()
lst.append(val)
cv.set()
th = threading.Thread(target=wait_for_val)
th.start()
bv.wait()
async.set('fred')
cv.wait()
eq_(lst, ['fred'])
th.join()
def test_get_with_nowait(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
timeout = self._makeHandler().timeout_exception
@raises(timeout)
def test_it():
async.get(block=False)
test_it()
@raises(timeout)
def test_nowait():
async.get_nowait()
test_nowait()
def test_get_with_exception(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
lst = []
bv = threading.Event()
cv = threading.Event()
def wait_for_val():
bv.set()
try:
val = async.get()
except ImportError:
lst.append('oops')
else:
lst.append(val)
cv.set()
th = threading.Thread(target=wait_for_val)
th.start()
bv.wait()
async.set_exception(ImportError)
cv.wait()
eq_(lst, ['oops'])
th.join()
def test_wait(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
lst = []
bv = threading.Event()
cv = threading.Event()
def wait_for_val():
bv.set()
try:
val = async.wait(10)
except ImportError:
lst.append('oops')
else:
lst.append(val)
cv.set()
th = threading.Thread(target=wait_for_val)
th.start()
bv.wait(10)
async.set("fred")
cv.wait(15)
eq_(lst, [True])
th.join()
def test_set_before_wait(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
lst = []
cv = threading.Event()
async.set('fred')
def wait_for_val():
val = async.get()
lst.append(val)
cv.set()
th = threading.Thread(target=wait_for_val)
th.start()
cv.wait()
eq_(lst, ['fred'])
th.join()
def test_set_exc_before_wait(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
lst = []
cv = threading.Event()
async.set_exception(ImportError)
def wait_for_val():
try:
val = async.get()
except ImportError:
lst.append('ooops')
else:
lst.append(val)
cv.set()
th = threading.Thread(target=wait_for_val)
th.start()
cv.wait()
eq_(lst, ['ooops'])
th.join()
def test_linkage(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
cv = threading.Event()
lst = []
def add_on():
lst.append(True)
def wait_for_val():
async.get()
cv.set()
th = threading.Thread(target=wait_for_val)
th.start()
async.rawlink(add_on)
async.set('fred')
assert mock_handler.completion_queue.put.called
async.unlink(add_on)
cv.wait()
eq_(async.value, 'fred')
th.join()
def test_linkage_not_ready(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
lst = []
def add_on():
lst.append(True)
async.set('fred')
assert not mock_handler.completion_queue.called
async.rawlink(add_on)
assert mock_handler.completion_queue.put.called
def test_link_and_unlink(self):
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
lst = []
def add_on():
lst.append(True)
async.rawlink(add_on)
assert not mock_handler.completion_queue.put.called
async.unlink(add_on)
async.set('fred')
assert not mock_handler.completion_queue.put.called
def test_captured_exception(self):
from kazoo.handlers.utils import capture_exceptions
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
@capture_exceptions(async)
def exceptional_function():
return 1/0
exceptional_function()
assert_raises(ZeroDivisionError, async.get)
def test_no_capture_exceptions(self):
from kazoo.handlers.utils import capture_exceptions
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
lst = []
def add_on():
lst.append(True)
async.rawlink(add_on)
@capture_exceptions(async)
def regular_function():
return True
regular_function()
assert not mock_handler.completion_queue.put.called
def test_wraps(self):
from kazoo.handlers.utils import wrap
mock_handler = mock.Mock()
async = self._makeOne(mock_handler)
lst = []
def add_on(result):
lst.append(result.get())
async.rawlink(add_on)
@wrap(async)
def regular_function():
return 'hello'
assert regular_function() == 'hello'
assert mock_handler.completion_queue.put.called
assert async.get() == 'hello'
|