async def test_send_with_invalid_policy_async(invalid_policy): client = EventHubProducerClient.from_connection_string(invalid_policy) async with client: with pytest.raises(ConnectError): batch = EventDataBatch() batch.add(EventData("test data")) await client.send_batch(batch)
async def test_non_existing_entity_sender_async(connection_str): client = EventHubProducerClient.from_connection_string( connection_str, eventhub_name="nemo") async with client: with pytest.raises(ConnectError): batch = EventDataBatch() batch.add(EventData("test data")) await client.send_batch(batch)
def test_send_batch_with_invalid_key(invalid_key): client = EventHubProducerClient.from_connection_string(invalid_key) try: with pytest.raises(ConnectError): batch = EventDataBatch() batch.add(EventData("test data")) client.send_batch(batch) finally: client.close()
def test_send_batch_with_invalid_key(live_eventhub): conn_str = live_eventhub["connection_str"].format( live_eventhub['hostname'], live_eventhub['key_name'], 'invalid', live_eventhub['event_hub']) client = EventHubProducerClient.from_connection_string(conn_str) with pytest.raises(ConnectError): batch = EventDataBatch() batch.add(EventData("test data")) client.send_batch(batch) client.close()
def test_send_batch_with_invalid_hostname(invalid_hostname): if sys.platform.startswith('darwin'): pytest.skip( "Skipping on OSX - it keeps reporting 'Unable to set external certificates' " "and blocking other tests") client = EventHubProducerClient.from_connection_string(invalid_hostname) with client: with pytest.raises(ConnectError): batch = EventDataBatch() batch.add(EventData("test data")) client.send_batch(batch)
def test_send_batch_pid_pk(invalid_hostname, partition_id, partition_key): # Use invalid_hostname because this is not a live test. client = EventHubProducerClient.from_connection_string(invalid_hostname) batch = EventDataBatch(partition_id=partition_id, partition_key=partition_key) with client: with pytest.raises(TypeError): client.send_batch(batch, partition_id=partition_id, partition_key=partition_key)
async def create_batch(self, max_size=None): # type:(int) -> EventDataBatch """ Create an EventDataBatch object with max size being max_size. The max_size should be no greater than the max allowed message size defined by the service side. :param int max_size: The maximum size of bytes data that an EventDataBatch object can hold. :rtype: ~azure.eventhub.EventDataBatch .. admonition:: Example: .. literalinclude:: ../samples/async_samples/sample_code_eventhub_async.py :start-after: [START eventhub_producer_client_create_batch_async] :end-before: [END eventhub_producer_client_create_batch_async] :language: python :dedent: 4 :caption: Create EventDataBatch object within limited size """ if not self._max_message_size_on_link: await self._init_locks_for_producers() async with self._producers_locks[-1]: if self._producers[-1] is None: self._producers[-1] = self._create_producer(partition_id=None) await self._producers[-1]._open_with_retry() # pylint: disable=protected-access async with self._client_lock: self._max_message_size_on_link = \ self._producers[-1]._handler.message_handler._link.peer_max_message_size or constants.MAX_MESSAGE_LENGTH_BYTES # pylint: disable=protected-access, line-too-long if max_size and max_size > self._max_message_size_on_link: raise ValueError('Max message size: {} is too large, acceptable max batch size is: {} bytes.' .format(max_size, self._max_message_size_on_link)) return EventDataBatch(max_size=(max_size or self._max_message_size_on_link))
def test_event_data_batch(): batch = EventDataBatch(max_size_in_bytes=100, partition_key="par") batch.add(EventData("A")) assert str(batch) == "EventDataBatch(max_size_in_bytes=100, partition_id=None, partition_key='par', event_count=1)" assert repr(batch) == "EventDataBatch(max_size_in_bytes=100, partition_id=None, partition_key='par', event_count=1)" assert batch.size_in_bytes == 97 and len(batch) == 1 with pytest.raises(ValueError): batch.add(EventData("A"))
def test_event_data_batch(): batch = EventDataBatch(max_size_in_bytes=110, partition_key="par") batch.add(EventData("A")) assert str( batch ) == "EventDataBatch(max_size_in_bytes=110, partition_id=None, partition_key='par', event_count=1)" assert repr( batch ) == "EventDataBatch(max_size_in_bytes=110, partition_id=None, partition_key='par', event_count=1)" # In uamqp v1.2.8, the encoding size of a message has changed. delivery_count in message header is now set to 0 # instead of None according to the C spec. # This uamqp change is transparent to EH users so it's not considered as a breaking change. However, it's breaking # the unit test here. The solution is to add backward compatibility in test. if version.parse(uamqp.__version__) >= version.parse("1.2.8"): assert batch.size_in_bytes == 101 and len(batch) == 1 else: assert batch.size_in_bytes == 93 and len(batch) == 1 with pytest.raises(ValueError): batch.add(EventData("A"))
def test_event_data_batch(): batch = EventDataBatch(max_size_in_bytes=100, partition_key="par") batch.add(EventData("A")) assert batch.size_in_bytes == 89 and len(batch) == 1 with pytest.raises(ValueError): batch.add(EventData("A"))