<s> import json <EOL> import unittest <EOL> from mock import Mock , patch <EOL> class MessageTestCase ( unittest . TestCase ) : <EOL> def test_id_is_passed_in_with_options ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> _id = "<STR_LIT:id>" <EOL> options = { '<STR_LIT:id>' : _id , '<STR_LIT:value>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : { <NUM_LIT:1> : <NUM_LIT:1> } } <EOL> message = Message ( id = _id , value = <NUM_LIT:1> , other = '<STR_LIT>' , nested = { <NUM_LIT:1> : <NUM_LIT:1> } ) <EOL> self . assertEqual ( options , message . _options ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_options_are_set ( self , get_uuid ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> _id = "<STR_LIT:id>" <EOL> get_uuid . return_value . hex = _id <EOL> options = { '<STR_LIT:id>' : _id , '<STR_LIT:value>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : { <NUM_LIT:1> : <NUM_LIT:1> } } <EOL> message = Message ( value = <NUM_LIT:1> , other = '<STR_LIT>' , nested = { <NUM_LIT:1> : <NUM_LIT:1> } ) <EOL> self . assertEqual ( options , message . _options ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_update_options ( self , get_uuid ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> _id = "<STR_LIT:id>" <EOL> get_uuid . return_value . hex = _id <EOL> options = { '<STR_LIT:id>' : _id , '<STR_LIT:value>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : { <NUM_LIT:1> : <NUM_LIT:1> } } <EOL> message = Message ( ) <EOL> message . update_options ( ** options . copy ( ) ) <EOL> self . assertEqual ( options , message . _options ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_update_options_supersede_init_opts ( self , get_uuid ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> _id = "<STR_LIT:id>" <EOL> get_uuid . return_value . hex = _id <EOL> options = { '<STR_LIT:id>' : _id , '<STR_LIT:value>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : { <NUM_LIT:1> : <NUM_LIT:1> } } <EOL> message = Message ( ** options ) <EOL> message . update_options ( value = <NUM_LIT> , other = '<STR_LIT>' ) <EOL> options [ '<STR_LIT:value>' ] = <NUM_LIT> <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . assertEqual ( options , message . _options ) <EOL> def test_get_options ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> options = { '<STR_LIT:value>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : { <NUM_LIT:1> : <NUM_LIT:1> } } <EOL> message = Message ( ) <EOL> message . _options = options <EOL> self . assertEqual ( options , message . get_options ( ) ) <EOL> def test_get_queue ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> queue = "<STR_LIT:test>" <EOL> message = Message ( queue = queue ) <EOL> self . assertEqual ( queue , message . get_queue ( ) ) <EOL> def test_get_default_queue ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> message = Message ( ) <EOL> self . assertEqual ( '<STR_LIT>' , message . get_queue ( ) ) <EOL> def test_get_task_args ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> task_args = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } <EOL> options = { '<STR_LIT>' : task_args } <EOL> message = Message ( ** options ) <EOL> self . assertEqual ( task_args , message . get_task_args ( ) ) <EOL> def test_get_empty_task_args ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> message = Message ( ) <EOL> self . assertEqual ( { } , message . get_task_args ( ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_to_dict ( self , get_uuid ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> _id = "<STR_LIT:id>" <EOL> get_uuid . return_value . hex = _id <EOL> task_args = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } <EOL> options = { '<STR_LIT:id>' : _id , '<STR_LIT>' : task_args } <EOL> message = Message ( ** options . copy ( ) ) <EOL> self . assertEqual ( options , message . to_dict ( ) ) <EOL> def test_from_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> task_args = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } <EOL> options = { '<STR_LIT:id>' : '<STR_LIT:id>' , '<STR_LIT>' : task_args } <EOL> message = Message . from_dict ( options ) <EOL> self . assertEqual ( task_args , message . get_task_args ( ) ) <EOL> self . assertEqual ( message . id , '<STR_LIT:id>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_reconstitution ( self , get_uuid ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> _id = "<STR_LIT:id>" <EOL> get_uuid . return_value . hex = _id <EOL> task_args = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } <EOL> options = { '<STR_LIT:id>' : _id , '<STR_LIT>' : task_args } <EOL> message = Message . from_dict ( options ) <EOL> self . assertEqual ( options , message . to_dict ( ) ) <EOL> def test_to_task_with_payload ( self ) : <EOL> """<STR_LIT>""" <EOL> import datetime <EOL> import time <EOL> from google . appengine . ext import testbed <EOL> from furious . batcher import Message <EOL> testbed = testbed . Testbed ( ) <EOL> testbed . activate ( ) <EOL> eta = datetime . datetime . now ( ) + datetime . timedelta ( minutes = <NUM_LIT> ) <EOL> eta_posix = time . mktime ( eta . timetuple ( ) ) <EOL> task_args = { '<STR_LIT>' : eta_posix , '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } <EOL> options = { '<STR_LIT>' : task_args } <EOL> task = Message . from_dict ( options ) . to_task ( ) <EOL> full_headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . assertEqual ( eta_posix , task . eta_posix ) <EOL> self . assertEqual ( full_headers , task . headers ) <EOL> self . assertEqual ( task_args [ '<STR_LIT>' ] , json . loads ( task . payload ) ) <EOL> def test_to_task_without_payload ( self ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import testbed <EOL> from furious . batcher import Message <EOL> testbed = testbed . Testbed ( ) <EOL> testbed . activate ( ) <EOL> options = { '<STR_LIT>' : { } } <EOL> task = Message . from_dict ( options ) . to_task ( ) <EOL> full_headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . assertEqual ( full_headers , task . headers ) <EOL> self . assertIsNone ( json . loads ( task . payload ) ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> def test_insert ( self , queue_mock ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import Message <EOL> message = Message ( queue = '<STR_LIT>' ) <EOL> message . insert ( ) <EOL> queue_mock . assert_called_once_with ( name = '<STR_LIT>' ) <EOL> self . assertTrue ( queue_mock . return_value . add . called ) <EOL> class MessageProcessorTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( MessageProcessorTestCase , self ) . setUp ( ) <EOL> import os <EOL> import uuid <EOL> os . environ [ '<STR_LIT>' ] = uuid . uuid4 ( ) . hex <EOL> def tearDown ( self ) : <EOL> super ( MessageProcessorTestCase , self ) . tearDown ( ) <EOL> import os <EOL> del os . environ [ '<STR_LIT>' ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_to_task_with_no_name_passed_in ( self , memcache , time ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageProcessor <EOL> memcache . get . return_value = '<STR_LIT>' <EOL> time . time . return_value = <NUM_LIT:100> <EOL> processor = MessageProcessor ( '<STR_LIT>' , queue = '<STR_LIT>' ) <EOL> task = processor . to_task ( ) <EOL> self . assertEqual ( task . name , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_to_task_with_frequency_passed_in ( self , memcache , time ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageProcessor <EOL> memcache . get . return_value = '<STR_LIT>' <EOL> time . time . return_value = <NUM_LIT:100> <EOL> processor = MessageProcessor ( '<STR_LIT>' , queue = '<STR_LIT>' , freq = <NUM_LIT:100> ) <EOL> task = processor . to_task ( ) <EOL> self . assertEqual ( task . name , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_to_task_with_name_passed_in ( self , memcache , time ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageProcessor <EOL> memcache . get . return_value = '<STR_LIT>' <EOL> time . time . return_value = <NUM_LIT:100> <EOL> processor = MessageProcessor ( '<STR_LIT>' , queue = '<STR_LIT>' , <EOL> task_args = { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> task = processor . to_task ( ) <EOL> self . assertEqual ( task . name , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_to_task_with_tag_passed_in ( self , memcache , time ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageProcessor <EOL> memcache . get . return_value = '<STR_LIT>' <EOL> time . time . return_value = <NUM_LIT:100> <EOL> processor = MessageProcessor ( '<STR_LIT>' , queue = '<STR_LIT>' , <EOL> tag = '<STR_LIT>' ) <EOL> task = processor . to_task ( ) <EOL> self . assertEqual ( task . name , '<STR_LIT>' ) <EOL> memcache . get . assert_called_once_with ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_to_task_with_tag_not_passed_in ( self , memcache , time ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageProcessor <EOL> memcache . get . return_value = '<STR_LIT>' <EOL> time . time . return_value = <NUM_LIT:100> <EOL> processor = MessageProcessor ( '<STR_LIT>' , queue = '<STR_LIT>' ) <EOL> task = processor . to_task ( ) <EOL> self . assertEqual ( task . name , '<STR_LIT>' ) <EOL> memcache . get . assert_called_once_with ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' , autospec = True ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_to_task_has_correct_arguments ( self , memcache , time , task , <EOL> task_retry ) : <EOL> """<STR_LIT>""" <EOL> from furious . async import MAX_RESTARTS <EOL> from furious . batcher import MessageProcessor <EOL> memcache . get . return_value = '<STR_LIT>' <EOL> time . time . return_value = <NUM_LIT:100> <EOL> task_retry_object = Mock ( ) <EOL> task_retry . return_value = task_retry_object <EOL> processor = MessageProcessor ( '<STR_LIT>' , queue = '<STR_LIT>' , <EOL> id = '<STR_LIT>' , parent_id = '<STR_LIT>' , <EOL> context_id = "<STR_LIT>" ) <EOL> processor . to_task ( ) <EOL> task_args = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:30> , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : task_retry_object , <EOL> '<STR_LIT>' : json . dumps ( processor . to_dict ( ) ) <EOL> } <EOL> task . assert_called_once_with ( ** task_args ) <EOL> task_retry . assert_called_once_with ( task_retry_limit = MAX_RESTARTS ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_curent_batch_key_exists_in_cache ( self , cache ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageProcessor <EOL> cache . get . return_value = <NUM_LIT:1> <EOL> processor = MessageProcessor ( '<STR_LIT>' ) <EOL> current_batch = processor . current_batch <EOL> cache . get . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( current_batch , <NUM_LIT:1> ) <EOL> self . assertFalse ( cache . add . called ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_curent_batch_key_doesnt_exist_in_cache ( self , cache ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageProcessor <EOL> cache . get . return_value = None <EOL> processor = MessageProcessor ( '<STR_LIT>' ) <EOL> current_batch = processor . current_batch <EOL> self . assertEqual ( current_batch , <NUM_LIT:1> ) <EOL> cache . get . assert_called_once_with ( '<STR_LIT>' ) <EOL> cache . add . assert_called_once_with ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> class BumpBatchTestCase ( unittest . TestCase ) : <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_cache_incremented_by_key ( self , cache ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import bump_batch <EOL> cache . incr . return_value = <NUM_LIT:2> <EOL> val = bump_batch ( '<STR_LIT>' ) <EOL> self . assertEqual ( val , <NUM_LIT:2> ) <EOL> cache . incr . assert_called_once_with ( '<STR_LIT>' ) <EOL> class MessageIteratorTestCase ( unittest . TestCase ) : <EOL> def test_raise_stopiteration_if_no_messages ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageIterator <EOL> iterator = MessageIterator ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> with patch . object ( iterator , '<STR_LIT>' ) as queue : <EOL> queue . lease_tasks_by_tag . return_value = [ ] <EOL> self . assertRaises ( StopIteration , iterator . next ) <EOL> def test_iterates ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageIterator <EOL> payload = '<STR_LIT>' <EOL> task = Mock ( payload = payload , tag = '<STR_LIT>' ) <EOL> task1 = Mock ( payload = payload , tag = '<STR_LIT>' ) <EOL> task2 = Mock ( payload = payload , tag = '<STR_LIT>' ) <EOL> iterator = MessageIterator ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> with patch . object ( iterator , '<STR_LIT>' ) as queue : <EOL> queue . lease_tasks_by_tag . return_value = [ task , task1 , task2 ] <EOL> results = [ payload for payload in iterator ] <EOL> self . assertEqual ( results , [ payload , payload , payload ] ) <EOL> def test_calls_lease_exactly_once ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageIterator <EOL> payload = '<STR_LIT>' <EOL> task = Mock ( payload = payload , tag = '<STR_LIT>' ) <EOL> message_iterator = MessageIterator ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> with patch . object ( message_iterator , '<STR_LIT>' ) as queue : <EOL> queue . lease_tasks_by_tag . return_value = [ task ] <EOL> iterator = iter ( message_iterator ) <EOL> iterator . next ( ) <EOL> self . assertRaises ( StopIteration , iterator . next ) <EOL> self . assertRaises ( StopIteration , iterator . next ) <EOL> queue . lease_tasks_by_tag . assert_called_once_with ( <EOL> <NUM_LIT> , <NUM_LIT:1> , tag = '<STR_LIT>' , deadline = <NUM_LIT:10> ) <EOL> def test_rerun_after_depletion_calls_once ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageIterator <EOL> payload = '<STR_LIT>' <EOL> task = Mock ( payload = payload , tag = '<STR_LIT>' ) <EOL> iterator = MessageIterator ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> with patch . object ( iterator , '<STR_LIT>' ) as queue : <EOL> queue . lease_tasks_by_tag . return_value = [ task ] <EOL> results = [ payload for payload in iterator ] <EOL> self . assertEqual ( results , [ payload ] ) <EOL> results = [ payload for payload in iterator ] <EOL> queue . lease_tasks_by_tag . assert_called_once_with ( <EOL> <NUM_LIT> , <NUM_LIT:1> , tag = '<STR_LIT>' , deadline = <NUM_LIT:10> ) <EOL> def test_rerun_after_depletion_doesnt_delete_too_much ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageIterator <EOL> payload = '<STR_LIT>' <EOL> task = Mock ( payload = payload , tag = '<STR_LIT>' ) <EOL> iterator = MessageIterator ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> , auto_delete = False ) <EOL> with patch . object ( iterator , '<STR_LIT>' ) as queue : <EOL> queue . lease_tasks_by_tag . return_value = [ task ] <EOL> results = [ payload for payload in iterator ] <EOL> self . assertEqual ( results , [ payload ] ) <EOL> task_1 = Mock ( payload = '<STR_LIT>' , tag = '<STR_LIT>' ) <EOL> queue . lease_tasks_by_tag . return_value = [ task_1 ] <EOL> results = [ payload for payload in iterator ] <EOL> self . assertEqual ( results , [ payload ] ) <EOL> queue . lease_tasks_by_tag . assert_called_once_with ( <EOL> <NUM_LIT> , <NUM_LIT:1> , tag = '<STR_LIT>' , deadline = <NUM_LIT:10> ) <EOL> iterator . delete_messages ( ) <EOL> queue . delete_tasks . assert_called_once_with ( [ task ] ) <EOL> def test_custom_deadline ( self ) : <EOL> """<STR_LIT>""" <EOL> from furious . batcher import MessageIterator <EOL> payload = '<STR_LIT>' <EOL> task = Mock ( payload = payload , tag = '<STR_LIT>' ) <EOL> message_iterator = MessageIterator ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> , deadline = <NUM_LIT:2> ) <EOL> with patch . object ( message_iterator , '<STR_LIT>' ) as queue : <EOL> queue . lease_tasks_by_tag . return_value = [ task ] <EOL> iterator = iter ( message_iterator ) <EOL> iterator . next ( ) <EOL> self . assertRaises ( StopIteration , iterator . next ) <EOL> queue . lease_tasks_by_tag . assert_called_once_with ( <EOL> <NUM_LIT> , <NUM_LIT:1> , tag = '<STR_LIT>' , deadline = <NUM_LIT:2> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_time_check ( self , time ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . runtime import apiproxy_errors <EOL> from furious . batcher import MessageIterator <EOL> time . side_effect = [ <NUM_LIT:0.0> , <NUM_LIT> ] <EOL> message_iterator = MessageIterator ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> with patch . object ( message_iterator , '<STR_LIT>' ) as queue : <EOL> queue . lease_tasks_by_tag . return_value = [ ] <EOL> self . assertRaises ( <EOL> apiproxy_errors . DeadlineExceededError , iter , message_iterator ) </s>
