Ejemplo n.º 1
0
def get_queue(
    queue_type,
    queue_name,
    host=HOST,
    port=PORT,
    auth_token=None,
    ssl=False,
    iterator_timeout=0,
):
    host = HOST if host is None else host
    port = PORT if port is None else port
    auth_token = None if auth_token is None else auth_token

    if queue_type == "list":
        return ListQueue(
            queue_name,
            ListQueueClient(host, port, DB, auth_token, ssl),
            iterator_timeout,
        )
    elif queue_type == "sorted":
        return SchedulingQueue(
            queue_name, SortedQueueClient(host, port, DB, auth_token, ssl)
        )
    else:
        raise ModuleNotFoundError(
            f"{queue_type} is not a valid queue type, options include 'list', 'sorted'"
        )
Ejemplo n.º 2
0
def test_iterator_with_timeout():
    queue_name = f"test_list_queue_test_iterator_with_timeout"
    list_queue = ListQueue(queue_name,
                           ListQueueClient(HOST, PORT, DB),
                           iterator_timeout=2)
    list_queue.initialize()

    output = list_queue.put("test1")
    assert output == 1

    output = list_queue.put("test2")
    assert output == 2

    output = list_queue.put("test3")
    assert output == 3

    message_index = 1

    for message in list_queue:
        assert isinstance(message, Message)

        message_value = message.value.decode("utf-8")
        assert message_value == f"test{message_index}"

        message_index += 1

    # assert 3 messages have been processed and iterator exited after timeout exceeded.
    assert message_index == 4
Ejemplo n.º 3
0
def test_iterator():
    queue_name = f"test_list_queue_test_iterator"
    list_queue = ListQueue(queue_name, ListQueueClient(HOST, PORT, DB))
    list_queue.initialize()

    output = list_queue.put("test1")
    assert output == 1

    output = list_queue.put("test2")
    assert output == 2

    output = list_queue.put("test3")
    assert output == 3

    message_index = 1

    for message in list_queue:
        assert isinstance(message, Message)

        message_value = message.value.decode("utf-8")
        assert message_value == f"test{message_index}"

        if message_index == 3:
            break

        message_index += 1
Ejemplo n.º 4
0
def test_put_with_duplicate_items_with_negative_1_count():
    list_queue = ListQueue(f"test_list_queue_test_put_with_duplicate_item",
                           ListQueueClient(HOST, PORT, DB))
    list_queue.initialize()
    output = list_queue.put("test")
    assert output == 1

    output = list_queue.put("test", clear_existing=False)
    assert output == 2

    output = list_queue.put("test", count=-1)
    assert output == 2
Ejemplo n.º 5
0
def test_put_with_duplicate_item():
    list_queue = ListQueue(f"test_list_queue_test_put_with_duplicate_item",
                           ListQueueClient(HOST, PORT, DB))
    list_queue.initialize()
    output = list_queue.put("test")
    assert output == 1

    output = list_queue.put("test")
    assert output == 1
Ejemplo n.º 6
0
def test_put_with_duplicate_item_without_clear_existing():
    list_queue = ListQueue(
        f"test_list_queue_test_put_with_duplicate_item_without_clear_existing",
        ListQueueClient(HOST, PORT, DB),
    )
    list_queue.initialize()
    output = list_queue.put("test")
    assert output == 1

    output = list_queue.put("test", clear_existing=False)
    assert output == 2
Ejemplo n.º 7
0
def test_delete():
    queue_name = f"test_list_queue_test_delete"
    list_queue = ListQueue(queue_name, ListQueueClient(HOST, PORT, DB))
    list_queue.initialize()

    output = list_queue.put("test")
    assert output == 1

    output = list_queue.remove_item("test")

    assert output == 1
Ejemplo n.º 8
0
def test_put_r_put():
    list_queue = ListQueue(
        f"test_put_r_put",
        ListQueueClient(HOST, PORT, DB),
    )

    list_queue.initialize()
    output = list_queue.put("test")
    assert output == 1

    output = list_queue.r_put("test1")
    assert output == 2

    item1 = list_queue.get()
    item2 = list_queue.get()

    # Assert first item is the item added via r_put
    assert item1[1].decode("utf-8") == "test1"
    assert item2[1].decode("utf-8") == "test"
Ejemplo n.º 9
0
def test_get():
    queue_name = f"test_list_queue_test_get"
    list_queue = ListQueue(queue_name, ListQueueClient(HOST, PORT, DB))
    list_queue.initialize()
    output = list_queue.put("test")
    assert output == 1

    get_output = list_queue.get()

    assert isinstance(get_output, tuple)

    # Queue name as first tuple item
    assert get_output[0].decode("utf-8") == queue_name

    # Queue value as second tuple item
    assert get_output[1].decode("utf-8") == "test"
Ejemplo n.º 10
0
def test_list_all():
    queue_name = f"test_list_queue_test_list_all"
    list_queue = ListQueue(queue_name, ListQueueClient(HOST, PORT, DB))
    list_queue.initialize()

    list_queue.put("test")
    list_queue.put("test2")
    list_queue.put("test3")
    list_queue.put("test4")

    output = list_queue.list_all()

    assert len(output) == 4
    assert isinstance(output, list)
Ejemplo n.º 11
0
def test_transaction_abort_with_previous_put():
    queue_name = f"test_list_queue_test_transaction_abort_with_previous_put"
    list_queue = ListQueue(queue_name, ListQueueClient(HOST, PORT, DB))
    list_queue.initialize()

    list_queue.put("test_prev")

    list_queue.begin_transaction()
    list_queue.put("test")
    list_queue.abort_transaction()

    get_output = list_queue.get()

    assert isinstance(get_output, tuple)

    # Queue name as first tuple item
    assert get_output[0].decode("utf-8") == queue_name

    # Queue value as second tuple item
    assert get_output[1].decode("utf-8") == "test_prev"

    next_get_output = list_queue.get()

    assert next_get_output is None
Ejemplo n.º 12
0
def test_transaction_abort():
    queue_name = f"test_list_queue_test_transaction_abort"
    list_queue = ListQueue(queue_name, ListQueueClient(HOST, PORT, DB))
    list_queue.initialize()

    list_queue.begin_transaction()
    list_queue.put("test")
    list_queue.abort_transaction()

    get_output = list_queue.get()

    assert get_output is None
Ejemplo n.º 13
0
def test_put():
    list_queue = ListQueue(f"test_list_queue_test_put",
                           ListQueueClient(HOST, PORT, DB))
    list_queue.initialize()
    output = list_queue.put("test")
    assert output == 1