def test_worker_fills_internal_queue_and_respects_visibility_timeouts(): """ Test read workers respect visibility timeouts """ # Setup logging logger = logging.getLogger("pyqs") logger.handlers.append(MockLoggingHandler()) # Setup SQS Queue conn = boto.connect_sqs() queue = conn.create_queue("tester") queue.set_timeout(1) # Add MEssages message = Message() body = '{"body": "KGRwMApTJ3Rhc2snCnAxClMndGVzdHMudGFza3MuaW5kZXhfaW5jcmVtZW50ZXInCnAyCnNTJ2Fy\\nZ3MnCnAzCihscDQKc1Mna3dhcmdzJwpwNQooZHA2ClMnbWVzc2FnZScKcDcKUydUZXN0IG1lc3Nh\\nZ2UyJwpwOApzcy4=\\n", "some stuff": "asdfasf"}' message.set_body(body) queue.write(message) queue.write(message) queue.write(message) # Run Reader internal_queue = Queue(maxsize=1) worker = ReadWorker(queue, internal_queue, BATCHSIZE) worker.read_message() # Check log messages logger.handlers[0].messages['warning'][0].should.contain("Timed out trying to add the following message to the internal queue") logger.handlers[0].messages['warning'][1].should.contain("Clearing Local messages since we exceeded their visibility_timeout")
def test_master_counts_processes(): """ Test managing process counts child processes """ # Setup Logging logger = logging.getLogger("pyqs") del logger.handlers[:] logger.handlers.append(MockLoggingHandler()) # Setup SQS Queue conn = boto3.client('sqs', region_name='us-east-1') conn.create_queue(QueueName="tester") # Setup Manager manager = ManagerWorker(["tester"], 2, 1, 10) manager.start() # Check Workers manager.process_counts() # Cleanup manager.stop() # Check messages msg1 = "Reader Processes: 1" logger.handlers[0].messages['debug'][-2].lower().should.contain( msg1.lower()) msg2 = "Worker Processes: 2" logger.handlers[0].messages['debug'][-1].lower().should.contain( msg2.lower())
def test_worker_fills_internal_queue_and_respects_visibility_timeouts(): """ Test read workers respect visibility timeouts """ # Setup logging logger = logging.getLogger("pyqs") logger.handlers.append(MockLoggingHandler()) # Setup SQS Queue conn = boto3.client('sqs', region_name='us-east-1') queue_url = conn.create_queue(QueueName="tester", Attributes={'VisibilityTimeout': '1'})['QueueUrl'] # Add MEssages message = json.dumps({ "body": ("KGRwMApTJ3Rhc2snCnAxClMndGVzdHMudGFza3MuaW5kZXhfaW5jcmVtZW" "50ZXInCnAyCnNTJ2Fy\nZ3MnCnAzCihscDQKc1Mna3dhcmdzJwpwNQooZHA" "2ClMnbWVzc2FnZScKcDcKUydUZXN0IG1lc3Nh\nZ2UyJwpwOApzcy4=\n"), "some stuff": "asdfasf", }) for _ in range(3): conn.send_message(QueueUrl=queue_url, MessageBody=message) # Run Reader internal_queue = Queue(maxsize=1) worker = ReadWorker(queue_url, internal_queue, BATCHSIZE, parent_id=1) worker.read_message() # Check log messages logger.handlers[0].messages['warning'][0].should.contain( "Timed out trying to add the following message to the internal queue") logger.handlers[0].messages['warning'][1].should.contain( "Clearing Local messages since we exceeded their visibility_timeout")
def test_worker_processes_tasks_and_logs_warning_correctly(): """ Test worker processes logs WARNING correctly """ # Setup logging logger = logging.getLogger("pyqs") del logger.handlers[:] logger.handlers.append(MockLoggingHandler()) # Setup SQS Queue conn = boto3.client('sqs', region_name='us-east-1') queue_url = conn.create_queue(QueueName="tester")['QueueUrl'] # Build the SQS Message message = { 'Body': json.dumps({ 'task': 'tests.tasks.index_incrementer', 'args': [], 'kwargs': { 'message': 23, }, }), "ReceiptHandle": "receipt-1234", "MessageId": "message-id-1", } # Add message to internal queue internal_queue = Queue() internal_queue.put({ "queue": queue_url, "message": message, "start_time": time.time(), "timeout": 30, }) # Process message worker = ProcessWorker(internal_queue, INTERVAL, parent_id=1) worker.process_message() # Check output kwargs = json.loads(message['Body'])['kwargs'] msg1 = ( "Task tests.tasks.index_incrementer raised error in 0.0000 seconds: " "with args: [] and kwargs: {}: " "Traceback (most recent call last)".format(kwargs)) # noqa logger.handlers[0].messages['error'][0].lower().should.contain( msg1.lower()) msg2 = ('ValueError: Need to be given basestring, ' 'was given 23') # noqa logger.handlers[0].messages['error'][0].lower().should.contain( msg2.lower())
def test_worker_processes_discard_tasks_that_exceed_their_visibility_timeout(): """ Test worker processes discards tasks that exceed their visibility timeout """ # Setup logging logger = logging.getLogger("pyqs") del logger.handlers[:] logger.handlers.append(MockLoggingHandler()) # Setup SQS Queue conn = boto3.client('sqs', region_name='us-east-1') queue_url = conn.create_queue(QueueName="tester")['QueueUrl'] # Build the SQS Message message = { 'Body': json.dumps({ 'task': 'tests.tasks.index_incrementer', 'args': [], 'kwargs': { 'message': 23, }, }), "ReceiptHandle": "receipt-1234", "MessageId": "message-id-1", } # Add message to internal queue with timeout of 0 that started long ago internal_queue = Queue() internal_queue.put({ "queue": queue_url, "message": message, "start_time": 0, "timeout": 0, }) # When I process the message worker = ProcessWorker(internal_queue, INTERVAL, parent_id=1) worker.process_message() # Then I get an error about exceeding the visibility timeout kwargs = json.loads(message['Body'])['kwargs'] msg1 = ("Discarding task tests.tasks.index_incrementer with args: [] " "and kwargs: {} due to exceeding " "visibility timeout").format(kwargs) # noqa logger.handlers[0].messages['warning'][0].lower().should.contain( msg1.lower())
def test_worker_processes_tasks_and_logs_correctly(): """ Test worker processes logs INFO correctly """ # Setup logging logger = logging.getLogger("pyqs") del logger.handlers[:] logger.handlers.append(MockLoggingHandler()) # Setup SQS Queue conn = boto3.client('sqs', region_name='us-east-1') queue_url = conn.create_queue(QueueName="tester")['QueueUrl'] # Build the SQS message message = { 'Body': json.dumps({ 'task': 'tests.tasks.index_incrementer', 'args': [], 'kwargs': { 'message': 'Test message', }, }), "ReceiptHandle": "receipt-1234", "MessageId": "message-id-1", } # Add message to internal queue internal_queue = Queue() internal_queue.put({ "queue": queue_url, "message": message, "start_time": time.time(), "timeout": 30, }) # Process message worker = ProcessWorker(internal_queue, INTERVAL, parent_id=1) worker.process_message() # Check output kwargs = json.loads(message['Body'])['kwargs'] expected_result = ( u"Processed task tests.tasks.index_incrementer in 0.0000 seconds " "with args: [] and kwargs: {}".format(kwargs)) logger.handlers[0].messages['info'].should.equal([expected_result])
def test_worker_processes_tasks_and_logs_warning_correctly(): """ Test worker processes logs WARNING correctly """ # Setup logging logger = logging.getLogger("pyqs") del logger.handlers[:] logger.handlers.append(MockLoggingHandler()) # Setup SQS Queue conn = boto.connect_sqs() queue = conn.create_queue("tester") # Build the SQS Message message_body = { 'task': 'tests.tasks.index_incrementer', 'args': [], 'kwargs': { 'message': 23, }, } message = Message() body = json.dumps(message_body) message.set_body(body) # Add message to internal queue internal_queue = Queue() internal_queue.put({ "queue": queue.id, "message": message, "start_time": time.time(), "timeout": 30 }) # Process message worker = ProcessWorker(internal_queue, INTERVAL) worker.process_message() # Check output kwargs = json.loads(body)['kwargs'] msg1 = "Task tests.tasks.index_incrementer raised error in 0.0000 seconds: with args: [] and kwargs: {}: Traceback (most recent call last)".format( kwargs) # noqa logger.handlers[0].messages['error'][0].lower().should.contain( msg1.lower()) msg2 = 'raise ValueError("Need to be given basestring, was given {}".format(message))\nValueError: Need to be given basestring, was given 23' # noqa logger.handlers[0].messages['error'][0].lower().should.contain( msg2.lower())
def test_worker_processes_discard_tasks_that_exceed_their_visibility_timeout(): """ Test worker processes discards tasks that exceed their visibility timeout """ # Setup logging logger = logging.getLogger("pyqs") del logger.handlers[:] logger.handlers.append(MockLoggingHandler()) # Setup SQS Queue conn = boto.connect_sqs() queue = conn.create_queue("tester") # Build the SQS Message message_body = { 'task': 'tests.tasks.index_incrementer', 'args': [], 'kwargs': { 'message': 23, }, } message = Message() body = json.dumps(message_body) message.set_body(body) # Add message to internal queue with timeout of 0 that started long ago internal_queue = Queue() internal_queue.put({ "queue": queue.id, "message": message, "start_time": 0, "timeout": 0 }) # When I process the message worker = ProcessWorker(internal_queue, INTERVAL) worker.process_message() # Then I get an error about exceeding the visibility timeout kwargs = json.loads(body)['kwargs'] msg1 = "Discarding task tests.tasks.index_incrementer with args: [] and kwargs: {} due to exceeding visibility timeout".format( kwargs) # noqa logger.handlers[0].messages['warning'][0].lower().should.contain( msg1.lower())
def test_worker_processes_tasks_and_logs_correctly(): """ Test worker processes logs INFO correctly """ # Setup logging logger = logging.getLogger("pyqs") del logger.handlers[:] logger.handlers.append(MockLoggingHandler()) # Setup SQS Queue conn = boto.connect_sqs() queue = conn.create_queue("tester") # Build the SQS message message_body = { 'task': 'tests.tasks.index_incrementer', 'args': [], 'kwargs': { 'message': 'Test message', }, } message = Message() body = json.dumps(message_body) message.set_body(body) # Add message to internal queue internal_queue = Queue() internal_queue.put({ "queue": queue.id, "message": message, "start_time": time.time(), "timeout": 30 }) # Process message worker = ProcessWorker(internal_queue, INTERVAL) worker.process_message() # Check output kwargs = json.loads(body)['kwargs'] expected_result = u"Processed task tests.tasks.index_incrementer in 0.0000 seconds with args: [] and kwargs: {}".format( kwargs) logger.handlers[0].messages['info'].should.equal([expected_result])
def test_worker_processes_tasks_and_logs_correctly(): """ Test simple worker processes logs INFO correctly """ # Setup logging logger = logging.getLogger("pyqs") del logger.handlers[:] logger.handlers.append(MockLoggingHandler()) queue_url, packed_message = _create_packed_message( 'tests.tasks.index_incrementer') # Process message worker = SimpleProcessWorker(queue_url, INTERVAL, BATCHSIZE, parent_id=1) worker.process_message(packed_message) # Check output kwargs = json.loads(packed_message["message"]['Body'])['kwargs'] expected_result = ( u"Processed task tests.tasks.index_incrementer in 0.0000 seconds " "with args: [] and kwargs: {}".format(kwargs)) logger.handlers[0].messages['info'].should.equal([expected_result])