コード例 #1
0
def main():
    # Do set-up.
    logger = utils.setup_logging(CURR_DIR)
    thread_names.monkey_patch()
    utils.make_lease_deterministic()

    # Get clients and resource paths.
    topic_name = 't-repro-{}'.format(int(1000 * time.time()))
    subscription_name = 's-repro-{}'.format(int(1000 * time.time()))
    client_info = utils.get_client_info(topic_name, subscription_name)
    publisher, topic_path, subscriber, subscription_path = client_info

    # Create a topic.
    publisher.create_topic(topic_path)

    # Subscribe to the topic. We do this before the messages are
    # published so that we'll receive them as they come in.
    subscriber.create_subscription(subscription_path, topic_path)
    logger.info('Listening for messages on %s', subscription_path)
    subscription = subscriber.subscribe(subscription_path)
    sub_future = subscription.open(utils.AckCallback(logger))

    # Set off async job to publish some messages.
    publish_async(publisher, topic_path, logger)

    # The subscriber is non-blocking, so we must keep the main thread from
    # exiting to allow it to process messages in the background.
    utils.heartbeats_block(logger, sub_future)

    # Do clean-up.
    publisher.delete_topic(topic_path)
    subscriber.delete_subscription(subscription_path)
    thread_names.save_tree(CURR_DIR, logger)
    thread_names.restore()
    utils.restore()
コード例 #2
0
def main():
    # Do set-up.
    logger = utils.setup_logging(CURR_DIR)
    thread_names.monkey_patch()
    utils.make_lease_deterministic()

    # Get clients and resource paths.
    topic_name = 't-repro-{}'.format(int(1000 * time.time()))
    subscription_name = 's-repro-{}'.format(int(1000 * time.time()))
    client_info = utils.get_client_info(topic_name, subscription_name)
    publisher, topic_path, subscriber, subscription_path = client_info

    # Create a topic, though we won't push messages to it.
    publisher.create_topic(topic_path)

    # Subscribe to the topic, even though it won't publish any messages.
    subscriber.create_subscription(subscription_path, topic_path)
    logger.info('Listening for messages on %s', subscription_path)
    subscription = subscriber.subscribe(subscription_path)
    sub_future = subscription.open(utils.AckCallback(logger))

    # The subscriber is non-blocking, so we must keep the main thread from
    # exiting to allow it to process messages in the background.
    utils.heartbeats_block(logger, sub_future)

    # Do clean-up.
    publisher.delete_topic(topic_path)
    subscriber.delete_subscription(subscription_path)
    thread_names.save_tree(CURR_DIR, logger)
    thread_names.restore()
    utils.restore()
コード例 #3
0
def main():
    # Do set-up.
    logger = utils.setup_logging(CURR_DIR)
    thread_names.monkey_patch()
    random_mod = NotRandom(0.75)
    utils.make_lease_deterministic(random_mod)

    # Get clients and resource paths.
    topic_name = 't-repro-{}'.format(int(1000 * time.time()))
    subscription_name = 's-repro-{}'.format(int(1000 * time.time()))
    client_info = utils.get_client_info(topic_name,
                                        subscription_name,
                                        policy_class=utils.FlowControlPolicy)
    publisher, topic_path, subscriber, subscription_path = client_info

    # Create a topic and subscription (subscription must exist when
    # messages are published to topic).
    publisher.create_topic(topic_path)
    subscriber.create_subscription(subscription_path, topic_path)

    # Set off sync job to publish some messages.
    publish_sync(publisher, topic_path, logger)

    # Sleep to let the backend have some time with its thoughts.
    logger.info('Sleeping for 10s after publishing.')
    time.sleep(10.0)

    # Subscribe to the topic. We do this before the messages are
    # published so that we'll receive them as they come in.
    logger.info('Listening for messages on %s', subscription_path)
    subscription = subscriber.subscribe(subscription_path,
                                        flow_control=types.FlowControl(
                                            max_messages=MAX_MESSAGES, ))
    callback = TrackingCallback(SLEEP_TIME, logger)
    sub_future = subscription.open(callback)

    # The subscriber is non-blocking, so we must keep the main thread from
    # exiting to allow it to process messages in the background.
    helper = HeartbeatHelper(callback, subscription)
    utils.heartbeats_block(logger, sub_future, max_time=500, helper=helper)

    # Do clean-up.
    subscription.close()
    subscription._executor.shutdown()  # Idempotent, but needed for 0.29.2.
    publisher.delete_topic(topic_path)
    subscriber.delete_subscription(subscription_path)
    teardown_summary(subscription, logger)
    thread_names.save_tree(CURR_DIR, logger)
    thread_names.restore()
    utils.restore()