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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
import itertools
import queue
import time
import unittest
from unittest.mock import patch, Mock
from mpire.async_result import (AsyncResult, AsyncResultWithExceptionGetter, UnorderedAsyncExitResultIterator,
UnorderedAsyncResultIterator)
from mpire.comms import INIT_FUNC, EXIT_FUNC, MAIN_PROCESS
class AsyncResultTest(unittest.TestCase):
def test_init(self):
"""
Test that the job_id is set correctly and that the cache is updated
"""
with self.subTest("job_counter=0", job_id=None), \
patch('mpire.async_result.job_counter', itertools.count(start=0)):
cache = {}
r = AsyncResult(cache, None, None, None, True, None)
self.assertEqual(r.job_id, 0)
self.assertEqual(cache, {0: r})
with self.subTest("job_counter=10", job_id=None), \
patch('mpire.async_result.job_counter', itertools.count(start=10)):
cache = {}
r = AsyncResult(cache, None, None, None, True, None)
self.assertEqual(r.job_id, 10)
self.assertEqual(cache, {10: r})
with self.subTest("job_counter=0", job_id=42), \
patch('mpire.async_result.job_counter', itertools.count(start=0)):
cache = {}
r = AsyncResult(cache, None, None, 42, True, None)
self.assertEqual(r.job_id, 42)
self.assertEqual(cache, {42: r})
with self.subTest("job_id already exists in cache"), \
patch('mpire.async_result.job_counter', itertools.count(start=0)):
with self.assertRaises(ValueError):
AsyncResult({42: None}, None, None, 42, True, None)
with self.subTest(timeout=None):
r = AsyncResult({}, None, None, None, True, None)
self.assertIsNone(r._timeout)
with self.subTest(timeout=10):
r = AsyncResult({}, None, None, None, True, 10)
self.assertEqual(r._timeout, 10)
def test_ready(self):
"""
Test that the ready method returns the correct value
"""
r = AsyncResult({}, None, None, None, True, None)
self.assertFalse(r.ready())
r._ready_event.set()
self.assertTrue(r.ready())
def test_successful(self):
"""
Test that the successful method returns the correct value
"""
r = AsyncResult({}, None, None, None, True, None)
# Test that the method raises a ValueError if the task is not finished yet
with self.assertRaises(ValueError):
r.successful()
r._success = True
r._ready_event.set()
self.assertTrue(r.successful())
r._success = False
self.assertFalse(r.successful())
def test_get_value(self):
"""
Test that the get method returns the correct value
"""
r = AsyncResult({}, None, None, None, True, None)
r._value = 42
r._success = True
r._ready_event.set()
self.assertEqual(r.get(), 42)
r._value = 1337
self.assertEqual(r.get(), 1337)
def test_get_timeout(self):
"""
Test that the get method raises a TimeoutError if the timeout is exceeded
"""
r = AsyncResult({}, None, None, None, True, None)
start_t = time.time()
with self.assertRaises(TimeoutError):
r.get(timeout=0.001)
self.assertGreaterEqual(time.time() - start_t, 0.001)
def test_get_error(self):
"""
Test that the get method raises the error if the task has failed
"""
r = AsyncResult({}, None, None, None, True, None)
r._value = ValueError('test')
r._success = False
r._ready_event.set()
with self.assertRaises(ValueError):
r.get()
def test_set_success(self):
"""
Test that the _set method sets the correct values if the task has succeeded
"""
r = AsyncResult({}, None, None, None, True, None)
r._set(True, 42)
self.assertTrue(r._success)
self.assertEqual(r._value, 42)
self.assertTrue(r._ready_event.is_set())
def test_set_exception(self):
"""
Test that the _set method sets the correct values if the task has failed
"""
value_error = ValueError('test')
r = AsyncResult({}, None, None, None, True, None)
r._set(False, value_error)
self.assertFalse(r._success)
self.assertEqual(r._value, value_error)
self.assertTrue(r._ready_event.is_set())
def test_set_delete_from_cache(self):
"""
Test that the _set method deletes the result from the cache if the task has finished
"""
with self.subTest("delete"):
cache = {}
r = AsyncResult(cache, None, None, None, True, None)
r._set(True, 42)
self.assertNotIn(r.job_id, cache)
with self.subTest("no_delete"):
cache = {}
r = AsyncResult(cache, None, None, None, False, None)
r._set(True, 42)
self.assertIn(r.job_id, cache)
def test_callback_success(self):
"""
Test that the callback is called if the task has succeeded
"""
callback = Mock()
r = AsyncResult({}, callback, None, None, True, None)
r._set(True, 42)
callback.assert_called_once_with(42)
def test_callback_error(self):
"""
Test that the callback is called if the task has failed
"""
callback = Mock()
value_error = ValueError('test')
r = AsyncResult({}, None, callback, None, True, None)
r._set(False, value_error)
callback.assert_called_once_with(value_error)
class UnorderedAsyncResultIteratorTest(unittest.TestCase):
def test_init(self):
"""
Test that the job_id is set correctly and that the cache is updated
"""
with self.subTest("job_counter=0", job_id=None), \
patch('mpire.async_result.job_counter', itertools.count(start=0)):
cache = {}
r = UnorderedAsyncResultIterator(cache, None, None, None)
self.assertEqual(r.job_id, 0)
self.assertEqual(cache, {0: r})
with self.subTest("job_counter=10", job_id=None), \
patch('mpire.async_result.job_counter', itertools.count(start=10)):
cache = {}
r = UnorderedAsyncResultIterator(cache, None, None, None)
self.assertEqual(r.job_id, 10)
self.assertEqual(cache, {10: r})
with self.subTest("job_counter=0", job_id=42), \
patch('mpire.async_result.job_counter', itertools.count(start=0)):
cache = {}
r = UnorderedAsyncResultIterator(cache, None, 42, None)
self.assertEqual(r.job_id, 42)
self.assertEqual(cache, {42: r})
with self.subTest("job_id already exists in cache"), \
patch('mpire.async_result.job_counter', itertools.count(start=0)):
with self.assertRaises(ValueError):
UnorderedAsyncResultIterator({42: None}, None, 42, None)
with self.subTest(timeout=None):
r = UnorderedAsyncResultIterator({}, None, None, None)
self.assertIsNone(r._timeout)
with self.subTest(timeout=0.001):
r = UnorderedAsyncResultIterator({}, None, None, 0.001)
self.assertEqual(r._timeout, 0.001)
def test_iter(self):
"""
Test that the iterator is returned
"""
r = UnorderedAsyncResultIterator({}, None, None, None)
self.assertEqual(r, iter(r))
def test_next(self):
"""
Test that the next method returns the correct value
"""
r = UnorderedAsyncResultIterator({}, None, None, None)
r._set(True, 42)
r._set(True, 1337)
self.assertEqual(next(r), 42)
self.assertEqual(r._n_returned, 1)
self.assertEqual(next(r), 1337)
self.assertEqual(r._n_returned, 2)
def test_next_timeout(self):
"""
Test that the next method raises a queue.Empty if the timeout is exceeded
"""
r = UnorderedAsyncResultIterator({}, None, None, None)
start_t = time.time()
with self.assertRaises(queue.Empty):
r.next(block=True, timeout=0.001)
self.assertGreaterEqual(time.time() - start_t, 0.001)
with self.assertRaises(queue.Empty):
r.next(block=False)
def test_next_all_returned(self):
"""
Test that the next method raises a StopIteration if all values have been returned
"""
r = UnorderedAsyncResultIterator({}, 2, None, None)
r._set(True, 42)
r._set(True, 1337)
next(r)
next(r)
with self.assertRaises(StopIteration):
next(r)
def test_set_success(self):
"""
Test that the _set method sets the correct values if the task has succeeded
"""
r = UnorderedAsyncResultIterator({}, None, None, None)
r._set(True, 42)
self.assertIsNone(r._exception)
self.assertFalse(r._got_exception.is_set())
self.assertEqual(list(r._items), [42])
r._set(True, 1337)
r._set(True, 0)
self.assertEqual(list(r._items), [42, 1337, 0])
def test_set_exception(self):
"""
Test that the _set method sets the exception if the task has failed
"""
r = UnorderedAsyncResultIterator({}, None, None, None)
value_error = ValueError('test')
r._set(False, value_error)
self.assertEqual(r._exception, value_error)
self.assertTrue(r._got_exception.is_set())
self.assertEqual(list(r._items), [])
def test_set_length(self):
"""
Test that the _set method sets the correct length if it is given
"""
r = UnorderedAsyncResultIterator({}, None, None, None)
self.assertIsNone(r._n_tasks)
r.set_length(2)
self.assertEqual(r._n_tasks, 2)
def test_set_length_already_set(self):
"""
Test that the _set method raises an ValueError if the length is already set. Setting the length to the same
value should not raise an error.
"""
r = UnorderedAsyncResultIterator({}, 2, None, None)
r.set_length(2)
with self.assertRaises(ValueError):
r.set_length(1)
def test_get_exception(self):
"""
Test that the get_exception method returns the correct exception
"""
r = UnorderedAsyncResultIterator({}, None, None, None)
value_error = ValueError('test')
r._set(False, value_error)
self.assertEqual(r.get_exception(), value_error)
def test_remove_from_cache(self):
"""
Test that the remove_from_cache method removes the result from the cache
"""
cache = {}
r = UnorderedAsyncResultIterator(cache, None, None, None)
self.assertIn(r.job_id, cache)
r.remove_from_cache()
self.assertNotIn(r.job_id, cache)
class AsyncResultWithExceptionGetterTest(unittest.TestCase):
def test_init(self):
"""
Test that the result is initialized correctly
"""
r = AsyncResultWithExceptionGetter({}, INIT_FUNC)
self.assertEqual(r.job_id, INIT_FUNC)
self.assertFalse(r._delete_from_cache)
r = AsyncResultWithExceptionGetter({}, MAIN_PROCESS)
self.assertEqual(r.job_id, MAIN_PROCESS)
self.assertFalse(r._delete_from_cache)
def test_get_exception(self):
"""
Test that the get_exception method returns the correct exception
"""
r = AsyncResultWithExceptionGetter({}, INIT_FUNC)
value_error = ValueError('test')
r._set(False, value_error)
self.assertEqual(r.get_exception(), value_error)
def test_reset(self):
"""
Test that the reset method resets the result
"""
value_error = ValueError('test')
r = AsyncResultWithExceptionGetter({}, INIT_FUNC)
r._set(False, value_error)
self.assertFalse(r._success)
self.assertEqual(r._value, value_error)
self.assertTrue(r._ready_event.is_set())
r.reset()
self.assertIsNone(r._success)
self.assertIsNone(r._value)
self.assertFalse(r._ready_event.is_set())
class UnorderedAsyncExitResultIteratorTest(unittest.TestCase):
def test_init(self):
"""
Test that the result is initialized correctly
"""
r = UnorderedAsyncExitResultIterator({})
self.assertEqual(r.job_id, EXIT_FUNC)
self.assertIsNone(r._n_tasks)
def test_get_results(self):
"""
Test that the get_results method returns the correct results
"""
value_error = ValueError('test')
r = UnorderedAsyncExitResultIterator({})
r._set(True, 42)
r._set(True, 1337)
r._set(False, value_error)
self.assertEqual(r.get_results(), [42, 1337])
def test_reset(self):
"""
Test that the reset method resets the result
"""
r = UnorderedAsyncExitResultIterator({})
r._set(True, 42)
r._set(False, ValueError('test'))
r.set_length(2)
next(r)
self.assertEqual(r._n_tasks, 2)
self.assertEqual(len(r._items), 0) # Not 2, because 1 has alraedy been returned and 1 is an exception
self.assertEqual(r._n_received, 1)
self.assertEqual(r._n_returned, 1)
self.assertIsNotNone(r._exception)
self.assertTrue(r._got_exception.is_set())
r.reset()
self.assertEqual(r._n_tasks, None)
self.assertEqual(len(r._items), 0)
self.assertEqual(r._n_received, 0)
self.assertEqual(r._n_returned, 0)
self.assertIsNone(r._exception)
self.assertFalse(r._got_exception.is_set())
|