def test_get_single_item(queue, reporter, transaction_factory): # Add a single item to the queue. queue.put(["abc", "def"], TEST_MESSAGE_1, available_after=-1) # Have two "instances" retrieve an item to claim. Since there is only one, both calls should # return the same item. now = datetime.utcnow() first_item = queue._select_available_item(False, now) second_item = queue._select_available_item(False, now) assert first_item.id == second_item.id assert first_item.state_id == second_item.state_id # Have both "instances" now try to claim the item. Only one should succeed. first_claimed = queue._attempt_to_claim_item(first_item, now, 300) second_claimed = queue._attempt_to_claim_item(first_item, now, 300) assert first_claimed assert not second_claimed # Ensure the item is no longer available. assert queue.get() is None # Ensure the item's state ID has changed. assert first_item.state_id != QueueItem.get().state_id
def has_retries_remaining(self, item_id): """ Returns whether the queue item with the given id has any retries remaining. If the queue item does not exist, returns False. """ with self._transaction_factory(db): try: return QueueItem.get(id=item_id).retries_remaining > 0 except QueueItem.DoesNotExist: return False
def test_extend_processing(queue, reporter, transaction_factory): # Add and retrieve a queue item. queue.put(["abc", "def"], TEST_MESSAGE_1, available_after=-1) queue_item = queue.get(processing_time=10) assert queue_item is not None existing_db_item = QueueItem.get(id=queue_item.id) # Call extend processing with a timedelta less than the minimum and ensure its # processing_expires and state_id do not change. changed = queue.extend_processing( queue_item, 10 + MINIMUM_EXTENSION.total_seconds() - 1) assert not changed updated_db_item = QueueItem.get(id=queue_item.id) assert existing_db_item.processing_expires == updated_db_item.processing_expires assert existing_db_item.state_id == updated_db_item.state_id # Call extend processing with a timedelta greater than the minimum and ensure its # processing_expires and state_id are changed. changed = queue.extend_processing( queue_item, 10 + MINIMUM_EXTENSION.total_seconds() + 1) assert changed updated_db_item = QueueItem.get(id=queue_item.id) assert existing_db_item.processing_expires != updated_db_item.processing_expires assert existing_db_item.state_id != updated_db_item.state_id # Call extend processing with a timedelta less than the minimum but also with new data and # ensure its processing_expires and state_id are changed. changed = queue.extend_processing(queue_item, 10 + MINIMUM_EXTENSION.total_seconds() - 1, updated_data="newbody") assert changed updated_db_item = QueueItem.get(id=queue_item.id) assert existing_db_item.processing_expires != updated_db_item.processing_expires assert existing_db_item.state_id != updated_db_item.state_id assert updated_db_item.body == "newbody"