I am new to python and want to write unit testcase for pub/sub asynchronous pull feature . I understand that we need to mock the function for subscriber,event message but I am not sure how that can be done. Also how to test the assertion when we are creating the stream and not returning anything .
from google.cloud import pubsub_v1
def pubsub_listener():
project_id = <<Environment Variable>>
subscription_id = <<Environment Variable>>
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
logging.info(f"Received {message}.")
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
logging.info(f"Listening for messages on {subscription_path}..\n")
with subscriber:
try:
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel()
streaming_pull_future.result()
if __name__ =='__main__':
pubsub_listener()