def new(): """Get a new furious context and add it to the registry.""" new_context = Context() _local.get_local_context().registry.append(new_context) return new_context
def new(batch_size=None, **options): """Get a new furious context and add it to the registry. If a batch size is specified, use an AutoContext which inserts tasks in batches as they are added to the context. """ if batch_size: new_context = AutoContext(batch_size=batch_size, **options) else: new_context = Context(**options) _local.get_local_context().registry.append(new_context) return new_context
def test_job_removed_from_end_of_local_context(self): """Ensure entering the context removes the job from the end of the job context stack. """ from furious.async import Async from furious.context._execution import _ExecutionContext from furious.context._local import get_local_context job_outer = Async(target=dir) job_inner = Async(target=dir) with _ExecutionContext(job_outer): with _ExecutionContext(job_inner): pass self.assertEqual(1, len(get_local_context()._executing_async)) self.assertEqual(job_outer, get_local_context()._executing_async[-1])
def test_context_set_on_local_context(self): """Ensure the context is set on the local_context.""" from furious.async import Async from furious.context._local import get_local_context from furious.context._execution import execution_context_from_async context = execution_context_from_async(Async(target=dir)) self.assertIs(context, get_local_context()._executing_async_context)
def test_context_set_on_local_context(self): """Ensure the context is set on the local_context.""" from furious. async import Async from furious.context._local import get_local_context from furious.context._execution import execution_context_from_async context = execution_context_from_async(Async(target=dir)) self.assertIs(context, get_local_context()._executing_async_context)
def test_job_removed_from_end_of_local_context(self): """Ensure entering the context removes the job from the end of the job context stack. """ from furious. async import Async from furious.context._execution import _ExecutionContext from furious.context._local import get_local_context job_outer = Async(target=dir) job_inner = Async(target=dir) with _ExecutionContext(job_outer): with _ExecutionContext(job_inner): pass self.assertEqual(1, len(get_local_context()._executing_async)) self.assertEqual(job_outer, get_local_context()._executing_async[-1])
def setUp(self): import os import uuid from furious.context import _local os.environ['REQUEST_ID_HASH'] = uuid.uuid4().hex local_context = _local.get_local_context() local_context._executing_async_context = None
def get_current_context(): """Return a reference to the current Context object. """ local_context = _local.get_local_context() if local_context.registry: return local_context.registry[-1] raise errors.NotInContextError('Not in a Context.')
def test_job_added_to_local_context(self): """Ensure entering the context adds the job to the context stack.""" from furious.async import Async from furious.context._execution import _ExecutionContext from furious.context._local import get_local_context job = Async(target=dir) with _ExecutionContext(job): self.assertIn(job, get_local_context()._executing_async)
def test_job_added_to_local_context(self): """Ensure entering the context adds the job to the context stack.""" from furious. async import Async from furious.context._execution import _ExecutionContext from furious.context._local import get_local_context job = Async(target=dir) with _ExecutionContext(job): self.assertIn(job, get_local_context()._executing_async)
def setUp(self): import os import uuid from furious.context import _local os.environ["REQUEST_ID_HASH"] = uuid.uuid4().hex local_context = _local.get_local_context() local_context._executing_async_context = None
def get_current_async(): """Return a reference to the currently executing Async job object or None if not in an Async job. """ local_context = _local.get_local_context() if local_context._executing_async: return local_context._executing_async[-1] raise errors.NotInContextError('Not in an _ExecutionContext.')
def test_new_adds_to_registry(self): """Ensure new adds new contexts to the context registry.""" from furious.context import Context from furious.context._local import get_local_context from furious.context import new ctx = new() self.assertIsInstance(ctx, Context) self.assertIn(ctx, get_local_context().registry)
def __exit__(self, *exc_info): """Exit the context, pop this async from the executing context stack. """ local_context = _local.get_local_context() last = local_context._executing_async.pop() if last is not self._async: local_context._executing_async.append(last) raise errors.CorruptContextError(*exc_info) return False
def _get_async_details(self): """Pull the async from the local context and return the id and function path in a dict if it exists. """ from furious.context._local import get_local_context context = get_local_context() if not context or not context._executing_async_context: return {} async = context._executing_async_context.async
def execution_context_from_async(async): """Instantiate a new _ExecutionContext and store a reference to it in the global async context to make later retrieval easier. """ local_context = _local.get_local_context() if local_context._executing_async_context: raise errors.ContextExistsError execution_context = _ExecutionContext(async) local_context._executing_async_context = execution_context return execution_context
def test_job_removed_from_local_context(self): """Ensure exiting the context removes the job from the context stack. """ from furious. async import Async from furious.context._execution import _ExecutionContext from furious.context._local import get_local_context job = Async(target=dir) with _ExecutionContext(job): pass self.assertNotIn(job, get_local_context()._executing_async)
def _get_async_details(self): """Pull the async from the local context and return the id and function path in a dict if it exists. """ from furious.context._local import get_local_context context = get_local_context() if not context or not context._executing_async_context: return {} async = context._executing_async_context. async
def test_job_removed_from_local_context(self): """Ensure exiting the context removes the job from the context stack. """ from furious.async import Async from furious.context._execution import _ExecutionContext from furious.context._local import get_local_context job = Async(target=dir) with _ExecutionContext(job): pass self.assertNotIn(job, get_local_context()._executing_async)
def test_build_tree_from_context(self): from furious.context import _local from furious.context.context import Context from furious.marker_tree.marker import Marker from furious.marker_tree.graph_analysis import tree_graph_growth from furious.tests.marker_tree import dummy_success_callback context = Context() for arg in xrange(23): context.add(target=dummy_success_callback, args=[arg]) _local.get_local_context().registry.append(context) root_marker = Marker.make_marker_tree_for_context(context) root_marker.persist() # Plus one, because of the empty context. self.assertEqual(root_marker.count_nodes(), tree_graph_growth(23))
def test_clear_context(self): """Ensure clear_context successfully clears attributes set during initialization from the local context. """ from furious.context import _local from furious.context._local import _clear_context from furious.context._local import get_local_context # Initialize the local context by retrieving it. get_local_context() # Make sure there is something on the local context we need to clear. self.assertTrue(hasattr(_local._local_context, "registry")) _clear_context() # Make sure local context entries have been cleared self.assertFalse(hasattr(_local._local_context, "_executing_async")) self.assertFalse(hasattr(_local._local_context, "_executing_async_context")) self.assertFalse(hasattr(_local._local_context, "_initialized")) self.assertFalse(hasattr(_local._local_context, "registry"))
def test_count_nodes(self): """Ensure Marker.count_nodes """ from furious.context import _local from furious.context.context import Context from furious.marker_tree.marker import Marker from furious.tests.marker_tree import dummy_success_callback context = Context() context.add(target=dummy_success_callback, args=[1]) _local.get_local_context().registry.append(context) root_marker = Marker.make_marker_tree_for_context(context) root_marker.persist() # Plus one, because of the empty context. self.assertEqual(root_marker.count_nodes(), 2)
def test_corrupt_context(self): """Ensure wrong context is not popped from execution context stack.""" from furious. async import Async from furious.context._execution import _ExecutionContext from furious.context._local import get_local_context from furious.errors import CorruptContextError with self.assertRaises(CorruptContextError) as cm: job_outer = Async(target=dir) with _ExecutionContext(job_outer): local_context = get_local_context() local_context._executing_async.append(object()) self.assertEqual((None, None, None), cm.exception.exc_info)
def test_clear_context(self): """Ensure clear_context successfully clears attributes set during initialization from the local context. """ from furious.context import _local from furious.context._local import _clear_context from furious.context._local import get_local_context # Initialize the local context by retrieving it. get_local_context() # Make sure there is something on the local context we need to clear. self.assertTrue(hasattr(_local._local_context, 'registry')) _clear_context() # Make sure local context entries have been cleared self.assertFalse(hasattr(_local._local_context, '_executing_async')) self.assertFalse( hasattr(_local._local_context, '_executing_async_context')) self.assertFalse(hasattr(_local._local_context, '_initialized')) self.assertFalse(hasattr(_local._local_context, 'registry'))
def test_corrupt_context(self): """Ensure wrong context is not popped from execution context stack.""" from furious.async import Async from furious.context._execution import _ExecutionContext from furious.context._local import get_local_context from furious.errors import CorruptContextError with self.assertRaises(CorruptContextError) as cm: job_outer = Async(target=dir) with _ExecutionContext(job_outer): local_context = get_local_context() local_context._executing_async.append(object()) self.assertEqual((None, None, None), cm.exception.exc_info)
def __enter__(self): """Enter the context, add this async to the executing context stack.""" _local.get_local_context()._executing_async.append(self._async) return self