Exemple #1
0
def test_receive_with_sequence_no(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str)
    receiver = client._create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition('@latest'))

    with receiver:
        received = receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Data"))
        time.sleep(1)
        received = receiver.receive(timeout=5)
        assert len(received) == 1
        offset = received[0].sequence_number

    offset_receiver = client._create_consumer(consumer_group="$default",
                                              partition_id="0",
                                              event_position=EventPosition(
                                                  offset, False))
    with offset_receiver:
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Message next in sequence"))
        time.sleep(1)
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 1
    client.close()
def test_create_batch_with_invalid_hostname_sync(invalid_hostname):
    client = EventHubClient.from_connection_string(invalid_hostname)
    sender = client._create_producer()
    with pytest.raises(AuthenticationError):
        sender.create_batch(max_size=300)
    sender.close()
    client.close()
Exemple #3
0
def test_receive_with_custom_datetime_sync(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str)
    for i in range(5):
        senders[0].send(EventData(b"Message before timestamp"))
    time.sleep(65)

    now = datetime.datetime.utcnow()
    offset = datetime.datetime(now.year, now.month, now.day, now.hour,
                               now.minute)
    for i in range(5):
        senders[0].send(EventData(b"Message after timestamp"))

    receiver = client._create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition(offset))
    with receiver:
        all_received = []
        received = receiver.receive(timeout=5)
        while received:
            all_received.extend(received)
            received = receiver.receive(timeout=5)

        assert len(all_received) == 5
        for received_event in all_received:
            assert received_event.body_as_str() == "Message after timestamp"
            assert received_event.enqueued_time > offset
    client.close()
Exemple #4
0
def test_send_with_create_event_batch_sync(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(
        connection_str, transport_type=TransportType.AmqpOverWebsocket)
    sender = client._create_producer()

    event_data_batch = sender.create_batch(max_size=100000)
    while True:
        try:
            event_data_batch.try_add(EventData('A single event data'))
        except ValueError:
            break

    sender.send(event_data_batch)

    event_data_batch = sender.create_batch(max_size=100000)
    while True:
        try:
            event_data_batch.try_add(EventData('A single event data'))
        except ValueError:
            break

    sender.send(event_data_batch)
    sender.close()
    client.close()
def test_send_with_invalid_policy(invalid_policy):
    client = EventHubClient.from_connection_string(invalid_policy)
    sender = client._create_producer()
    with pytest.raises(AuthenticationError):
        sender.send(EventData("test data"))
    sender.close()
    client.close()
Exemple #6
0
def test_send_batch_with_app_prop_sync(connstr_receivers):
    connection_str, receivers = connstr_receivers
    app_prop_key = "raw_prop"
    app_prop_value = "raw_value"
    app_prop = {app_prop_key: app_prop_value}

    def batched():
        for i in range(10):
            ed = EventData("Event number {}".format(i))
            ed.application_properties = app_prop
            yield ed
        for i in range(10, 20):
            ed = EventData("Event number {}".format(i))
            ed.application_properties = app_prop
            yield ed

    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer()
    with sender:
        sender.send(batched())

    time.sleep(1)

    received = []
    for r in receivers:
        received.extend(r.receive(timeout=3))

    assert len(received) == 20
    for index, message in enumerate(received):
        assert list(
            message.body)[0] == "Event number {}".format(index).encode('utf-8')
        assert (app_prop_key.encode('utf-8') in message.application_properties) \
            and (dict(message.application_properties)[app_prop_key.encode('utf-8')] == app_prop_value.encode('utf-8'))
    client.close()
Exemple #7
0
def test_receive_with_inclusive_offset(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str)
    receiver = client._create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition('@latest'))

    with receiver:
        received = receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Data"))
        time.sleep(1)
        received = receiver.receive(timeout=5)
        assert len(received) == 1
        offset = received[0].offset

        assert list(received[0].body) == [b'Data']
        assert received[0].body_as_str() == "Data"

    offset_receiver = client._create_consumer(consumer_group="$default",
                                              partition_id="0",
                                              event_position=EventPosition(
                                                  offset, inclusive=True))
    with offset_receiver:
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 1
    client.close()
Exemple #8
0
def test_receive_with_offset_sync(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str)
    partitions = client.get_properties()
    assert partitions["partition_ids"] == ["0", "1"]
    receiver = client._create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition('@latest'))
    with receiver:
        more_partitions = client.get_properties()
        assert more_partitions["partition_ids"] == ["0", "1"]

        received = receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Data"))
        received = receiver.receive(timeout=5)
        assert len(received) == 1
        offset = received[0].offset

        assert list(received[0].body) == [b'Data']
        assert received[0].body_as_str() == "Data"

    offset_receiver = client._create_consumer(consumer_group="$default",
                                              partition_id="0",
                                              event_position=EventPosition(
                                                  offset, inclusive=False))
    with offset_receiver:
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Message after offset"))
        received = offset_receiver.receive(timeout=5)
        assert len(received) == 1
    client.close()
def test_create_batch_with_too_large_size_sync(connection_str):
    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer()
    with pytest.raises(ValueError):
        sender.create_batch(max_size=5 * 1024 * 1024)
    sender.close()
    client.close()
def test_non_existing_entity_sender(connection_str):
    client = EventHubClient.from_connection_string(connection_str,
                                                   event_hub_path="nemo")
    sender = client._create_producer(partition_id="1")
    with pytest.raises(AuthenticationError):
        sender.send(EventData("test data"))
    sender.close()
    client.close()
def test_receive_with_invalid_policy_sync(invalid_policy):
    client = EventHubClient.from_connection_string(invalid_policy)
    receiver = client._create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition("-1"))
    with pytest.raises(AuthenticationError):
        receiver.receive(timeout=5)
    receiver.close()
    client.close()
def test_non_existing_entity_receiver(connection_str):
    client = EventHubClient.from_connection_string(connection_str,
                                                   event_hub_path="nemo")
    receiver = client._create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition("-1"))
    with pytest.raises(AuthenticationError):
        receiver.receive(timeout=5)
    receiver.close()
    client.close()
def test_send_null_body(connection_str):
    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer()
    try:
        with pytest.raises(ValueError):
            data = EventData(None)
            sender.send(data)
    finally:
        sender.close()
        client.close()
def test_send_to_invalid_partitions(connection_str):
    partitions = ["XYZ", "-1", "1000", "-"]
    for p in partitions:
        client = EventHubClient.from_connection_string(connection_str)
        sender = client._create_producer(partition_id=p)
        try:
            with pytest.raises(ConnectError):
                sender.send(EventData("test data"))
        finally:
            sender.close()
            client.close()
def connstr_senders(connection_str):
    client = EventHubClient.from_connection_string(connection_str, network_tracing=False)
    partitions = client.get_partition_ids()

    senders = []
    for p in partitions:
        sender = client._create_producer(partition_id=p)
        senders.append(sender)
    yield connection_str, senders
    for s in senders:
        s.close()
    client.close()
Exemple #16
0
def test_send_partition(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer(partition_id="1")
    with sender:
        sender.send(EventData(b"Data"))

    partition_0 = receivers[0].receive(timeout=2)
    assert len(partition_0) == 0
    partition_1 = receivers[1].receive(timeout=2)
    assert len(partition_1) == 1
    client.close()
def test_send_too_large_message(connection_str):
    if sys.platform.startswith('darwin'):
        pytest.skip("Skipping on OSX - open issue regarding message size")
    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer()
    try:
        data = EventData(b"A" * 1100000)
        with pytest.raises(EventDataSendError):
            sender.send(data)
    finally:
        sender.close()
        client.close()
def connstr_receivers(connection_str):
    client = EventHubClient.from_connection_string(connection_str, network_tracing=False)
    partitions = client.get_partition_ids()
    receivers = []
    for p in partitions:
        receiver = client._create_consumer(consumer_group="$default", partition_id=p, event_position=EventPosition("-1"), prefetch=500)
        receiver._open()
        receivers.append(receiver)
    yield connection_str, receivers

    for r in receivers:
        r.close()
    client.close()
def test_receive_from_invalid_partitions_sync(connection_str):
    partitions = ["XYZ", "-1", "1000", "-"]
    for p in partitions:
        client = EventHubClient.from_connection_string(connection_str)
        receiver = client._create_consumer(consumer_group="$default",
                                           partition_id=p,
                                           event_position=EventPosition("-1"))
        try:
            with pytest.raises(ConnectError):
                receiver.receive(timeout=5)
        finally:
            receiver.close()
            client.close()
Exemple #20
0
def test_send_non_ascii(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer(partition_id="0")
    with sender:
        sender.send(EventData(u"é,è,à,ù,â,ê,î,ô,û"))
        sender.send(EventData(json.dumps({"foo": u"漢字"})))
    time.sleep(1)
    partition_0 = receivers[0].receive(timeout=2)
    assert len(partition_0) == 2
    assert partition_0[0].body_as_str() == u"é,è,à,ù,â,ê,î,ô,û"
    assert partition_0[1].body_as_json() == {"foo": u"漢字"}
    client.close()
def test_send_partition_key_with_partition_sync(connection_str):
    pytest.skip(
        "Skipped tentatively. Confirm whether to throw ValueError or just warn users"
    )
    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer(partition_id="1")
    try:
        data = EventData(b"Data")
        data._set_partition_key(b"PKey")
        with pytest.raises(ValueError):
            sender.send(data)
    finally:
        sender.close()
        client.close()
Exemple #22
0
def test_send_single_event(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer()
    with sender:
        sender.send(EventData(b"A single event"))

    received = []
    for r in receivers:
        received.extend(r.receive(timeout=1))

    assert len(received) == 1
    assert list(received[0].body)[0] == b"A single event"
    client.close()
Exemple #23
0
def test_send_array_sync(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer()
    with sender:
        sender.send(EventData([b"A", b"B", b"C"]))

    received = []
    for r in receivers:
        received.extend(r.receive(timeout=1))

    assert len(received) == 1
    assert list(received[0].body) == [b"A", b"B", b"C"]
    client.close()
Exemple #24
0
def test_send_multiple_clients(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(connection_str)
    sender_0 = client._create_producer(partition_id="0")
    sender_1 = client._create_producer(partition_id="1")
    with sender_0:
        sender_0.send(EventData(b"Message 0"))
    with sender_1:
        sender_1.send(EventData(b"Message 1"))

    partition_0 = receivers[0].receive(timeout=2)
    assert len(partition_0) == 1
    partition_1 = receivers[1].receive(timeout=2)
    assert len(partition_1) == 1
    client.close()
Exemple #25
0
def test_receive_end_of_stream(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str)
    receiver = client._create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition('@latest'))
    with receiver:
        received = receiver.receive(timeout=5)
        assert len(received) == 0
        senders[0].send(EventData(b"Receiving only a single event"))
        received = receiver.receive(timeout=5)
        assert len(received) == 1

        assert received[0].body_as_str() == "Receiving only a single event"
        assert list(received[-1].body)[0] == b"Receiving only a single event"
    client.close()
Exemple #26
0
def test_send_and_receive_large_body_size(connstr_receivers):
    if sys.platform.startswith('darwin'):
        pytest.skip("Skipping on OSX - open issue regarding message size")
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer()
    with sender:
        payload = 250 * 1024
        sender.send(EventData("A" * payload))

    received = []
    for r in receivers:
        received.extend(r.receive(timeout=10))

    assert len(received) == 1
    assert len(list(received[0].body)[0]) == payload
    client.close()
Exemple #27
0
def test_send_partition_batch(connstr_receivers):
    connection_str, receivers = connstr_receivers

    def batched():
        for i in range(10):
            yield EventData("Event number {}".format(i))

    client = EventHubClient.from_connection_string(connection_str)
    sender = client._create_producer(partition_id="1")
    with sender:
        sender.send(batched())
        time.sleep(1)

    partition_0 = receivers[0].receive(timeout=2)
    assert len(partition_0) == 0
    partition_1 = receivers[1].receive(timeout=2)
    assert len(partition_1) == 10
    client.close()
Exemple #28
0
def test_send_over_websocket_sync(connstr_receivers):
    connection_str, receivers = connstr_receivers
    client = EventHubClient.from_connection_string(
        connection_str, transport_type=TransportType.AmqpOverWebsocket)
    sender = client._create_producer()

    event_list = []
    for i in range(20):
        event_list.append(EventData("Event Number {}".format(i)))

    with sender:
        sender.send(event_list)

    time.sleep(1)
    received = []
    for r in receivers:
        received.extend(r.receive(timeout=3))

    assert len(received) == 20
    client.close()
Exemple #29
0
def test_receive_batch(connstr_senders):
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(connection_str)
    receiver = client._create_consumer(consumer_group="$default",
                                       partition_id="0",
                                       event_position=EventPosition('@latest'),
                                       prefetch=500)
    with receiver:
        received = receiver.receive(timeout=5)
        assert len(received) == 0
        for i in range(10):
            senders[0].send(EventData(b"Data"))
        received = receiver.receive(max_batch_size=5, timeout=5)
        assert len(received) == 5

        for event in received:
            assert event.system_properties
            assert event.sequence_number is not None
            assert event.offset
            assert event.enqueued_time
    client.close()
Exemple #30
0
def test_receive_run_time_metric(connstr_senders):
    from uamqp import __version__ as uamqp_version
    from distutils.version import StrictVersion
    if StrictVersion(uamqp_version) < StrictVersion('1.2.3'):
        pytest.skip(
            "Disabled for uamqp 1.2.2. Will enable after uamqp 1.2.3 is released."
        )
    connection_str, senders = connstr_senders
    client = EventHubClient.from_connection_string(
        connection_str, transport_type=TransportType.AmqpOverWebsocket)
    receiver = client._create_consumer(
        consumer_group="$default",
        partition_id="0",
        event_position=EventPosition('@latest'),
        prefetch=500,
        track_last_enqueued_event_properties=True)

    event_list = []
    for i in range(20):
        event_list.append(EventData("Event Number {}".format(i)))

    with receiver:
        received = receiver.receive(timeout=5)
        assert len(received) == 0

        senders[0].send(event_list)

        time.sleep(1)

        received = receiver.receive(max_batch_size=50, timeout=5)
        assert len(received) == 20
        assert receiver.last_enqueued_event_properties
        assert receiver.last_enqueued_event_properties.get(
            'sequence_number', None)
        assert receiver.last_enqueued_event_properties.get('offset', None)
        assert receiver.last_enqueued_event_properties.get(
            'enqueued_time', None)
        assert receiver.last_enqueued_event_properties.get(
            'retrieval_time', None)
    client.close()