/usr/lib/python3/dist-packages/ginga/misc/tests/test_Future.py is in python3-ginga 2.6.1-2.
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 327 328 329 330 | #
# Unit Tests for the Future class
#
# Rajul Srivastava (rajul09@gmail.com)
#
import unittest
import logging
import threading
import time
import sys
from ginga.misc.Future import Future, TimeoutError
class TestError(Exception):
pass
class TestFuture(unittest.TestCase):
def setUp(self):
self.future_thread = None
def test_init(self):
test_future = Future()
assert hasattr(test_future, 'cb')
if sys.version_info.major == 2:
assert isinstance(test_future.evt, threading._Event)
elif sys.version_info.major == 3:
assert isinstance(test_future.evt, threading.Event)
assert test_future.evt.isSet() == False
assert test_future.res == None
assert test_future.data == None
assert 'resolved' in test_future.cb
expected = []
actual = test_future.cb['resolved']
assert expected == actual
def test_init_with_data(self):
test_future = Future("TestData")
assert hasattr(test_future, 'cb')
if sys.version_info.major == 2:
assert isinstance(test_future.evt, threading._Event)
elif sys.version_info.major == 3:
assert isinstance(test_future.evt, threading.Event)
assert test_future.evt.isSet() == False
assert test_future.res == None
assert test_future.data == "TestData"
assert 'resolved' in test_future.cb
expected = []
actual = test_future.cb['resolved']
assert expected == actual
def test_get_data_no_data(self):
test_future = Future()
expected = None
actual = test_future.get_data()
assert expected == actual
def test_get_data_some_data(self):
test_future = Future("TestData")
expected = "TestData"
actual = test_future.get_data()
assert expected == actual
def test_freeze(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
pass
test_future.freeze(
test_method, "arg1", "arg2", kwarg1="test", kwarg2="test")
assert test_future.method == test_method
assert test_future.args == ("arg1", "arg2")
assert test_future.kwdargs == {"kwarg1": "test", "kwarg2": "test"}
def test_freeze_empty_args(self):
test_future = Future("TestData")
def test_method():
pass
test_future.freeze(test_method)
assert test_future.method == test_method
assert test_future.args == ()
assert test_future.kwdargs == {}
def test_thaw_suppress_exception_no_exception(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
return True
test_future.freeze(test_method)
expected = True
actual = test_future.thaw()
assert expected == actual
assert test_future.res == True
assert test_future.evt.isSet() == True
def test_thaw_suppress_exception_exception(self):
test_future = Future("TestData")
def test_method():
return True
test_future.freeze(
test_method, "arg1", "arg2", kwarg1="test", kwarg2="test")
test_result = test_future.thaw()
assert isinstance(test_result, TypeError)
assert isinstance(test_future.res, TypeError)
assert test_future.evt.isSet() == True
def test_thaw_not_suppress_exception_no_exception(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
return True
test_future.freeze(test_method)
expected = True
actual = test_future.thaw(False)
assert expected == actual
assert test_future.res == True
assert test_future.evt.isSet() == True
def test_thaw_not_suppress_exception_raise_exception(self):
test_future = Future("TestData")
def test_method():
return True
test_future.freeze(
test_method, "arg1", "arg2", kwarg1="test", kwarg2="test")
self.assertRaises(TypeError, test_future.thaw, False)
assert test_future.res == None
assert test_future.evt.isSet() == False
def test_has_value_unset(self):
test_future = Future("TestData")
expected = False
actual = test_future.has_value()
assert expected == actual
def test_has_value_set(self):
test_future = Future("TestData")
test_future.evt.set()
expected = True
actual = test_future.has_value()
assert expected == actual
def test_resolve(self):
test_future = Future("TestData")
test_future.resolve(True)
assert test_future.res == True
assert test_future.evt.isSet() == True
def test_resolve_callback(self):
test_future = Future("TestData")
def test_callback(obj):
try:
obj.res = not obj.res
except:
pass
test_future.add_callback('resolved', test_callback)
test_future.resolve(True)
# Callback reverses the boolean 'res' value
assert test_future.res == False
assert test_future.evt.isSet() == True
def test_wait(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
time.sleep(2)
return True
test_future.freeze(test_method)
self.future_thread = threading.Thread(target=test_future.thaw)
self.future_thread.start()
expected = True
actual = test_future.wait()
assert expected == actual
def test_wait_timeout(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
time.sleep(2)
return True
test_future.freeze(test_method)
self.future_thread = threading.Thread(target=test_future.thaw)
self.future_thread.start()
self.assertRaises(TimeoutError, test_future.wait, 1)
def test_get_value_block_no_timeout(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
time.sleep(2)
return True
test_future.freeze(test_method)
self.future_thread = threading.Thread(target=test_future.thaw)
self.future_thread.start()
expected = True
actual = test_future.get_value()
assert expected == actual
def test_get_value_block_timeout(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
time.sleep(2)
return True
test_future.freeze(test_method)
self.future_thread = threading.Thread(target=test_future.thaw)
self.future_thread.start()
self.assertRaises(
TimeoutError, test_future.get_value, True, 1)
def test_get_value_no_block_fail(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
time.sleep(2)
return True
test_future.freeze(test_method)
self.future_thread = threading.Thread(target=test_future.thaw)
self.future_thread.start()
self.assertRaises(TimeoutError, test_future.get_value, False)
def test_get_value_no_block_pass(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
return True
test_future.freeze(test_method)
self.future_thread = threading.Thread(target=test_future.thaw)
self.future_thread.start()
# Making the test running thread to sleep for a while
# for the self.future_thread to complete and ensure success
time.sleep(0.5)
expected = True
actual = test_future.get_value()
assert expected == actual
def test_get_value_suppress_exception(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
raise TestError("Test Error")
test_future.freeze(test_method)
self.future_thread = threading.Thread(target=test_future.thaw)
self.future_thread.start()
future_value = test_future.get_value(True, None, True)
assert isinstance(future_value, Exception)
def test_get_value_no_suppress_exception(self):
test_future = Future("TestData")
def test_method(*args, **kwargs):
raise TestError("Test Error")
test_future.freeze(test_method)
self.future_thread = threading.Thread(target=test_future.thaw)
self.future_thread.start()
self.assertRaises(TestError, test_future.get_value)
def tearDown(self):
if self.future_thread != None:
self.future_thread.join()
if __name__ == '__main__':
unittest.main()
# END
|