def test_get_scheduled_items_slice(): queue = SchedulingQueue(f"test_get_scheduled_items_slice", SortedQueueClient(HOST, PORT, DB)) queue.initialize() output = queue.schedule_item(current_timestamp(), "test item") assert output == 1 output = queue.schedule_item(current_timestamp(), "test item1") assert output == 1 output = queue.schedule_item(current_timestamp(), "test item2") assert output == 1 time.sleep(1) get_output = queue.get_scheduled_items(with_scores=False, start=0, num=2) assert isinstance(get_output, list) assert len(get_output) == 2 assert get_output[0].decode("utf-8") == "test item" assert get_output[1].decode("utf-8") == "test item1" get_output = queue.get_scheduled_items(with_scores=False, start=2, num=1) assert isinstance(get_output, list) assert len(get_output) == 1 assert get_output[0].decode("utf-8") == "test item2"
def test_get_range(): queue_name = "test_get_range_queue" sorted_queue_client = SortedQueueClient(HOST, PORT, DB) sorted_queue_client.connect() first_score = current_timestamp() second_score = current_timestamp() third_score = current_timestamp() sorted_queue_client.put(queue_name, first_score, "item1") sorted_queue_client.put(queue_name, second_score, "item2") sorted_queue_client.put(queue_name, third_score, "item3") # Passing no range values retrieves single item with greatest score. range_output = sorted_queue_client.get_range(queue_name, with_scores=False) assert isinstance(range_output, list) assert len(range_output) == 1 assert range_output[0].decode("utf-8") == "item3" # Assert passing start / end range values retrieves expected items. range_output = sorted_queue_client.get_range(queue_name, start=0, end=1, with_scores=False) assert isinstance(range_output, list) assert len(range_output) == 2 assert range_output[0].decode("utf-8") == "item3" assert range_output[1].decode("utf-8") == "item2"
def get_max_items(): queue_name = "get_max_items_queue" sorted_queue_client = SortedQueueClient(HOST, PORT, DB) sorted_queue_client.connect() first_score = current_timestamp() second_score = current_timestamp() sorted_queue_client.put(queue_name, first_score, "item1") sorted_queue_client.put(queue_name, second_score, "item2") range_output = sorted_queue_client.get_max_items(queue_name, 1) assert isinstance(range_output, list) assert len(range_output) == 1 assert range_output[0][0].decode("utf-8") == "item2"
def test_transaction_abort_schedule_items(): queue = SchedulingQueue(f"test_transaction_abort_schedule_queue", SortedQueueClient(HOST, PORT, DB)) queue.initialize() output = queue.schedule_item(current_timestamp(), "test item") assert output == 1 queue.begin_transaction() queue.schedule_item(current_timestamp(), "test transaction item") queue.abort_transaction() time.sleep(1) get_output = queue.get_scheduled_items(with_scores=False) assert isinstance(get_output, list) assert len(get_output) == 1 assert get_output[0].decode("utf-8") == "test item"
def test_remove_items(): queue = SchedulingQueue(f"test_remove_items_queue", SortedQueueClient(HOST, PORT, DB)) queue.initialize() output = queue.schedule_item(current_timestamp(), "test item") assert output == 1 time.sleep(1) output = queue.remove_item("test item") assert output == 1
def test_transaction_commit_remove_schedule_items(): queue = SchedulingQueue( f"test_transaction_commit_remove_schedule_queue", SortedQueueClient(HOST, PORT, DB), ) queue.initialize() output = queue.schedule_item(current_timestamp(), "test item") assert output == 1 queue.begin_transaction() queue.remove_item("test item") queue.commit_transaction() time.sleep(1) get_output = queue.get_scheduled_items(with_scores=False) assert isinstance(get_output, list) assert len(get_output) == 0
def get_scheduled_items(self, with_scores=True, start=None, num=None): return self._queue_client.get(self._queue_name, max=current_timestamp(), with_scores=with_scores, start=start, num=num)