def test_to_task(self): """Ensure to_task produces the right task object.""" import datetime import time from google.appengine.ext import testbed from furious. async import Async from furious. async import ASYNC_ENDPOINT testbed = testbed.Testbed() testbed.activate() # This just drops the microseconds. It is a total mess, but is needed # to handle all the rounding crap. eta = datetime.datetime.now() + datetime.timedelta(minutes=43) eta_posix = time.mktime(eta.timetuple()) headers = {'some': 'thing', 'fun': 1} job = ('test', None, None) expected_url = "%s/%s" % (ASYNC_ENDPOINT, 'test') task_args = {'eta': eta_posix} options = { 'job': job, 'headers': headers, 'task_args': task_args, 'id': 'ident', 'context_id': 'contextid', 'parent_id': 'parentid' } task = Async.from_dict(options).to_task() # App Engine sets these headers by default. full_headers = { 'Host': 'testbed.example.com', 'X-AppEngine-Current-Namespace': '' } full_headers.update(headers) self.assertEqual(eta_posix, task.eta_posix) self.assertEqual(expected_url, task.url) self.assertEqual(full_headers, task.headers) options['task_args']['eta'] = datetime.datetime.fromtimestamp( eta_posix) options['_recursion'] = {'current': 1, 'max': 100} options['_type'] = 'furious.async.Async' self.assertEqual( options, Async.from_dict(json.loads(task.payload)).get_options())
def test_to_task(self): """Ensure to_task produces the right task object.""" import datetime import time from google.appengine.ext import testbed from furious.async import Async from furious.async import ASYNC_ENDPOINT testbed = testbed.Testbed() testbed.activate() # This just drops the microseconds. It is a total mess, but is needed # to handle all the rounding crap. eta = datetime.datetime.now() + datetime.timedelta(minutes=43) eta_posix = time.mktime(eta.timetuple()) headers = {'some': 'thing', 'fun': 1} job = ('test', None, None) expected_url = "%s/%s" % (ASYNC_ENDPOINT, 'test') task_args = {'eta': eta_posix} options = {'job': job, 'headers': headers, 'task_args': task_args, 'id': 'ident', 'context_id': 'contextid', 'parent_id': 'parentid'} task = Async.from_dict(options).to_task() # App Engine sets these headers by default. full_headers = { 'Host': 'testbed.example.com', 'X-AppEngine-Current-Namespace': '' } full_headers.update(headers) self.assertEqual(eta_posix, task.eta_posix) self.assertEqual(expected_url, task.url) self.assertEqual(full_headers, task.headers) options['task_args']['eta'] = datetime.datetime.fromtimestamp( eta_posix) options['_recursion'] = {'current': 1, 'max': 100} options['_type'] = 'furious.async.Async' self.assertEqual( options, Async.from_dict(json.loads(task.payload)).get_options())
def test_to_task(self): """Ensure to_task produces the right task object.""" import datetime import time from google.appengine.ext import testbed from furious.async import Async from furious.async import ASYNC_ENDPOINT testbed = testbed.Testbed() testbed.activate() # This just drops the microseconds. It is a total mess, but is needed # to handle all the rounding crap. eta = datetime.datetime.now() + datetime.timedelta(minutes=43) eta_posix = time.mktime(eta.timetuple()) headers = {"some": "thing", "fun": 1} job = ("test", None, None) expected_url = "%s/%s" % (ASYNC_ENDPOINT, "test") task_args = {"eta": eta_posix} options = { "job": job, "headers": headers, "task_args": task_args, "id": "ident", "context_id": "contextid", "parent_id": "parentid", } task = Async.from_dict(options).to_task() # App Engine sets these headers by default. full_headers = {"Host": "testbed.example.com", "X-AppEngine-Current-Namespace": ""} full_headers.update(headers) self.assertEqual(eta_posix, task.eta_posix) self.assertEqual(expected_url, task.url) self.assertEqual(full_headers, task.headers) options["task_args"]["eta"] = datetime.datetime.fromtimestamp(eta_posix) options["_recursion"] = {"current": 1, "max": 100} options["_type"] = "furious.async.Async" self.assertEqual(options, Async.from_dict(json.loads(task.payload)).get_options())
def test_reconstitution(self): """Ensure to_dict(job.from_dict()) returns the same thing.""" from furious. async import Async headers = {'some': 'thing', 'fun': 1} job = ('test', None, None) task_args = {'other': 'zzz', 'nested': 1} options = { 'job': job, 'id': 'someid', 'headers': headers, 'task_args': task_args, 'persistence_engine': 'furious.extras.appengine.ndb_persistence', '_recursion': { 'current': 1, 'max': 100 }, '_type': 'furious.async.Async', 'context_id': None, 'parent_id': 'parentid' } async_job = Async.from_dict(options) self.assertEqual(options, async_job.to_dict())
def test_from_dict_with_callbacks(self): """Ensure from_dict reconstructs callbacks correctly.""" from furious.async import Async job = ('test', None, None) callbacks = { 'success': ("furious.tests.test_async." "TestAsync.test_to_dict_with_callbacks"), 'failure': "dir", 'exec': {'job': ('dir', None, None), 'id': 'petey'} } options = {'job': job, 'callbacks': callbacks} async_job = Async.from_dict(options) check_callbacks = { 'success': TestAsync.test_to_dict_with_callbacks, 'failure': dir } callbacks = async_job.get_callbacks() exec_callback = callbacks.pop('exec') correct_options = {'job': ('dir', None, None), 'id': 'petey', 'context_id': None, '_recursion': {'current': 0, 'max': 100}, '_type': 'furious.async.Async'} self.assertEqual(check_callbacks, callbacks) self.assertEqual(correct_options, exec_callback.to_dict())
def test_from_dict_with_callbacks(self): """Ensure from_dict reconstructs callbacks correctly.""" from furious.async import Async job = ('test', None, None) callbacks = { 'success': ("furious.tests.test_async." "TestAsync.test_to_dict_with_callbacks"), 'failure': "dir", 'exec': {'job': ('dir', None, None)} } options = {'job': job, 'callbacks': callbacks} async_job = Async.from_dict(options) check_callbacks = { 'success': TestAsync.test_to_dict_with_callbacks, 'failure': dir } callbacks = async_job.get_callbacks() exec_callback = callbacks.pop('exec') self.assertEqual(check_callbacks, callbacks) self.assertEqual({'job': ('dir', None, None)}, exec_callback.to_dict())
def test_from_dict_with_callbacks(self): """Ensure from_dict reconstructs callbacks correctly.""" from furious.async import Async job = ("test", None, None) callbacks = { "success": ("furious.tests.test_async." "TestAsync.test_to_dict_with_callbacks"), "failure": "dir", "exec": {"job": ("dir", None, None), "id": "petey", "parent_id": "parentid"}, } options = {"job": job, "callbacks": callbacks, "parent_id": "parentid"} async_job = Async.from_dict(options) check_callbacks = {"success": TestAsync.test_to_dict_with_callbacks, "failure": dir} callbacks = async_job.get_callbacks() exec_callback = callbacks.pop("exec") correct_options = { "job": ("dir", None, None), "id": "petey", "parent_id": "parentid", "context_id": None, "_recursion": {"current": 0, "max": 100}, "_type": "furious.async.Async", } self.assertEqual(check_callbacks, callbacks) self.assertEqual(correct_options, exec_callback.to_dict())
def test_retry_value_is_decodable(self): """Ensure that from_dict is the inverse of to_dict when retry options are given. """ from furious.async import Async async_job = Async("something", task_args={"retry_options": {"task_retry_limit": 5}}) new_async_job = Async.from_dict(async_job.to_dict()) self.assertEqual(async_job.to_dict(), new_async_job.to_dict())
def test_reconstitution(self): """Ensure to_dict(job.from_dict()) returns the same thing.""" from furious.async import Async headers = {'some': 'thing', 'fun': 1} job = ('test', None, None) task_args = {'other': 'zzz', 'nested': 1} options = {'job': job, 'headers': headers, 'task_args': task_args} async_job = Async.from_dict(options) self.assertEqual(options, async_job.to_dict())
def test_retry_value_is_decodable(self): """Ensure that from_dict is the inverse of to_dict when retry options are given. """ from furious. async import Async async_job = Async("something", task_args={'retry_options': { 'task_retry_limit': 5 }}) new_async_job = Async.from_dict(async_job.to_dict()) self.assertEqual(async_job.to_dict(), new_async_job.to_dict())
def test_used_async_retry_value_is_decodable(self): """Ensure that from_dict is the inverse of to_dict when retry options are given and the async has be cast to task. """ from furious.async import Async async_job = Async("something", task_args={'retry_options': {'task_retry_limit': 5}}) async_job.to_dict() new_async_job = Async.from_dict(async_job.to_dict()) self.assertEqual(async_job.to_dict(), new_async_job.to_dict())
def decode_callbacks(encoded_callbacks): """Decode the callbacks to an executable form.""" from furious.async import Async callbacks = {} for event, callback in encoded_callbacks.iteritems(): if isinstance(callback, dict): callback = Async.from_dict(callback) else: callback = path_to_reference(callback) callbacks[event] = callback return callbacks
def decode_callbacks(encoded_callbacks): """Decode the callbacks to an executable form.""" from furious. async import Async callbacks = {} for event, callback in encoded_callbacks.iteritems(): if isinstance(callback, dict): callback = Async.from_dict(callback) else: callback = path_to_reference(callback) callbacks[event] = callback return callbacks
def test_process_results_encoded_and_decoded(self): """Ensure _process_results is correctly encoded to and decoded from an Async options dict. """ from furious.async import Async async_job = Async("something", _process_results=locals) encoded_async = async_job.to_dict() self.assertEqual(encoded_async["__process_results"], "locals") new_async_job = Async.from_dict(encoded_async) self.assertEqual(new_async_job.get_options()["_process_results"], locals) self.assertEqual(async_job.to_dict(), new_async_job.to_dict())
def test_context_checker_encoded_and_decoded(self): """Ensure the _context_checker is correctly encoded to and decoded from an Async options dict. """ from furious.async import Async async_job = Async("something", _context_checker=dir) encoded_async = async_job.to_dict() self.assertEqual(encoded_async["__context_checker"], "dir") new_async_job = Async.from_dict(encoded_async) self.assertEqual(new_async_job.get_options()["_context_checker"], dir) self.assertEqual(async_job.to_dict(), new_async_job.to_dict())
def test_from_dict(self): """Ensure from_dict returns the correct Async object.""" from furious. async import Async headers = {'some': 'thing', 'fun': 1} job = ('test', None, None) task_args = {'other': 'zzz', 'nested': 1} options = {'job': job, 'headers': headers, 'task_args': task_args} async_job = Async.from_dict(options) self.assertEqual(headers, async_job.get_headers()) self.assertEqual(task_args, async_job.get_task_args()) self.assertEqual(job[0], async_job._function_path)
def test_context_checker_encoded_and_decoded(self): """Ensure the _context_checker is correctly encoded to and decoded from an Async options dict. """ from furious. async import Async async_job = Async("something", _context_checker=dir) encoded_async = async_job.to_dict() self.assertEqual(encoded_async['__context_checker'], 'dir') new_async_job = Async.from_dict(encoded_async) self.assertEqual(new_async_job.get_options()['_context_checker'], dir) self.assertEqual(async_job.to_dict(), new_async_job.to_dict())
def test_from_dict(self): """Ensure from_dict returns the correct Async object.""" from furious.async import Async headers = {'some': 'thing', 'fun': 1} job = ('test', None, None) task_args = {'other': 'zzz', 'nested': 1} options = {'job': job, 'headers': headers, 'task_args': task_args} async_job = Async.from_dict(options) self.assertEqual(headers, async_job.get_headers()) self.assertEqual(task_args, async_job.get_task_args()) self.assertEqual(job[0], async_job._function_path)
def test_from_dict(self): """Ensure from_dict returns the correct Async object.""" from furious.async import Async headers = {"some": "thing", "fun": 1} job = ("test", None, None) task_args = {"other": "zzz", "nested": 1} options = {"job": job, "headers": headers, "task_args": task_args} async_job = Async.from_dict(options) self.assertEqual(headers, async_job.get_headers()) self.assertEqual(task_args, async_job.get_task_args()) self.assertEqual(job[0], async_job._function_path) self.assertEqual(job[0], async_job.function_path)
def test_process_results_encoded_and_decoded(self): """Ensure _process_results is correctly encoded to and decoded from an Async options dict. """ from furious. async import Async async_job = Async("something", _process_results=locals) encoded_async = async_job.to_dict() self.assertEqual(encoded_async['__process_results'], 'locals') new_async_job = Async.from_dict(encoded_async) self.assertEqual(new_async_job.get_options()['_process_results'], locals) self.assertEqual(async_job.to_dict(), new_async_job.to_dict())
def test_reconstitution(self): """Ensure to_dict(job.from_dict()) returns the same thing.""" from furious.async import Async headers = {'some': 'thing', 'fun': 1} job = ('test', None, None) task_args = {'other': 'zzz', 'nested': 1} options = { 'job': job, 'headers': headers, 'task_args': task_args, 'persistence_engine': 'furious.extras.appengine.ndb_persistence', '_recursion': {'current': 1, 'max': 100}, '_type': 'furious.async.Async', } async_job = Async.from_dict(options) self.assertEqual(options, async_job.to_dict())
def test_from_dict_with_callbacks(self): """Ensure from_dict reconstructs callbacks correctly.""" from furious. async import Async job = ('test', None, None) callbacks = { 'success': ("furious.tests.test_async." "TestAsync.test_to_dict_with_callbacks"), 'failure': "dir", 'exec': { 'job': ('dir', None, None), 'id': 'petey', 'parent_id': 'parentid' } } options = {'job': job, 'callbacks': callbacks, 'parent_id': 'parentid'} async_job = Async.from_dict(options) check_callbacks = { 'success': TestAsync.test_to_dict_with_callbacks, 'failure': dir } callbacks = async_job.get_callbacks() exec_callback = callbacks.pop('exec') correct_options = { 'job': ('dir', None, None), 'id': 'petey', 'parent_id': 'parentid', 'context_id': None, '_recursion': { 'current': 0, 'max': 100 }, '_type': 'furious.async.Async' } self.assertEqual(check_callbacks, callbacks) self.assertEqual(correct_options, exec_callback.to_dict())
def test_reconstitution(self): """Ensure to_dict(job.from_dict()) returns the same thing.""" from furious.async import Async headers = {"some": "thing", "fun": 1} job = ("test", None, None) task_args = {"other": "zzz", "nested": 1} options = { "job": job, "id": "someid", "headers": headers, "task_args": task_args, "persistence_engine": "furious.extras.appengine.ndb_persistence", "_recursion": {"current": 1, "max": 100}, "_type": "furious.async.Async", "context_id": None, "parent_id": "parentid", } async_job = Async.from_dict(options) self.assertEqual(options, async_job.to_dict())