# NOTE: This code require service-bus package > 7.x # ------------------------------------------------------ import os from azure.servicebus import ServiceBusClient, Message from azure.identity import ClientSecretCredential #topic & subscription connection details FULLY_QUALIFIED_NAMESPACE = os.environ['SERVICE_BUS_NAMESPACE'] TOPIC_NAME = os.environ["SERVICE_BUS_TOPIC_NAME"] SUBSCRIPTION_NAME = os.environ["SERVICE_BUS_SUBSCRIPTION_NAME"] # credentials details AZURE_TENANT_ID = os.environ["AZURE_TENANT_ID"] AZURE_CLIENT_SECRET = os.environ["AZURE_CLIENT_SECRET"] AZURE_CLIENT_ID = os.environ["AZURE_CLIENT_ID"] credential = ClientSecretCredential(tenant_id=AZURE_TENANT_ID, client_id=AZURE_CLIENT_ID, client_secret=AZURE_CLIENT_SECRET) servicebus_client = ServiceBusClient(FULLY_QUALIFIED_NAMESPACE, credential) receiver = servicebus_client.get_subscription_receiver( topic_name=TOPIC_NAME, subscription_name=SUBSCRIPTION_NAME) with receiver: for message in receiver.get_streaming_message_iter(): print(str(message)) message.complete()
def test_backoff_fixed_retry(self): client = ServiceBusClient('fake.host.com', 'fake_eh', retry_mode='fixed') # queue sender sender = client.get_queue_sender('fake_name') backoff = client._config.retry_backoff_factor start_time = time.time() sender._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time # exp = 0.8 * (2 ** 1) = 1.6 # time.sleep() in _backoff will take AT LEAST time 'exp' for retry_mode='exponential' # check that fixed is less than 'exp' assert sleep_time_fixed < backoff * (2**1) # topic sender sender = client.get_topic_sender('fake_name') backoff = client._config.retry_backoff_factor start_time = time.time() sender._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time assert sleep_time_fixed < backoff * (2**1) # queue receiver receiver = client.get_queue_receiver('fake_name') backoff = client._config.retry_backoff_factor start_time = time.time() receiver._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time assert sleep_time_fixed < backoff * (2**1) # subscription receiver receiver = client.get_subscription_receiver('fake_topic', 'fake_sub') backoff = client._config.retry_backoff_factor start_time = time.time() receiver._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time assert sleep_time_fixed < backoff * (2**1) client = ServiceBusClient('fake.host.com', 'fake_eh', retry_mode=RetryMode.Fixed) # queue sender sender = client.get_queue_sender('fake_name') backoff = client._config.retry_backoff_factor start_time = time.time() sender._backoff(retried_times=1, last_exception=Exception('fake'), abs_timeout_time=None) sleep_time_fixed = time.time() - start_time # exp = 0.8 * (2 ** 1) = 1.6 # time.sleep() in _backoff will take AT LEAST time 'exp' for retry_mode='exponential' # check that fixed is less than 'exp' assert sleep_time_fixed < backoff * (2**1)